@@ -500,6 +500,54 @@ start().catch((e) => {
500
500
});
501
501
```
502
502
503
+ ### Distributed Lock API
504
+
505
+ #### Try Lock and Unlock APIs
506
+
507
+ ``` javascript
508
+ import { CommunicationProtocolEnum , DaprClient } from " @dapr/dapr" ;
509
+ import { LockStatus } from " @dapr/dapr/types/lock/UnlockResponse" ;
510
+
511
+ const daprHost = " 127.0.0.1" ;
512
+ const daprPortDefault = " 3500" ;
513
+
514
+ async function start () {
515
+ const client = new DaprClient (daprHost, daprPort);
516
+
517
+ const storeName = " redislock" ;
518
+ const resourceId = " resourceId" ;
519
+ const lockOwner = " owner1" ;
520
+ let expiryInSeconds = 1000 ;
521
+
522
+ console .log (` Acquiring lock on ${ storeName} , ${ resourceId} as owner: ${ lockOwner} ` );
523
+ const tryLockResponse = await client .lock .tryLock (storeName, resourceId, lockOwner, expiryInSeconds);
524
+ console .log (tryLockResponse);
525
+
526
+ console .log (` Unlocking on ${ storeName} , ${ resourceId} as owner: ${ lockOwner} ` );
527
+ const unlockResponse = await client .lock .unlock (storeName, resourceId, lockOwner);
528
+ console .log (" Unlock API response: " + getResponseStatus (unlockResponse .status ));
529
+ }
530
+
531
+ function getResponseStatus (status : LockStatus ) {
532
+ switch (status) {
533
+ case LockStatus .Success :
534
+ return " Success" ;
535
+ case LockStatus .LockDoesNotExist :
536
+ return " LockDoesNotExist" ;
537
+ case LockStatus .LockBelongsToOthers :
538
+ return " LockBelongsToOthers" ;
539
+ default :
540
+ return " InternalError" ;
541
+ }
542
+ }
543
+
544
+ start ().catch ((e ) => {
545
+ console .error (e);
546
+ process .exit (1 );
547
+ });
548
+ ```
549
+ > For a full guide on distributed locks visit [ How-To: Use Distributed Locks] ({{< ref howto-use-distributed-lock.md >}}).
550
+
503
551
## Related links
504
552
505
553
- [ JavaScript SDK examples] ( https://github.com/dapr/js-sdk/tree/master/examples )
0 commit comments