Skip to content

Commit

Permalink
Update requests parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
tinrab committed Aug 21, 2024
1 parent d591594 commit ba12c0a
Show file tree
Hide file tree
Showing 18 changed files with 293 additions and 143 deletions.
17 changes: 8 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni"
version = "0.1.57"
version = "0.1.58"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utility Library for Rust"
repository = "https://github.com/tinrab/bomboni"
Expand All @@ -27,7 +27,6 @@ members = [
"bomboni_wasm",
"bomboni_wasm_core",
"bomboni_wasm_derive",
"wasm_sample",
]

[features]
Expand All @@ -52,11 +51,11 @@ fs = ["dep:bomboni_fs"]
postgres = ["bomboni_common/postgres", "bomboni_request/postgres"]

[dependencies]
bomboni_common = { path = "bomboni_common", version = "0.1.57" }
bomboni_common = { path = "bomboni_common", version = "0.1.58" }

bomboni_prost = { path = "bomboni_prost", version = "0.1.57", default-features = false, optional = true }
bomboni_proto = { path = "bomboni_proto", version = "0.1.57", default-features = false, optional = true }
bomboni_request = { path = "bomboni_request", version = "0.1.57", default-features = false, optional = true }
bomboni_template = { path = "bomboni_template", version = "0.1.57", default-features = false, optional = true }
bomboni_wasm = { path = "bomboni_wasm", version = "0.1.57", default-features = false, optional = true }
bomboni_fs = { path = "bomboni_fs", version = "0.1.57", default-features = false, optional = true }
bomboni_prost = { path = "bomboni_prost", version = "0.1.58", default-features = false, optional = true }
bomboni_proto = { path = "bomboni_proto", version = "0.1.58", default-features = false, optional = true }
bomboni_request = { path = "bomboni_request", version = "0.1.58", default-features = false, optional = true }
bomboni_template = { path = "bomboni_template", version = "0.1.58", default-features = false, optional = true }
bomboni_wasm = { path = "bomboni_wasm", version = "0.1.58", default-features = false, optional = true }
bomboni_fs = { path = "bomboni_fs", version = "0.1.58", default-features = false, optional = true }
7 changes: 4 additions & 3 deletions bomboni_common/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_common"
version = "0.1.57"
version = "0.1.58"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Common things for Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand All @@ -15,7 +15,7 @@ path = "src/lib.rs"

