Skip to content

Commit 93fc233

Browse files
committed
Switch to ULID for ID generations
1 parent c36dfab commit 93fc233

File tree

2 files changed

+9
-10
lines changed

2 files changed

+9
-10
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ tokio = { version = "1.21.2", features = ["sync", "time"] }
2121
tower = { version = "0.4.13", features = ["util"] }
2222
tower-http = { version = "0.3.4", features = ["cors", "limit", "set-header"] }
2323
tracing = "0.1.37"
24-
uuid = { version = "1.1.2", features = ["v4", "fast-rng", "serde"] }
24+
ulid = { version = "1.0.0", features = ["serde"] }
2525

2626
[dev-dependencies]
2727
hyper = "0.14.20"

src/lib.rs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ use tower_http::{
4848
limit::RequestBodyLimitLayer,
4949
set_header::SetResponseHeaderLayer,
5050
};
51-
use uuid::Uuid;
51+
use ulid::Ulid;
5252

5353
struct Session {
5454
hash: [u8; 32],
@@ -114,12 +114,12 @@ impl Session {
114114
#[derive(Clone, Default)]
115115
struct Sessions {
116116
// TODO: is that global lock alright?
117-
inner: Arc<RwLock<HashMap<Uuid, Session>>>,
117+
inner: Arc<RwLock<HashMap<Ulid, Session>>>,
118118
ttl: Duration,
119119
}
120120

121121
impl Sessions {
122-
async fn insert(self, id: Uuid, session: Session, ttl: Duration) {
122+
async fn insert(self, id: Ulid, session: Session, ttl: Duration) {
123123
self.inner.write().await.insert(id, session);
124124
// TODO: cancel this task when an item gets deleted
125125
tokio::task::spawn(async move {
@@ -130,7 +130,7 @@ impl Sessions {
130130
}
131131

132132
impl Deref for Sessions {
133-
type Target = RwLock<HashMap<Uuid, Session>>;
133+
type Target = RwLock<HashMap<Ulid, Session>>;
134134

135135
fn deref(&self) -> &Self::Target {
136136
&self.inner
@@ -159,8 +159,7 @@ async fn new_session(
159159
payload: Bytes,
160160
) -> impl IntoResponse {
161161
let ttl = sessions.ttl;
162-
// TODO: should we use something else? Check for colisions?
163-
let id = Uuid::new_v4();
162+
let id = Ulid::new();
164163
let content_type =
165164
content_type.map_or(mime::APPLICATION_OCTET_STREAM, |TypedHeader(c)| c.into());
166165
let session = Session::new(payload, content_type, ttl);
@@ -172,7 +171,7 @@ async fn new_session(
172171
(StatusCode::CREATED, headers, additional_headers)
173172
}
174173

175-
async fn delete_session(State(sessions): State<Sessions>, Path(id): Path<Uuid>) -> StatusCode {
174+
async fn delete_session(State(sessions): State<Sessions>, Path(id): Path<Ulid>) -> StatusCode {
176175
if sessions.write().await.remove(&id).is_some() {
177176
StatusCode::NO_CONTENT
178177
} else {
@@ -182,7 +181,7 @@ async fn delete_session(State(sessions): State<Sessions>, Path(id): Path<Uuid>)
182181

183182
async fn update_session(
184183
State(sessions): State<Sessions>,
185-
Path(id): Path<Uuid>,
184+
Path(id): Path<Ulid>,
186185
content_type: Option<TypedHeader<ContentType>>,
187186
if_match: Option<TypedHeader<IfMatch>>,
188187
payload: Bytes,
@@ -206,7 +205,7 @@ async fn update_session(
206205

207206
async fn get_session(
208207
State(sessions): State<Sessions>,
209-
Path(id): Path<Uuid>,
208+
Path(id): Path<Ulid>,
210209
if_none_match: Option<TypedHeader<IfNoneMatch>>,
211210
) -> Response {
212211
let sessions = sessions.read().await;

0 commit comments

Comments
 (0)