All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.alex1304.ultimategdbot.api.utils.MutexPool Maven / Gradle / Ivy

There is a newer version: 6.0.2
Show newest version
package com.github.alex1304.ultimategdbot.api.utils;

import java.time.Duration;

import org.reactivestreams.Publisher;

import com.github.benmanes.caffeine.cache.Cache;
import com.github.benmanes.caffeine.cache.Caffeine;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import reactor.pool.InstrumentedPool;
import reactor.pool.PoolBuilder;

public class MutexPool {

	private final Cache> poolCache = Caffeine.newBuilder()
			.expireAfterAccess(Duration.ofMinutes(30))
			.build();
	
	public  Flux acquireUntil(Object mutex, Publisher until) {
		var pool = poolCache.asMap().computeIfAbsent(mutex, key -> PoolBuilder.from(Mono.fromCallable(Object::new))
					.sizeBetween(0, 1)
					.fifo());
		return pool.withPoolable(o -> until);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy