chenggangpro / reactive-lock

Reactive Lock Based On SpringBoot and simple Reactive-Redis-Distributed-Lock

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

reactive-lock

Reactive Lock Demo

Use Case

  • 1 . Make Sure Use Spring Boot Starter Dependency if using redis-reactive-lock
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
  • 2 . Configure Properties
lock:
  reactive:
    expire-after: 10s
    expire-evict-idle: 1s
    type:
      - DEFAULT   # default using simple spin lock
      - REDIS    # redis
      - CLH     # jvm lock using CLH algorithm
      - MCS     # jvm lock using MCS algorithm
  • 3 . Autowired By Spring

    • ReactiveLockRegistry defaultReactiveLockRegistry
    • ReactiveLockRegistry redisReactiveLockRegistry
    • ReactiveLockRegistry clhReactiveLockRegistry
    • ReactiveLockRegistry mcsReactiveLockRegistry
  • 4 . Use ReactiveLockRegistry With Your Reactive Application

public interface ReactiveLock {

    /**
     * Try to acquire the lock once
     * @param lockResultExecution apply lockResult return executable Mono
     * @param <T> Mono type
     * @return executable Mono
     */
    <T> Mono<T> tryLockThenExecute(@NotNull Function<Boolean, Mono<T>> lockResultExecution);

    /**
     * Try to acquire the lock once
     * @param lockResultExecution apply lockResult return executable Flux
     * @param <T> Flux type
     * @return executable Flux
     */
    <T> Flux<T> tryLockThenExecuteMany(@NotNull Function<Boolean, Flux<T>> lockResultExecution);

    /**
     * Try to acquire the lock for a given duration.
     * @param duration lock expire duration
     * @param lockResultExecution apply lockResult return executable Mono
     * @param <T> Mono type
     * @return executable Mono
     */
    <T> Mono<T> lockThenExecute(@NotNull Duration duration, @NotNull Function<Boolean, Mono<T>> lockResultExecution);

    /**
     * Try to acquire the lock for a given duration.
     * @param duration lock expire duration
     * @param lockResultExecution apply lockResult return executable Flux
     * @param <T> Flux type
     * @return executable Flux
     */
    <T> Flux<T> lockThenExecuteMany(@NotNull Duration duration, @NotNull Function<Boolean, Flux<T>> lockResultExecution);

}
  • 5 .If You Need More Detail About This Please Read The Source Code and test cases.

Reference

Benchmark Results

Benchmark details is in benchmark folder

To display benchmark results ,recommending using this website jmh.morethan.io

About

Reactive Lock Based On SpringBoot and simple Reactive-Redis-Distributed-Lock


Languages

Language:Java 100.0%