io.github.bucket4j.BucketConfiguration Java Examples

The following examples show how to use io.github.bucket4j.BucketConfiguration. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: InfinispanProxy.java    From bucket4j with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<BucketConfiguration> getConfiguration(K key) {
    try {
        SerializableFunction<EntryView.ReadWriteEntryView<K, GridBucketState>, GridBucketState> findFunction =
                (SerializableFunction<EntryView.ReadWriteEntryView<K, GridBucketState>, GridBucketState>)
                entry -> entry.find().orElse(null);
        GridBucketState state = readWriteMap.eval(key, findFunction).get();
        if (state == null) {
            return Optional.empty();
        } else {
            return Optional.of(state.getConfiguration());
        }
    } catch (InterruptedException | ExecutionException e) {
        throw new CacheException(e);
    }
}
 
Example #2
Source File: InfinispanProxy.java    From bucket4j with Apache License 2.0 6 votes vote down vote up
@Override
public Optional<BucketConfiguration> getConfiguration(K key) {
    try {
        SerializableFunction<EntryView.ReadWriteEntryView<K, GridBucketState>, GridBucketState> findFunction =
                (SerializableFunction<EntryView.ReadWriteEntryView<K, GridBucketState>, GridBucketState>)
                entry -> entry.find().orElse(null);
        GridBucketState state = readWriteMap.eval(key, findFunction).get();
        if (state == null) {
            return Optional.empty();
        } else {
            return Optional.of(state.getConfiguration());
        }
    } catch (InterruptedException | ExecutionException e) {
        throw new CacheException(e);
    }
}
 
Example #3
Source File: GridProxyMock.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Override
public Serializable createInitialStateAndExecute(Serializable key, BucketConfiguration configuration, GridCommand command) {
    if (exception != null) {
        throw new RuntimeException();
    }
    createInitialState(key, configuration);
    return execute(key, command);
}
 
Example #4
Source File: GridProxyMock.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<BucketConfiguration> getConfiguration(Serializable key) {
    if (exception != null) {
        throw new RuntimeException();
    }
    return Optional.of(state.getConfiguration());
}
 
Example #5
Source File: GridProxyMock.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Override
public void createInitialState(Serializable key, BucketConfiguration configuration) {
    if (exception != null) {
        throw new RuntimeException();
    }
    BucketState bucketState = BucketState.createInitialState(configuration, timeMeter.currentTimeNanos());
    this.state = new GridBucketState(configuration, bucketState);
}
 
Example #6
Source File: GridProxyMock.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture createInitialStateAndExecuteAsync(Serializable key, BucketConfiguration configuration, GridCommand command) {
    if (exception != null) {
        CompletableFuture future = new CompletableFuture();
        future.completeExceptionally(new RuntimeException());
        return future;
    }
    return CompletableFuture.completedFuture(createInitialStateAndExecute(key, configuration, command));
}
 
Example #7
Source File: GridBucketState.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
public BucketConfiguration replaceConfigurationOrReturnPrevious(BucketConfiguration newConfiguration) {
    if (!configuration.isCompatible(newConfiguration)) {
        return configuration;
    }
    configuration = newConfiguration;
    return null;
}
 
Example #8
Source File: InfinispanSerializerTest.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Test
public void tetsSerializationOfEntryProcessors() {
    BucketConfiguration configuration = Bucket4j.configurationBuilder()
            .addLimit(simple(10, ofSeconds(1)))
            .build();
    GridCommand command = new AddTokensCommand(42);

    testSerialization(new InitStateProcessor<>(configuration));
    testSerialization(new ExecuteProcessor<>(command));
    testSerialization(new InitStateAndExecuteProcessor(command, configuration));

    testSerialization(new SerializableFunctionAdapter<>(new ExecuteProcessor<>(command)));
}
 
Example #9
Source File: ReplaceConfigurationOrReturnPreviousCommand.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Override
public BucketConfiguration execute(GridBucketState state, long currentTimeNanos) {
    state.refillAllBandwidth(currentTimeNanos);
    BucketConfiguration previousConfiguration = state.replaceConfigurationOrReturnPrevious(newConfiguration);
    if (previousConfiguration != null) {
        return previousConfiguration;
    }
    replaced = true;
    return null;
}
 
Example #10
Source File: HazelcastProxy.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<BucketConfiguration> getConfiguration(K key) {
    GridBucketState state = cache.get(key);
    if (state == null) {
        return Optional.empty();
    } else {
        return Optional.of(state.getConfiguration());
    }
}
 
Example #11
Source File: HazelcastSerializerTest.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Test
public void tetsSerializationOfEntryProcessors() {
    BucketConfiguration configuration = Bucket4j.configurationBuilder()
            .addLimit(simple(10, ofSeconds(1)))
            .build();
    GridCommand command = new AddTokensCommand(42);

    testSerialization(new InitStateProcessor<>(configuration));
    testSerialization(new ExecuteProcessor<>(command));
    testSerialization(new InitStateAndExecuteProcessor(command, configuration));
}
 
Example #12
Source File: CoherenceProxy.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<BucketConfiguration> getConfiguration(K key) {
    GridBucketState state = cache.get(key);
    if (state == null) {
        return Optional.empty();
    } else {
        return Optional.of(state.getConfiguration());
    }
}
 
