snowflaked 0.1.2 → 0.1.3
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 +11 -0
- data/ext/snowflaked/Cargo.toml +1 -1
- data/ext/snowflaked/src/lib.rs +34 -17
- data/lib/snowflaked/model_extensions.rb +7 -3
- 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: 2d0d8f2af68a50e38cc1aa32c1adc0641fbcdb2d0f01d0bc6e942520da33089f
|
|
4
|
+
data.tar.gz: 47f1b23d9a38e2f60ab43f7059c8efd27b783012fbdc8f510ed220b221e3200d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: '0278267978cd6a55be0f569659f36ff841c874cbeb2e813cc7f88c1789cf6d8b7ef21886d0bffe1f3268b9c34e1dede2b2eea75ba77468760acbf956d069d57f'
|
|
7
|
+
data.tar.gz: 8f2dc54ef2896404ee4698644844ca2611ccb7b6339735ffeb0a7e2dc38854b1df73035c9e7f66a92202c6b5001760b814907303007db2c327dd32195b1d917f
|
data/README.md
CHANGED
|
@@ -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:
|
|
@@ -151,6 +158,10 @@ bundle install
|
|
|
151
158
|
bundle exec rake
|
|
152
159
|
```
|
|
153
160
|
|
|
161
|
+
## Acknowledgments
|
|
162
|
+
|
|
163
|
+
- [snowflaked-rs](https://github.com/MrGunflame/snowflaked-rs) - the Rust implementation of Snowflake IDs
|
|
164
|
+
|
|
154
165
|
## License
|
|
155
166
|
|
|
156
167
|
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,7 +5,7 @@ module Snowflaked
|
|
|
5
5
|
extend ActiveSupport::Concern
|
|
6
6
|
|
|
7
7
|
included do
|
|
8
|
-
class_attribute :_snowflake_attributes, instance_writer: false, default: [:id]
|
|
8
|
+
class_attribute :_snowflake_attributes, instance_writer: false, default: [:id]
|
|
9
9
|
before_validation :_generate_snowflake_ids, on: :create
|
|
10
10
|
end
|
|
11
11
|
|
|
@@ -17,9 +17,13 @@ module Snowflaked
|
|
|
17
17
|
end
|
|
18
18
|
|
|
19
19
|
def _snowflake_columns_from_comments
|
|
20
|
-
return
|
|
20
|
+
return @_snowflake_columns_from_comments if defined?(@_snowflake_columns_from_comments)
|
|
21
21
|
|
|
22
|
-
|
|
22
|
+
@_snowflake_columns_from_comments = if table_exists?
|
|
23
|
+
columns.filter_map { |col| col.name.to_sym if col.comment == Snowflaked::SchemaDefinitions::COMMENT }
|
|
24
|
+
else
|
|
25
|
+
[]
|
|
26
|
+
end
|
|
23
27
|
end
|
|
24
28
|
end
|
|
25
29
|
|
data/lib/snowflaked/version.rb
CHANGED
data/lib/snowflaked.rb
CHANGED