Skip to content

Commit 41b6c6c

Browse files
committed
Refactor token consumption logic for race conditions.
1 parent 0cce23c commit 41b6c6c

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

index.js

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -141,9 +141,10 @@ export async function consume(arg) {
141141
};
142142
}
143143

144-
// Hitting this point means the second latest token is used while the latest token is already
145-
// issued, which might happen on race condition (e.g. user sends multiple requests in parallel).
146-
// This token is still considered active, but cannot be used to rotate token.
144+
// Hitting this point means the second latest token is used in reqeust while the latest token is
145+
// already issued.
146+
// This might happen on a race condition where the client sends multiple requests simultaneously.
147+
// This second latest token is still considered active, but cannot be used to rotate token.
147148
if (isToken2) {
148149
return { state: "Active" };
149150
}
@@ -167,6 +168,10 @@ export async function consume(arg) {
167168
},
168169
};
169170
}
171+
172+
// Hitting this point means the latest token is used and already expired.
173+
// We will rotate the token.
174+
170175
const nextToken = generate256BitEntropyHex();
171176
const nextTokenHash = await hash(nextToken);
172177

0 commit comments

Comments
 (0)