File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -29,10 +29,10 @@ export class Lock {
2929 ) {
3030 this . #key = key
3131 this . #config = config
32+ this . #ttl = ttl ?? null
3233 this . #lockStore = lockStore
3334 this . #expirationTime = expirationTime ?? null
3435 this . #owner = owner ?? this . #generateOwner( )
35- this . #ttl = ttl ?? null
3636 }
3737
3838 /**
@@ -75,17 +75,30 @@ export class Lock {
7575 const start = Date . now ( )
7676 while ( attemptsDone ++ < attemptsMax ) {
7777 const now = Date . now ( )
78+
79+ /**
80+ * Try to acquire the lock
81+ */
7882 const result = await this . #lockStore. save ( this . #key, this . #owner, this . #ttl)
7983 if ( result ) {
8084 this . #expirationTime = this . #ttl ? now + this . #ttl : null
8185 break
8286 }
8387
88+ /**
89+ * Check if we reached the maximum number of attempts
90+ */
8491 if ( attemptsDone === attemptsMax ) throw new E_LOCK_TIMEOUT ( )
8592
93+ /**
94+ * Or check if we reached the timeout
95+ */
8696 const elapsed = Date . now ( ) - start
8797 if ( timeout && elapsed > timeout ) throw new E_LOCK_TIMEOUT ( )
8898
99+ /**
100+ * Otherwise wait for the delay and try again
101+ */
89102 await setTimeout ( delay )
90103 }
91104
You can’t perform that action at this time.
0 commit comments