[PERF] Avoid calling Session::getAttachment on each SMTP line
2% of the memory allocation was spent doing this.
diff --git a/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/esmtp/MailSizeEsmtpExtension.java b/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/esmtp/MailSizeEsmtpExtension.java
index a2f90e9..6e6774a 100644
--- a/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/esmtp/MailSizeEsmtpExtension.java
+++ b/protocols/smtp/src/main/java/org/apache/james/protocols/smtp/core/esmtp/MailSizeEsmtpExtension.java
@@ -50,7 +50,6 @@
private static final Logger LOGGER = LoggerFactory.getLogger(MailSizeEsmtpExtension.class);
private static final ProtocolSession.AttachmentKey<Integer> MESG_SIZE = ProtocolSession.AttachmentKey.of("MESG_SIZE", Integer.class); // The size of the
- private static final ProtocolSession.AttachmentKey<Boolean> MESG_FAILED = ProtocolSession.AttachmentKey.of("MESG_FAILED", Boolean.class); // Message failed flag
private static final String[] MAIL_PARAMS = { "SIZE" };
private static final HookResult SYNTAX_ERROR = HookResult.builder()
@@ -70,6 +69,7 @@
public HookResult doMailParameter(SMTPSession session, String paramName,
String paramValue) {
MaybeSender tempSender = session.getAttachment(SMTPSession.SENDER, State.Transaction).orElse(MaybeSender.nullSender());
+ session.setMessageFailed(false);
return doMailSize(session, paramValue, tempSender);
}
@@ -135,10 +135,10 @@
@Override
public Response onLine(SMTPSession session, byte[] line, LineHandler<SMTPSession> next) {
- Optional<Boolean> failed = session.getAttachment(MESG_FAILED, State.Transaction);
+ boolean failed = session.messageFailed();
// If we already defined we failed and sent a reply we should simply
// wait for a CRLF.CRLF to be sent by the client.
- if (failed.isPresent() && failed.get()) {
+ if (failed) {
if (isDataTerminated(line)) {
next.onLine(session, line);
return new SMTPResponse(SMTPRetCode.QUOTA_EXCEEDED, "Quota exceeded");
@@ -160,7 +160,7 @@
// logging of extra lines of data
// that are sent after the size limit has
// been hit.
- session.setAttachment(MESG_FAILED, Boolean.TRUE, State.Transaction);
+ session.setMessageFailed(true);
return null;
} else {
@@ -176,8 +176,8 @@
@Override
public HookResult onMessage(SMTPSession session, MailEnvelope mail) {
- Optional<Boolean> failed = session.getAttachment(MESG_FAILED, State.Transaction);
- if (failed.orElse(false)) {
+ boolean failed = session.messageFailed();
+ if (failed) {
LOGGER.info("Rejected message from {} from {} exceeding system maximum message size of {}",
session.getAttachment(SMTPSession.SENDER, State.Transaction).orElse(MaybeSender.nullSender()).asPrettyString(),
session.getRemoteAddress().getAddress().getHostAddress(), session.getConfiguration().getMaxMessageSize());