How to Use pubstoragelock

Hello Everybody,

I need your help and Suggestion on how to Use pub.storage.lock

Scenario :
Two Flow Services are accessing a single File for writing. I need to lock an “Object”(Using Store as an Object) in order to make any one Service to use the file at any given point of time.

What I have tried to Implement :

(I)i have defined a Store.
call 1)registerStore by giving a Suitable Store Name(Name: Store1).
call 2)add by giving a key and a value( Key : key1, value: false).
(II)i have written first flow Service which writes to the file.
call 1)lock(pub.storage:lock) by giving the following values
storeName : Store1
key : key1
waitLength : 20000
lockMode : EXCLUSIVE
call 2)writeToFile(My Custom Service)

i have not unlocked here(purposfully to test locking mechanism)

Then i have written second flow service with the same code as above calling lock again.
(So both the services are locking now on the same Store to access same file)

RunTime :

I first run the service which creates Store.
I run my first flow service which writes to file (now the store is locked and not been unlocked)
I then run my second flow service which also writes to file.

I am expecting that the second flow service should not be able to lock unless the first service releases the lock.

But surprisingly, the second service also writes to file without even waiting for anytime( second service runs normally as if there was no lock on the store)

I tried this using put and get also , but got the same results.

I initially was trying to run these services in Trace-Mode, but later i understood from documentation that locks cannot be tested in Trace-Mode. From then on, i am running the Services normally. But still not able to get the desired results.

I kindly request you all to help me in identifying where i am going wrong. or pls advice me if i am on the right track to accomplish my requirement.

Best Regards,
Nagaraj.

This is a little perplexing …

I created 2 service as you have above. At the end of the locking service I created a loop just to keep the service executing for 10 seconds. The second service just calls a get and was issued from a browser so both can be run at the same time.

  1. If I ran the locking service and upon completion run the browser server both are able to lock the key.
  2. If I ran the locking service and the browser service at the same time the browser service gets the lock error and the key stays locked until the locking service is until the wait-length is up.

It appears that the lock is removed once the locking flow completes. If another flow tries to lock it while it is locked then the it stays locked until the lock time times out. But it is only allowing 1 lock at a time.

Just my observation.