1414import org .threadly .litesockets .buffers .ReuseableMergedByteBuffers ;
1515import org .threadly .litesockets .utils .IOUtils ;
1616import org .threadly .litesockets .utils .SimpleByteStats ;
17- import org .threadly .util .ArgumentVerifier ;
1817import org .threadly .util .Clock ;
1918import org .threadly .util .ExceptionUtils ;
2019
@@ -40,9 +39,9 @@ public abstract class Client implements Closeable {
4039 protected final SocketExecuterCommonBase se ;
4140 protected final long startTime = Clock .lastKnownForwardProgressingMillis ();
4241 protected final Object readerLock = new Object ();
43- protected final ClientByteStats stats = new ClientByteStats ();
4442 protected final AtomicBoolean closed = new AtomicBoolean (false );
4543 protected final ConcurrentLinkedQueue <ClientCloseListener > closerListener = new ConcurrentLinkedQueue <>();
44+ protected volatile ClientByteStats stats ;
4645 protected volatile Runnable readerCaller = null ;
4746 protected volatile boolean useNativeBuffers = false ;
4847 protected volatile boolean keepReadBuffer = true ;
@@ -51,12 +50,17 @@ public abstract class Client implements Closeable {
5150 protected volatile int newReadBufferSize = IOUtils .DEFAULT_CLIENT_READ_BUFFER_SIZE ;
5251 private ByteBuffer readByteBuffer = IOUtils .EMPTY_BYTEBUFFER ;
5352
54- public Client (final SocketExecuterCommonBase se ) {
53+ public Client (final SocketExecuterCommonBase se , final boolean statsEnabled ) {
54+ setStatsEnabled (statsEnabled );
55+
5556 this .se = se ;
5657 this .clientExecutor = se .getExecutorFor (this );
5758 }
5859
59- protected Client (final SocketExecuterCommonBase se , final SubmitterExecutor lclientExecutor ) {
60+ protected Client (final SocketExecuterCommonBase se , final SubmitterExecutor lclientExecutor ,
61+ final boolean statsEnabled ) {
62+ setStatsEnabled (statsEnabled );
63+
6064 this .se = se ;
6165 this .clientExecutor = lclientExecutor ;
6266 }
@@ -120,14 +124,12 @@ protected <T> SettableListenableFuture<T> makeClientSettableListenableFuture() {
120124 */
121125 public abstract SocketAddress getRemoteSocketAddress ();
122126
123-
124127 /**
125128 *
126129 * @return the local {@link SocketAddress} this client is using.
127130 */
128131 public abstract SocketAddress getLocalSocketAddress ();
129132
130-
131133 /**
132134 * Returns true if this client has data pending in its write buffers. False if there is no data pending write.
133135 *
@@ -232,11 +234,11 @@ public void close() {
232234
233235 /*Implemented functions*/
234236
235- protected void addReadStats (final int size ) {
237+ protected void recordReadStats (final int size ) {
236238 stats .addRead (size );
237239 }
238240
239- protected void addWriteStats (final int size ) {
241+ protected void recordWriteStats (final int size ) {
240242 stats .addWrite (size );
241243 }
242244
@@ -311,8 +313,8 @@ protected void addReadBuffer(final ByteBuffer bb) {
311313 if (! bb .hasRemaining ()) {
312314 return ;
313315 }
314- addReadStats (bb .remaining ());
315- se .addReadAmount (bb .remaining ());
316+ recordReadStats (bb .remaining ());
317+ se .recordReadStats (bb .remaining ());
316318 int start ;
317319 // synchronize to ensure readBuffers are not modified by non-client thread getRead call
318320 synchronized (readerLock ) {
@@ -441,24 +443,49 @@ protected boolean setClose() {
441443 public SimpleByteStats getStats () {
442444 return stats ;
443445 }
446+
447+ /**
448+ * Can be invoked with {@code false} to disable stat collection on client. This can reduce
449+ * minor memory and performance overheads.
450+ *
451+ * @param enabled {@code false} to disable stat collection, or {@code true} to enable / reset stats
452+ */
453+ public void setStatsEnabled (boolean enabled ) {
454+ if (enabled ) {
455+ ClientByteStats stats = this .stats ;
456+ if (stats == ClientByteStats .NO_OP_STATS || stats == null ) {
457+ this .stats = new ClientByteStats ();
458+ } else {
459+ stats .resetStats ();
460+ }
461+ } else {
462+ stats = ClientByteStats .NO_OP_STATS ;
463+ }
464+ }
444465
445466 /**
446467 * Implementation of the SimpleByteStats.
447468 */
448469 private static class ClientByteStats extends SimpleByteStats {
449- public ClientByteStats () {
450- super ();
451- }
470+ public static final ClientByteStats NO_OP_STATS = new ClientByteStats () {
471+ @ Override
472+ protected void addWrite (final int size ) {
473+ // ignored
474+ }
475+
476+ @ Override
477+ protected void addRead (final int size ) {
478+ // ignored
479+ }
480+ };
452481
453482 @ Override
454483 protected void addWrite (final int size ) {
455- ArgumentVerifier .assertNotNegative (size , "size" );
456484 super .addWrite (size );
457485 }
458486
459487 @ Override
460488 protected void addRead (final int size ) {
461- ArgumentVerifier .assertNotNegative (size , "size" );
462489 super .addRead (size );
463490 }
464491 }
@@ -491,7 +518,6 @@ public interface Reader {
491518 public void onRead (Client client );
492519 }
493520
494-
495521 /**
496522 * Used to notify when a Client is closed.
497523 *
0 commit comments