Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#116: Implement method to reset bucket to initial state
- Loading branch information
1 parent
ee27e5c
commit b2e7140
Showing
19 changed files
with
318 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
bucket4j-core/src/main/java/io/github/bucket4j/distributed/remote/commands/ResetCommand.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/*- | ||
* ========================LICENSE_START================================= | ||
* Bucket4j | ||
* %% | ||
* Copyright (C) 2015 - 2020 Vladimir Bukhtoyarov | ||
* %% | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* =========================LICENSE_END================================== | ||
*/ | ||
|
||
package io.github.bucket4j.distributed.remote.commands; | ||
|
||
import io.github.bucket4j.Nothing; | ||
import io.github.bucket4j.distributed.remote.CommandResult; | ||
import io.github.bucket4j.distributed.remote.MutableBucketEntry; | ||
import io.github.bucket4j.distributed.remote.RemoteBucketState; | ||
import io.github.bucket4j.distributed.remote.RemoteCommand; | ||
import io.github.bucket4j.distributed.serialization.DeserializationAdapter; | ||
import io.github.bucket4j.distributed.serialization.SerializationAdapter; | ||
import io.github.bucket4j.distributed.serialization.SerializationHandle; | ||
import io.github.bucket4j.distributed.versioning.Version; | ||
import io.github.bucket4j.distributed.versioning.Versions; | ||
import io.github.bucket4j.util.ComparableByContent; | ||
|
||
import java.io.IOException; | ||
|
||
import static io.github.bucket4j.distributed.versioning.Versions.v_7_0_0; | ||
|
||
|
||
public class ResetCommand implements RemoteCommand<Nothing>, ComparableByContent<ResetCommand> { | ||
|
||
public static final SerializationHandle<ResetCommand> SERIALIZATION_HANDLE = new SerializationHandle<ResetCommand>() { | ||
@Override | ||
public <S> ResetCommand deserialize(DeserializationAdapter<S> adapter, S input, Version backwardCompatibilityVersion) throws IOException { | ||
int formatNumber = adapter.readInt(input); | ||
Versions.check(formatNumber, v_7_0_0, v_7_0_0); | ||
return new ResetCommand(); | ||
} | ||
|
||
@Override | ||
public <O> void serialize(SerializationAdapter<O> adapter, O output, ResetCommand command, Version backwardCompatibilityVersion) throws IOException { | ||
adapter.writeInt(output, v_7_0_0.getNumber()); | ||
} | ||
|
||
@Override | ||
public int getTypeId() { | ||
return 39; | ||
} | ||
|
||
@Override | ||
public Class<ResetCommand> getSerializedType() { | ||
return ResetCommand.class; | ||
} | ||
|
||
}; | ||
|
||
public ResetCommand() { | ||
|
||
} | ||
|
||
@Override | ||
public CommandResult<Nothing> execute(MutableBucketEntry mutableEntry, long currentTimeNanos) { | ||
if (!mutableEntry.exists()) { | ||
return CommandResult.bucketNotFound(); | ||
} | ||
|
||
RemoteBucketState state = mutableEntry.get(); | ||
state.refillAllBandwidth(currentTimeNanos); | ||
state.reset(); | ||
mutableEntry.set(state); | ||
return CommandResult.empty(); | ||
} | ||
|
||
@Override | ||
public SerializationHandle getSerializationHandle() { | ||
return SERIALIZATION_HANDLE; | ||
} | ||
|
||
@Override | ||
public boolean equalsByContent(ResetCommand other) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean isImmediateSyncRequired(long unsynchronizedTokens, long nanosSinceLastSync) { | ||
return true; | ||
} | ||
|
||
@Override | ||
public long estimateTokensToConsume() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public long getConsumedTokens(Nothing result) { | ||
return 0; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.