Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggest reactor version of semaphore implementation #292

Open
semistone opened this issue Mar 23, 2023 · 4 comments
Open

Suggest reactor version of semaphore implementation #292

semistone opened this issue Mar 23, 2023 · 4 comments
Labels
type/enhancement A general enhancement
Milestone

Comments

@semistone
Copy link

Motivation

in non-reactive programming, we could use synchronized or semaphore to guarantee only one thread enters some critical block.
e.g:
public synchronized fun() {
...
}

even though we already use the reactor, we still don't want the same user to call our service before the previous request is finished because some of our libraries aren't thread-safe.
but in reactor programming, we don't want to control that in the synchronized block or semaphore as those are blocking methods.

Desired solution

provide a simple utility like

ReactorSemaphore reactorSemaphore = new ReactorSemaphore(1);
Mono<?> mono1 = reactorSemaphore.acquire(lock -> Mono.just("test1")...)
Mono<?> mono2 = reactorSemaphore.acquire(lock -> Mono.just("test2")...)

when mono1 and mono2 subscribe at the same time, they will subscribe first mono and only after the first mono is terminated, then subscribe second one. which is similar to controlled by Semaphore.
but implemented in a non-blocking way.

actually, I already implement that utility, but still wondering do you have any suggestions or a better way to solve this problem by native reactor library.
or if you are interested, I could share my utility later.

Considered alternatives

Additional context

@reactorbot reactorbot added the ❓need-triage This issue needs triage, hasn't been looked at by a team member yet label Mar 23, 2023
@OlegDokuka OlegDokuka transferred this issue from reactor/reactor-core Mar 23, 2023
@OlegDokuka
Copy link
Contributor

@semistone transferred to addons

@OlegDokuka OlegDokuka added type/enhancement A general enhancement and removed ❓need-triage This issue needs triage, hasn't been looked at by a team member yet labels Mar 23, 2023
@OlegDokuka OlegDokuka added this to the Backlog milestone Mar 23, 2023
@semistone
Copy link
Author

I had done a few unit tests,
it's simple POC but it worked.

@semistone
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

3 participants