forked from redis/lettuce
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Custom policies to discard bytes read from CommandHandler#buffer redi…
- Loading branch information
Showing
10 changed files
with
309 additions
and
32 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
48 changes: 48 additions & 0 deletions
48
src/main/java/io/lettuce/core/protocol/RatioReadBytesDiscardPolicy.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,48 @@ | ||
/* | ||
* Copyright 2011-2020 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
package io.lettuce.core.protocol; | ||
|
||
import io.lettuce.core.internal.LettuceAssert; | ||
import io.netty.buffer.ByteBuf; | ||
|
||
/** | ||
* A {@link ReadBytesDiscardPolicy} that tries to discard read bytes when buffer usage reaches a higher ratio | ||
*/ | ||
public class RatioReadBytesDiscardPolicy implements ReadBytesDiscardPolicy { | ||
private final int bufferUsageRatio; | ||
private final float discardReadBytesRatio; | ||
|
||
public RatioReadBytesDiscardPolicy(int bufferUsageRatio) { | ||
LettuceAssert.isTrue(bufferUsageRatio > 0 && bufferUsageRatio < Integer.MAX_VALUE, | ||
"BufferUsageRatio must be greater than 0"); | ||
|
||
this.bufferUsageRatio = bufferUsageRatio; | ||
this.discardReadBytesRatio = (float)bufferUsageRatio / (bufferUsageRatio + 1); | ||
} | ||
|
||
public int getBufferUsageRatio() { | ||
return bufferUsageRatio; | ||
} | ||
|
||
@Override | ||
public void discardReadBytesIfNecessary(ByteBuf buffer) { | ||
float usedRatio = (float) buffer.readerIndex() / buffer.capacity(); | ||
|
||
if (usedRatio >= discardReadBytesRatio && buffer.refCnt() != 0) { | ||
buffer.discardReadBytes(); | ||
} | ||
} | ||
} |
31 changes: 31 additions & 0 deletions
31
src/main/java/io/lettuce/core/protocol/ReadBytesDiscardPolicy.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,31 @@ | ||
/* | ||
* Copyright 2011-2020 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
package io.lettuce.core.protocol; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
import io.netty.channel.ChannelHandlerContext; | ||
|
||
/** | ||
* Defines the approach to discard bytes read from {@link ByteBuf} | ||
* in {@link CommandHandler#decode(ChannelHandlerContext ctx, ByteBuf buffer)} | ||
*/ | ||
public interface ReadBytesDiscardPolicy { | ||
/** | ||
* Calls {@link ByteBuf#discardReadBytes()} if appropriate according to the current discard policy | ||
* @param buffer {@link CommandHandler#buffer} | ||
*/ | ||
void discardReadBytesIfNecessary(ByteBuf buffer); | ||
} |
36 changes: 36 additions & 0 deletions
36
src/main/java/io/lettuce/core/protocol/SimpleReadBytesDiscardPolicy.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,36 @@ | ||
/* | ||
* Copyright 2011-2020 the original author or authors. | ||
* | ||
* 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 | ||
* | ||
* https://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. | ||
*/ | ||
package io.lettuce.core.protocol; | ||
|
||
import io.netty.buffer.ByteBuf; | ||
|
||
/** | ||
* A {@link ReadBytesDiscardPolicy} that discards bytes read from a buffer on the sole condition | ||
* that the buffer has not been deallocated yet. | ||
*/ | ||
public class SimpleReadBytesDiscardPolicy implements ReadBytesDiscardPolicy { | ||
public static final SimpleReadBytesDiscardPolicy INSTANCE = new SimpleReadBytesDiscardPolicy(); | ||
|
||
private SimpleReadBytesDiscardPolicy() { | ||
} | ||
|
||
@Override | ||
public void discardReadBytesIfNecessary(ByteBuf buffer) { | ||
if (buffer.refCnt() != 0) { | ||
buffer.discardReadBytes(); | ||
} | ||
} | ||
} |
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.