8000 fix wrapping when stream id is 0 · FTTF-git/rsocket-java@f29ff85 · GitHub
[go: up one dir, main page]

Skip to content

Commit f29ff85

Browse files
committed
fix wrapping when stream id is 0
Signed-off-by: Robert Roeser <rroeserr@gmail.com>
1 parent e9f2efe commit f29ff85

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package io.rsocket;
2+
3+
import io.netty.util.collection.IntObjectMap;
4+
import io.rsocket.internal.SynchronizedIntObjectHashMap;
5+
import org.openjdk.jmh.annotations.*;
6+
import org.openjdk.jmh.infra.Blackhole;
7+
8+
@BenchmarkMode(Mode.Throughput)
9+
@Fork(
10+
value = 1 // , jvmArgsAppend = {"-Dio.netty.leakDetection.level=advanced"}
11+
)
12+
@Warmup(iterations = 10)
13+
@Measurement(iterations = 10)
14+
@State(Scope.Thread)
15+
public class StreamIdSupplierPerf {
16+
@Benchmark
17+
public void benchmarkStreamId(Input input) {
18+
int i = input.supplier.nextStreamId(input.map);
19+
input.bh.consume(i);
20+
}
21+
22+
@State(Scope.Benchmark)
23+
public static class Input {
24+
Blackhole bh;
25+
IntObjectMap map;
26+
StreamIdSupplier supplier;
27+
28+
@Setup
29+
public void setup(Blackhole bh) {
30+
this.supplier = StreamIdSupplier.clientSupplier();
31+
this.bh = bh;
32+
this.map = new SynchronizedIntObjectHashMap();
33+
}
34+
}
35+
}

rsocket-core/src/main/java/io/rsocket/StreamIdSupplier.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
1716
package io.rsocket;
1817

1918
import io.netty.util.collection.IntObjectMap;
@@ -43,7 +42,7 @@ int nextStreamId(IntObjectMap<?> streamIds) {
4342
int streamId;
4443
do {
4544
streamId = (int) STREAM_ID.addAndGet(this, 2) & MASK;
46-
} while (streamIds.containsKey(streamId));
45+
} while (streamId == 0 || streamIds.containsKey(streamId));
4746
return streamId;
4847
}
4948

rsocket-core/src/test/java/io/rsocket/StreamIdSupplierTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,8 +95,8 @@ public void testWrap() {
9595
StreamIdSupplier s = new StreamIdSupplier(Integer.MAX_VALUE - 3);
9696

9797
assertEquals(2147483646, s.nextStreamId(map));
98-
assertEquals(0, s.nextStreamId(map));
9998
assertEquals(2, s.nextStreamId(map));
99+
assertEquals(4, s.nextStreamId(map));
100100

101101
s = new StreamIdSupplier(Integer.MAX_VALUE - 2);
102102

0 commit comments

Comments
 (0)
0