BsonWriter.toBuffers存在并发问题

This commit is contained in:
Redkale
2018-08-27 12:07:45 +08:00
parent 2ca1e6305c
commit 29ce57d3af
4 changed files with 10 additions and 5 deletions

View File

@@ -35,8 +35,12 @@ public class BsonByteBufferWriter extends BsonWriter {
this.supplier = supplier; this.supplier = supplier;
} }
@Override
public ByteBuffer[] toBuffers() { public ByteBuffer[] toBuffers() {
return toBuffers(supplier);
}
@Override
public ByteBuffer[] toBuffers(Supplier<ByteBuffer> supplier) {
if (buffers == null) return new ByteBuffer[0]; if (buffers == null) return new ByteBuffer[0];
for (int i = index; i < this.buffers.length; i++) { for (int i = index; i < this.buffers.length; i++) {
ByteBuffer buf = this.buffers[i]; ByteBuffer buf = this.buffers[i];

View File

@@ -6,6 +6,7 @@
package org.redkale.convert.bson; package org.redkale.convert.bson;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.function.Supplier;
import org.redkale.convert.*; import org.redkale.convert.*;
import org.redkale.util.*; import org.redkale.util.*;
@@ -37,8 +38,8 @@ public class BsonWriter extends Writer {
return newdata; return newdata;
} }
public ByteBuffer[] toBuffers() { public ByteBuffer[] toBuffers(final Supplier<ByteBuffer> supplier) {
return new ByteBuffer[]{ByteBuffer.wrap(content, 0, count)}; return ByteBufferWriter.create(supplier).put(content, 0, count).toBuffers();
} }
protected BsonWriter(byte[] bs) { protected BsonWriter(byte[] bs) {

View File

@@ -367,7 +367,7 @@ public final class SncpClient {
return future; return future;
} }
final AsyncConnection conn = conn0; final AsyncConnection conn = conn0;
final ByteBuffer[] sendBuffers = writer.toBuffers(); final ByteBuffer[] sendBuffers = writer.toBuffers(transport.getBufferSupplier());
fillHeader(sendBuffers[0], seqid, actionid, reqBodyLength); fillHeader(sendBuffers[0], seqid, actionid, reqBodyLength);
final ByteBuffer buffer = transport.pollBuffer(); final ByteBuffer buffer = transport.pollBuffer();

View File

@@ -65,7 +65,7 @@ public final class SncpResponse extends Response<SncpContext, SncpRequest> {
return; return;
} }
final int respBodyLength = out.count(); //body总长度 final int respBodyLength = out.count(); //body总长度
final ByteBuffer[] buffers = out.toBuffers(); final ByteBuffer[] buffers = out.toBuffers(context.getBufferSupplier());
fillHeader(buffers[0], respBodyLength - HEADER_SIZE, retcode); fillHeader(buffers[0], respBodyLength - HEADER_SIZE, retcode);
finish(buffers); finish(buffers);
} }