-1
  • So after acquiring a lock on a specfic thread my code enter into critical section
  • In my critical section I have used flatMap which changes the thread
  • now since the thread has changed, that thread which was released might be allocated to a different task
  • now that different task can enter into my critical section even though my earlier task has not finished its execution and released the lock

Hence I'm not able to implement locking in reactive web flux project

  • So I was making a generic locking framework where it can used anywhere in the project(something like an annotation)
  • since function on which I apply the annotation changes the thread it was not able to implement this logic

So giving more context on this question-

NormalFlow.class

@ReactiveRedissonLock(<waitTime> <leaseTime> <lockingparams>)
Mono<String> testFunction() {
 return service.flatMap( value -> return dao.getValue() //now its waiting for dao to get the value
//thread1 is deallocated and might be allocated to a different process )
}

Consider the below code as function in LockAspect.class

public Object createLock(ProceedingJoinPoint joinPoint, ReactiveRedissonLock reactiveRedissonLock) throws Throwable {
        RLockReactive reactiveLock = getReactiveLock(requestParamMap, keyRoot);
        Long id = Thread.currentThread().getId(); // consider current thread to be thread1
//Lock taken on thread1, now thread1 can freely enter the critical section        
return 
reactiveLock.tryLock(reactiveRedissonLock.waitTime(), reactiveRedissonLock.leaseTime(), TimeUnit.SECONDS).flatMap(value -> {
            if (value == false) {
                return Mono.error(new AssessmentEngineException(HttpStatus.INTERNAL_SERVER_ERROR, "Multiple calls not allowed to this part of the code"));
            }
            try {
                return (Mono) joinPoint.proceed(); // call the normal flow of the function on which this annotation was applied
            } catch (Throwable e) {
                return Mono.error(new AssessmentEngineException(HttpStatus.INTERNAL_SERVER_ERROR, "Please check the return type used in redisson lock"));
            }
        }).doFinally(a -> reactiveLock.unlock(id).subscribe());
    }

now my question is since thread1 is deallocated and might be allocated to different process, the other process can enter this critical section with thread1 even though the earlier process has not finished its execution

so is there a way to efficiently implement locking in WebFlux

1 Answers1

0

Solved it by making my own implementation of locking which is not dependent on thread.

Basically we check if the key is present in redis and set it before a task is trying to enter the critical section(single atomic transaction, used setIfAbsent), if it was able to do so then it would enter the critical section otherwise the task would be waiting before timing out.When the task is exiting the critical section it would delete the set key from redis enabling other task waiting to enter the critical section again.

  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community May 21 '23 at 19:35