Example #13
Source File: JCacheProxy.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<BucketConfiguration> getConfiguration(K key) {
    GridBucketState state = cache.get(key);
    if (state == null) {
        return Optional.empty();
    } else {
        return Optional.of(state.getConfiguration());
    }
}
 
Example #14
Source File: CoherenceProxyManager.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Override
public Bucket getProxy(K key, Supplier<BucketConfiguration> supplier) {
    return GridBucket.createLazyBucket(key, supplier, gridProxy);
}
 
Example #15
Source File: HazelcastProxy.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Override
public Optional<BucketConfiguration> getConfiguration(K key) {
    GridBucketState state = cache.get(key);
    if (state == null) {
        return Optional.empty();
    } else {
        return Optional.of(state.getConfiguration());
    }
}
 
Example #16
Source File: HazelcastSerializerTest.java    From bucket4j with Apache License 2.0 5 votes vote down vote up
@Test
public void tetsSerializationOfEntryProcessors() {
    BucketConfiguration configuration = Bucket4j.configurationBuilder()
            .addLimit(simple(10, ofSeconds(1)))
            .build();
    GridCommand command = new AddTokensCommand(42);

    testSerialization(new InitStateProcessor<>(configuration));
    testSerialization(new ExecuteProcessor<>(command));
    testSerialization(new InitStateAndExecuteProcessor(command, configuration));
}
 
Example #17
Source File: JCacheProxy.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends Serializable> CompletableFuture<T> createInitialStateAndExecuteAsync(K key, BucketConfiguration configuration, GridCommand<T> command) {
    // because JCache does not specify async API
    throw new UnsupportedOperationException();
}
 
Example #18
Source File: InfinispanProxy.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends Serializable> CompletableFuture<T> createInitialStateAndExecuteAsync(K key, BucketConfiguration configuration, GridCommand<T> command) {
    JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.initStateAndExecuteProcessor(command, configuration);
    CompletableFuture<CommandResult<T>> result = invokeAsync(key, entryProcessor);
    return result.thenApply(CommandResult::getData);
}
 
Example #19
Source File: JCacheProxyManager.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<BucketConfiguration> getProxyConfiguration(K key) {
    return gridProxy.getConfiguration(key);
}
 
Example #20
Source File: InitStateAndExecuteProcessor.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
public BucketConfiguration getConfiguration() {
    return configuration;
}
 
Example #21
Source File: JCacheProxyManager.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
public Bucket getProxy(K key, Supplier<BucketConfiguration> supplier) {
    return GridBucket.createLazyBucket(key, supplier, gridProxy);
}
 
Example #22
Source File: InfinispanProxy.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends Serializable> T createInitialStateAndExecute(K key, BucketConfiguration configuration, GridCommand<T> command) {
    JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.initStateAndExecuteProcessor(command, configuration);
    CommandResult<T> result = invokeSync(key, entryProcessor);
    return result.getData();
}
 
Example #23
Source File: InfinispanProxy.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
public void createInitialState(K key, BucketConfiguration configuration) {
    JCacheEntryProcessor<K, Nothing> entryProcessor = JCacheEntryProcessor.initStateProcessor(configuration);
    invokeSync(key, entryProcessor);
}
 
Example #24
Source File: InfinispanProxyManager.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<BucketConfiguration> getProxyConfiguration(K key) {
    return gridProxy.getConfiguration(key);
}
 
Example #25
Source File: InfinispanProxyManager.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
public Bucket getProxy(K key, Supplier<BucketConfiguration> supplier) {
    return GridBucket.createLazyBucket(key, supplier, gridProxy);
}
 
Example #26
Source File: InfinispanProxy.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends Serializable> CompletableFuture<T> createInitialStateAndExecuteAsync(K key, BucketConfiguration configuration, GridCommand<T> command) {
    JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.initStateAndExecuteProcessor(command, configuration);
    CompletableFuture<CommandResult<T>> result = invokeAsync(key, entryProcessor);
    return result.thenApply(CommandResult::getData);
}
 
Example #27
Source File: InfinispanProxy.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
public <T extends Serializable> T createInitialStateAndExecute(K key, BucketConfiguration configuration, GridCommand<T> command) {
    JCacheEntryProcessor<K, T> entryProcessor = JCacheEntryProcessor.initStateAndExecuteProcessor(command, configuration);
    CommandResult<T> result = invokeSync(key, entryProcessor);
    return result.getData();
}
 
Example #28
Source File: InfinispanProxy.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
public void createInitialState(K key, BucketConfiguration configuration) {
    JCacheEntryProcessor<K, Nothing> entryProcessor = JCacheEntryProcessor.initStateProcessor(configuration);
    invokeSync(key, entryProcessor);
}
 
Example #29
Source File: InfinispanProxyManager.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
public Optional<BucketConfiguration> getProxyConfiguration(K key) {
    return gridProxy.getConfiguration(key);
}
 
Example #30
Source File: InfinispanProxyManager.java    From bucket4j with Apache License 2.0 4 votes vote down vote up
@Override
public Bucket getProxy(K key, Supplier<BucketConfiguration> supplier) {
    return GridBucket.createLazyBucket(key, supplier, gridProxy);
}