snowflaked 0.1.2 → 0.1.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +18 -1
- data/ext/snowflaked/Cargo.toml +1 -1
- data/ext/snowflaked/src/lib.rs +34 -17
- data/lib/snowflaked/model_extensions.rb +15 -5
- data/lib/snowflaked/version.rb +1 -1
- data/lib/snowflaked.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 4a6f86e1432fc8b11f282674da8568ffecb835bb5aba3d5c52c124acb4a55ace
|
|
4
|
+
data.tar.gz: 2d13a338cceed05710c0afa3e56896d8199d90b1934ac259e256bed49ba8c3cf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 54d45cfb0b0f503e8cc839ac7530439582e08ee11a98e3ec8a79bd9cc3bb0539cd8a0096e2d6618a9015c712bc02e8423ee18dc3d325eb695bdb4e413ad998dd
|
|
7
|
+
data.tar.gz: 4f291445d0e558e1e856f146d5972d52ac068ee907cb20aa4a2ea8d9eb0be818c3d4b53738e0b3ade960662aa8a5f05e6e616f210e0c7a3de7c37d089cbe9454
|
data/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://rubygems.org/gems/snowflaked)
|
|
6
6
|
[](LICENSE.txt)
|
|
7
7
|
|
|
8
|
-
A high-performance, thread-safe Snowflake ID generator for Ruby, powered by Rust.
|
|
8
|
+
A database-agnostic, high-performance, thread-safe Snowflake ID generator for Ruby, powered by Rust.
|
|
9
9
|
|
|
10
10
|
Snowflake IDs are 64-bit unique identifiers that encode a timestamp, machine ID, and sequence number. They're time-sortable (IDs created later are always larger), making them ideal for distributed systems where you need unique IDs without coordination between machines. Unlike UUIDs, Snowflake IDs are smaller, sortable, and index-friendly for databases.
|
|
11
11
|
|
|
@@ -87,6 +87,13 @@ Snowflaked.configure do |config|
|
|
|
87
87
|
end
|
|
88
88
|
```
|
|
89
89
|
|
|
90
|
+
### Machine ID
|
|
91
|
+
|
|
92
|
+
> [!TIP]
|
|
93
|
+
> For multi-process servers like Puma, it is recommended to **not** configure `machine_id` explicitly. The gem automatically calculates a unique machine ID using `(hostname.hash ^ pid) % 1024`, which ensures each forked worker process gets a different ID and avoids duplicate Snowflake IDs.
|
|
94
|
+
|
|
95
|
+
If you must set `machine_id` explicitly, use environment variables that differ per worker process.
|
|
96
|
+
|
|
90
97
|
### Machine ID Resolution
|
|
91
98
|
|
|
92
99
|
If `machine_id` is not explicitly configured, it resolves in this order:
|
|
@@ -136,6 +143,12 @@ Snowflaked.machine_id(id)
|
|
|
136
143
|
# => 42
|
|
137
144
|
```
|
|
138
145
|
|
|
146
|
+
## Benchmarks
|
|
147
|
+
|
|
148
|
+
See [BENCHMARKS.md](benchmarks/BENCHMARKS.md) for more details.
|
|
149
|
+
|
|
150
|
+
tl;dr: Snowflake IDs have a negligible performance impact compared to database-backed IDs.
|
|
151
|
+
|
|
139
152
|
## Requirements
|
|
140
153
|
|
|
141
154
|
- Ruby >= 3.2
|
|
@@ -151,6 +164,10 @@ bundle install
|
|
|
151
164
|
bundle exec rake
|
|
152
165
|
```
|
|
153
166
|
|
|
167
|
+
## Acknowledgments
|
|
168
|
+
|
|
169
|
+
- [snowflaked-rs](https://github.com/MrGunflame/snowflaked-rs) - the Rust implementation of Snowflake IDs
|
|
170
|
+
|
|
154
171
|
## License
|
|
155
172
|
|
|
156
173
|
MIT
|
data/ext/snowflaked/Cargo.toml
CHANGED
data/ext/snowflaked/src/lib.rs
CHANGED
|
@@ -1,50 +1,65 @@
|
|
|
1
1
|
use magnus::{function, prelude::*, Error, RHash, Ruby};
|
|
2
2
|
use snowflaked::sync::Generator;
|
|
3
3
|
use snowflaked::{Builder, Snowflake};
|
|
4
|
-
use std::sync::
|
|
4
|
+
use std::sync::RwLock;
|
|
5
5
|
use std::time::UNIX_EPOCH;
|
|
6
6
|
|
|
7
7
|
struct GeneratorState {
|
|
8
8
|
generator: Generator,
|
|
9
9
|
epoch_offset: u64,
|
|
10
10
|
machine_id: u16,
|
|
11
|
+
init_pid: u32,
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
static STATE:
|
|
14
|
+
static STATE: RwLock<Option<GeneratorState>> = RwLock::new(None);
|
|
14
15
|
|
|
15
16
|
fn init_generator(machine_id: u16, epoch_ms: Option<u64>) -> bool {
|
|
16
|
-
let
|
|
17
|
+
let current_pid = std::process::id();
|
|
17
18
|
let epoch_offset = epoch_ms.unwrap_or(0);
|
|
18
19
|
|
|
19
|
-
STATE.
|
|
20
|
-
let epoch = UNIX_EPOCH + std::time::Duration::from_millis(epoch_offset);
|
|
20
|
+
let mut state = STATE.write().unwrap();
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
if state.as_ref().is_some_and(|s| s.init_pid == current_pid) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
23
25
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
26
|
+
let epoch = UNIX_EPOCH + std::time::Duration::from_millis(epoch_offset);
|
|
27
|
+
let generator = Builder::new().instance(machine_id).epoch(epoch).build();
|
|
28
|
+
|
|
29
|
+
*state = Some(GeneratorState {
|
|
30
|
+
generator,
|
|
31
|
+
epoch_offset,
|
|
32
|
+
machine_id,
|
|
33
|
+
init_pid: current_pid,
|
|
29
34
|
});
|
|
30
35
|
|
|
31
|
-
|
|
36
|
+
true
|
|
32
37
|
}
|
|
33
38
|
|
|
34
39
|
fn generate(ruby: &Ruby) -> Result<u64, Error> {
|
|
35
|
-
let state = STATE.
|
|
40
|
+
let state = STATE.read().unwrap();
|
|
41
|
+
|
|
42
|
+
let s = state.as_ref().ok_or_else(|| {
|
|
36
43
|
Error::new(
|
|
37
44
|
ruby.exception_runtime_error(),
|
|
38
45
|
"Generator not initialized. Call Snowflaked.configure or Snowflaked.id first.",
|
|
39
46
|
)
|
|
40
47
|
})?;
|
|
41
48
|
|
|
42
|
-
|
|
49
|
+
if s.init_pid != std::process::id() {
|
|
50
|
+
return Err(Error::new(
|
|
51
|
+
ruby.exception_runtime_error(),
|
|
52
|
+
"Fork detected: generator was initialized in a different process. This should not happen if using Snowflaked.id - please report this bug.",
|
|
53
|
+
));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
Ok(s.generator.generate())
|
|
43
57
|
}
|
|
44
58
|
|
|
45
59
|
fn timestamp_ms(id: u64) -> u64 {
|
|
46
60
|
let timestamp_raw = id.timestamp();
|
|
47
|
-
let
|
|
61
|
+
let state = STATE.read().unwrap();
|
|
62
|
+
let epoch_offset = state.as_ref().map(|s| s.epoch_offset).unwrap_or(0);
|
|
48
63
|
timestamp_raw.saturating_add(epoch_offset)
|
|
49
64
|
}
|
|
50
65
|
|
|
@@ -67,11 +82,13 @@ fn sequence(id: u64) -> u64 {
|
|
|
67
82
|
}
|
|
68
83
|
|
|
69
84
|
fn is_initialized() -> bool {
|
|
70
|
-
STATE.
|
|
85
|
+
let state = STATE.read().unwrap();
|
|
86
|
+
state.as_ref().is_some_and(|s| s.init_pid == std::process::id())
|
|
71
87
|
}
|
|
72
88
|
|
|
73
89
|
fn configured_machine_id() -> Option<u16> {
|
|
74
|
-
STATE.
|
|
90
|
+
let state = STATE.read().unwrap();
|
|
91
|
+
state.as_ref().and_then(|s| if s.init_pid == std::process::id() { Some(s.machine_id) } else { None })
|
|
75
92
|
}
|
|
76
93
|
|
|
77
94
|
#[magnus::init]
|
|
@@ -5,8 +5,8 @@ module Snowflaked
|
|
|
5
5
|
extend ActiveSupport::Concern
|
|
6
6
|
|
|
7
7
|
included do
|
|
8
|
-
class_attribute :_snowflake_attributes, instance_writer: false, default: [:id]
|
|
9
|
-
|
|
8
|
+
class_attribute :_snowflake_attributes, instance_writer: false, default: [:id]
|
|
9
|
+
after_initialize :_generate_snowflake_ids, if: :new_record?
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
class_methods do
|
|
@@ -14,19 +14,29 @@ module Snowflaked
|
|
|
14
14
|
attrs = attributes.map(&:to_sym)
|
|
15
15
|
attrs |= [:id] if id
|
|
16
16
|
self._snowflake_attributes = attrs
|
|
17
|
+
@_snowflake_attributes_with_columns = nil
|
|
17
18
|
end
|
|
18
19
|
|
|
19
20
|
def _snowflake_columns_from_comments
|
|
20
|
-
return
|
|
21
|
+
return @_snowflake_columns_from_comments if defined?(@_snowflake_columns_from_comments)
|
|
21
22
|
|
|
22
|
-
|
|
23
|
+
@_snowflake_columns_from_comments = if table_exists?
|
|
24
|
+
columns.filter_map { |col| col.name.to_sym if col.comment == Snowflaked::SchemaDefinitions::COMMENT }
|
|
25
|
+
else
|
|
26
|
+
[]
|
|
27
|
+
end
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
def _snowflake_attributes_with_columns
|
|
31
|
+
@_snowflake_attributes_with_columns ||= (_snowflake_attributes | _snowflake_columns_from_comments)
|
|
23
32
|
end
|
|
24
33
|
end
|
|
25
34
|
|
|
26
35
|
private
|
|
27
36
|
|
|
28
37
|
def _generate_snowflake_ids
|
|
29
|
-
attributes_to_generate = self.class.
|
|
38
|
+
attributes_to_generate = self.class._snowflake_attributes_with_columns
|
|
39
|
+
return if attributes_to_generate.empty?
|
|
30
40
|
|
|
31
41
|
attributes_to_generate.each do |attribute|
|
|
32
42
|
next if self[attribute].present?
|
data/lib/snowflaked/version.rb
CHANGED
data/lib/snowflaked.rb
CHANGED