- 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