Distributed Lock API reference

Detailed documentation on the distributed lock API

Lock

This endpoint lets you acquire a lock by supplying a named lock owner and the resource ID to lock.

HTTP Request

  1. POST http://localhost:<daprPort>/v1.0-alpha1/lock/<storename>

URL Parameters

ParameterDescription
daprPortThe Dapr port
storenameThe metadata.name field component file. Refer to the [component schema] (https://v1-8.docs.dapr.io/operations/components/component-schema/)

Query Parameters

None

HTTP Response codes

CodeDescription
200Request successful
204Empty Response
400Malformed request
500Request failed

HTTP Request Body

The lock endpoint receives the following JSON payload:

  1. {
  2. "resourceId": "",
  3. "lockOwner": "",
  4. "expiryInSeconds": 0
  5. }
FieldDescription
resourceIdThe ID of the resource to lock. Can be any value
lockOwnerThe name of the lock owner. Should be set to a unique value per-request
expiryInSecondsThe time in seconds to hold the lock before it expires

HTTP Response Body

The lock endpoint returns the following payload:

  1. {
  2. "success": true
  3. }

Examples

  1. curl -X POST http://localhost:3500/v1.0-alpha/lock/redisStore \
  2. -H "Content-Type: application/json" \
  3. -d '{
  4. "resourceId": "lock1",
  5. "lockOwner": "vader",
  6. "expiryInSeconds": 60
  7. }'
  8. {
  9. "success": "true"
  10. }

Unlock

This endpoint lets you unlock an existing lock based on the lock owner and resource Id

HTTP Request

  1. POST http://localhost:<daprPort>/v1.0-alpha1/unlock/<storename>

URL Parameters

ParameterDescription
daprPortThe Dapr port
storenameThe metadata.name field component file. Refer to the [component schema] (https://v1-8.docs.dapr.io/operations/components/component-schema/)

Query Parameters

None

HTTP Response codes

CodeDescription
200Request successful
204Empty Response
400Malformed request
500Request failed

HTTP Request Body

The unlock endpoint receives the following JSON payload:

  1. {
  2. "resourceId": "",
  3. "lockOwner": ""
  4. }

HTTP Response Body

The unlock endpoint returns the following payload:

  1. {
  2. "status": 0
  3. }

The status field contains the following response codes:

CodeDescription
0Success
1Lock doesn’t exist
2Lock belongs to another owner
3Internal error

Examples

  1. curl -X POST http://localhost:3500/v1.0-alpha/unlock/redisStore \
  2. -H "Content-Type: application/json" \
  3. -d '{
  4. "resourceId": "lock1",
  5. "lockOwner": "vader"
  6. }'
  7. {
  8. "status": 0
  9. }

Last modified July 5, 2022: Add distributed lock api reference (#2596) (09b7e18d)