You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@james.apache.org by bt...@apache.org on 2022/03/22 02:01:49 UTC

[james-project] 18/29: JAMES-3715 Avoid stateful static fields

This is an automated email from the ASF dual-hosted git repository.

btellier pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/james-project.git

commit 5618e72d24f13971bd3bbe7fd70162ab54ef317d
Author: Benoit Tellier <bt...@linagora.com>
AuthorDate: Wed Mar 16 13:03:15 2022 +0700

    JAMES-3715 Avoid stateful static fields
    
    ByteBuf are pooled and can be released. Rely on supplier instead.
---
 .../james/imapserver/netty/AbstractNettyImapRequestLineReader.java   | 5 +++--
 .../java/org/apache/james/imapserver/netty/ImapHeartbeatHandler.java | 5 +++--
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/AbstractNettyImapRequestLineReader.java b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/AbstractNettyImapRequestLineReader.java
index 9230b50..14be6d5 100644
--- a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/AbstractNettyImapRequestLineReader.java
+++ b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/AbstractNettyImapRequestLineReader.java
@@ -19,6 +19,7 @@
 package org.apache.james.imapserver.netty;
 
 import java.nio.charset.StandardCharsets;
+import java.util.function.Supplier;
 
 import org.apache.james.imap.decode.ImapRequestLineReader;
 
@@ -27,7 +28,7 @@ import io.netty.buffer.Unpooled;
 import io.netty.channel.Channel;
 
 public abstract class AbstractNettyImapRequestLineReader extends ImapRequestLineReader {
-    private static final ByteBuf CONTINUATION_REQUEST = Unpooled.wrappedUnmodifiableBuffer(Unpooled.wrappedBuffer("+ Ok\r\n".getBytes(StandardCharsets.US_ASCII)));
+    private static final Supplier<ByteBuf> CONTINUATION_REQUEST = () -> Unpooled.wrappedUnmodifiableBuffer(Unpooled.wrappedBuffer("+ Ok\r\n".getBytes(StandardCharsets.US_ASCII)));
 
     private final Channel channel;
     private final boolean retry;
@@ -44,7 +45,7 @@ public abstract class AbstractNettyImapRequestLineReader extends ImapRequestLine
         // request..
 
         if (!retry) {
-            channel.writeAndFlush(CONTINUATION_REQUEST);
+            channel.writeAndFlush(CONTINUATION_REQUEST.get());
         }
     }
 
diff --git a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapHeartbeatHandler.java b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapHeartbeatHandler.java
index 0bfa67f..21c3781 100644
--- a/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapHeartbeatHandler.java
+++ b/server/protocols/protocols-imap4/src/main/java/org/apache/james/imapserver/netty/ImapHeartbeatHandler.java
@@ -19,6 +19,7 @@
 package org.apache.james.imapserver.netty;
 
 import java.nio.charset.StandardCharsets;
+import java.util.function.Supplier;
 
 import io.netty.buffer.ByteBuf;
 import io.netty.buffer.Unpooled;
@@ -31,7 +32,7 @@ import io.netty.handler.timeout.IdleStateHandler;
 @ChannelHandler.Sharable
 public class ImapHeartbeatHandler extends IdleStateHandler {
 
-    private static final ByteBuf HEARTBEAT_BUFFER = Unpooled.wrappedUnmodifiableBuffer(Unpooled.wrappedBuffer("* OK Hang in there..\r\n".getBytes(StandardCharsets.US_ASCII)));
+    private static final Supplier<ByteBuf> HEARTBEAT_BUFFER = () -> Unpooled.wrappedUnmodifiableBuffer(Unpooled.wrappedBuffer("* OK Hang in there..\r\n".getBytes(StandardCharsets.US_ASCII)));
 
     ImapHeartbeatHandler(int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) {
         super(readerIdleTimeSeconds, writerIdleTimeSeconds, allIdleTimeSeconds);
@@ -40,7 +41,7 @@ public class ImapHeartbeatHandler extends IdleStateHandler {
     @Override
     public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) throws Exception {
         if (e.state().equals(IdleState.WRITER_IDLE)) {
-            ctx.channel().writeAndFlush(HEARTBEAT_BUFFER);
+            ctx.channel().writeAndFlush(HEARTBEAT_BUFFER.get());
         }
         super.channelIdle(ctx, e);
     }

---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@james.apache.org
For additional commands, e-mail: notifications-help@james.apache.org