[features]
serde = ["dep:serde"]
tokio = ["dep:tokio"]
tokio = ["dep:tokio", "dep:parking_lot"]
chrono = ["dep:chrono"]
wasm = [
"dep:bomboni_wasm",
Expand All @@ -27,7 +27,7 @@ js = []
postgres = ["dep:postgres-types", "dep:bytes"]

[dependencies]
bomboni_wasm = { path = "../bomboni_wasm", version = "0.1.57", features = [
bomboni_wasm = { path = "../bomboni_wasm", version = "0.1.58", features = [
"derive",
], optional = true }

Expand All @@ -36,6 +36,7 @@ regex = "1.10.5"
time = { version = "0.3.36", features = ["formatting", "parsing"] }

tokio = { version = "1.39.1", features = ["time", "sync"], optional = true }
parking_lot = { version = "0.12.3", optional = true }
serde = { version = "1.0.204", features = ["derive"], optional = true }
chrono = { version = "0.4.38", optional = true }
postgres-types = { version = "0.2.7", features = [
Expand Down
45 changes: 36 additions & 9 deletions bomboni_common/src/id/generator.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
use std::thread;
use std::time::Duration;

#[cfg(all(
target_family = "wasm",
not(any(target_os = "emscripten", target_os = "wasi")),
feature = "wasm"
))]
use wasm_bindgen::prelude::*;

#[cfg(feature = "tokio")]
use parking_lot::Mutex;
#[cfg(feature = "tokio")]
use std::{ops::Deref, sync::Arc};

use crate::date_time::UtcDateTime;
use crate::id::Id;

Expand All @@ -19,16 +25,20 @@ use crate::id::Id;
),
wasm_bindgen(js_name = IdGenerator)
)]
pub struct Generator {
pub struct IdGenerator {
worker: u16,
next: u16,
}

#[cfg(feature = "tokio")]
#[derive(Debug, Clone)]
pub struct IdGeneratorArc(Arc<Mutex<IdGenerator>>);

/// Duration to sleep after overflowing the sequence number.
/// Used to avoid collisions.
const SLEEP_DURATION: Duration = Duration::from_secs(1);

impl Generator {
impl IdGenerator {
#[must_use]
pub const fn new(worker: u16) -> Self {
Self { next: 0, worker }
Expand All @@ -41,9 +51,9 @@ impl Generator {
/// Basic usage:
///
/// ```
/// use bomboni_common::id::generator::Generator;
/// use bomboni_common::id::generator::IdGenerator;
///
/// let mut g = Generator::new(1);
/// let mut g = IdGenerator::new(1);
/// assert_ne!(g.generate(), g.generate());
/// ```
pub fn generate(&mut self) -> Id {
Expand Down Expand Up @@ -83,9 +93,9 @@ impl Generator {
///
/// ```
/// # use std::collections::HashSet;
/// use bomboni_common::id::generator::Generator;
/// use bomboni_common::id::generator::IdGenerator;
///
/// let mut g = Generator::new(1);
/// let mut g = IdGenerator::new(1);
/// let ids = g.generate_multiple(3);
/// let id_set: HashSet<_> = ids.iter().collect();
/// assert_eq!(id_set.len(), ids.len());
Expand Down Expand Up @@ -147,7 +157,7 @@ impl Generator {
feature = "wasm",
))]
#[wasm_bindgen(js_class = IdGenerator)]
impl Generator {
impl IdGenerator {
#[wasm_bindgen(constructor)]
pub fn wasm_new(worker: u16) -> Self {
Self { next: 0, worker }
Expand All @@ -159,13 +169,30 @@ impl Generator {
}
}

#[cfg(feature = "tokio")]
const _: () = {
impl IdGeneratorArc {
pub fn new(worker: u16) -> Self {
Self(Arc::new(Mutex::new(IdGenerator::new(worker))))
}
}

impl Deref for IdGeneratorArc {
type Target = Mutex<IdGenerator>;

fn deref(&self) -> &Self::Target {
&self.0
}
}
};

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn it_works() {
let mut id_generator = Generator::new(42);
let mut id_generator = IdGenerator::new(42);
let id = id_generator.generate();
let (_timestamp, worker, sequence) = id.decode();
assert_eq!(worker, 42);
Expand All @@ -179,7 +206,7 @@ mod tests {
use std::collections::HashSet;
const N: usize = 10;

let mut g = Generator::new(1);
let mut g = IdGenerator::new(1);

let mut ids = HashSet::new();
ids.extend(g.generate_multiple_async(N / 2).await);
Expand Down
2 changes: 1 addition & 1 deletion bomboni_core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_core"
version = "0.1.57"
version = "0.1.58"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Internal part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
2 changes: 1 addition & 1 deletion bomboni_fs/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_fs"
version = "0.1.57"
version = "0.1.58"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilites for working with the file system. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down
4 changes: 2 additions & 2 deletions bomboni_prost/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_prost"
version = "0.1.57"
version = "0.1.58"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working with prost. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand All @@ -14,7 +14,7 @@ name = "bomboni_prost"
path = "src/lib.rs"

[dependencies]
bomboni_core = { path = "../bomboni_core", version = "0.1.57" }
bomboni_core = { path = "../bomboni_core", version = "0.1.58" }

prost = "0.13.1"
prost-types = "0.13.1"
Expand Down
8 changes: 4 additions & 4 deletions bomboni_proto/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_proto"
version = "0.1.57"
version = "0.1.58"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working with Protobuf/gRPC. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down Expand Up @@ -30,7 +30,7 @@ js = ["bomboni_common/js"]
[dependencies]
bomboni_common = { path = "../bomboni_common", features = [
"serde",
], version = "0.1.57" }
], version = "0.1.58" }

thiserror = "1.0.63"
time = { version = "0.3.36", features = ["serde", "formatting", "parsing"] }
Expand All @@ -44,7 +44,7 @@ http = { version = "1.1.0", optional = true }
chrono = { version = "0.4.38", optional = true }

[target.'cfg(all(target_family = "wasm", not(any(target_os = "emscripten", target_os = "wasi"))))'.dependencies]
bomboni_wasm = { path = "../bomboni_wasm", version = "0.1.57", optional = true }
bomboni_wasm = { path = "../bomboni_wasm", version = "0.1.58", optional = true }

wasm-bindgen = { version = "0.2.92", optional = true }
js-sys = { version = "0.3.69", optional = true }
Expand All @@ -54,5 +54,5 @@ serde-wasm-bindgen = { version = "0.6.5", optional = true }
serde_json = "1.0.120"

[build-dependencies]
bomboni_prost = { path = "../bomboni_prost", version = "0.1.57" }
bomboni_prost = { path = "../bomboni_prost", version = "0.1.58" }
prost-build = "0.13.1"
10 changes: 5 additions & 5 deletions bomboni_request/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bomboni_request"
version = "0.1.57"
version = "0.1.58"
authors = ["Tin Rabzelj <tin@flinect.com>"]
description = "Utilities for working with API requests. Part of Bomboni library."
repository = "https://github.com/tinrab/bomboni"
Expand Down Expand Up @@ -29,10 +29,10 @@ wasm = [
postgres = ["dep:postgres-types", "dep:bytes"]

[dependencies]
bomboni_common = { path = "../bomboni_common", version = "0.1.57" }
bomboni_proto = { path = "../bomboni_proto", version = "0.1.57" }
bomboni_request_derive = { path = "../bomboni_request_derive", version = "0.1.57" }
bomboni_wasm = { path = "../bomboni_wasm", version = "0.1.57", features = [
bomboni_common = { path = "../bomboni_common", version = "0.1.58" }
bomboni_proto = { path = "../bomboni_proto", version = "0.1.58" }
bomboni_request_derive = { path = "../bomboni_request_derive", version = "0.1.58" }
bomboni_wasm = { path = "../bomboni_wasm", version = "0.1.58", features = [
"derive",
], optional = true }

Expand Down
Loading

0 comments on commit ba12c0a

Please sign in to comment.