summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-02-24 19:55:53 +0700
committerShulhan <ms@kilabit.info>2022-02-25 22:20:40 +0700
commit2f9ffc6a8973a150773c0919a0477cf0f82a48e4 (patch)
tree1aa527855067eeb198fe55b3bbf9ae030cd0d60d
parent3008d44e258b26a94247d03fe4ae9eac297f9c6f (diff)
downloadpakakeh.go-2f9ffc6a8973a150773c0919a0477cf0f82a48e4.tar.xz
lib/email: change the Header and Body fields on Message to non-pointer
The idea is to minimize GC pressure on system with many messages, minimize checking for nil value, and make an empty Message ready to use without any initialization.
-rw-r--r--lib/email/message.go42
-rw-r--r--lib/email/message_test.go2
2 files changed, 26 insertions, 18 deletions
diff --git a/lib/email/message.go b/lib/email/message.go
index eafdc144..c03672b5 100644
--- a/lib/email/message.go
+++ b/lib/email/message.go
@@ -20,10 +20,11 @@ import (
// Message represent an unpacked internet message format.
//
type Message struct {
- Header *Header
- Body *Body
DKIMSignature *dkim.Signature
dkimStatus *dkim.Status
+
+ Header Header
+ Body Body
}
//
@@ -38,10 +39,7 @@ func NewMultipart(from, to, subject, bodyText, bodyHTML []byte) (
dateValue = timeNow.Format(DateFormat)
)
- msg = &Message{
- Header: &Header{},
- Body: &Body{},
- }
+ msg = &Message{}
err = msg.Header.Set(FieldTypeDate, []byte(dateValue))
if err != nil {
@@ -106,18 +104,32 @@ func ParseMessage(raw []byte) (msg *Message, rest []byte, err error) {
return nil, nil, nil
}
+ var (
+ logp = "ParseMessage"
+
+ hdr *Header
+ body *Body
+ boundary []byte
+ )
+
msg = &Message{}
- msg.Header, rest, err = ParseHeader(raw)
+ hdr, rest, err = ParseHeader(raw)
if err != nil {
- return nil, rest, err
+ return nil, rest, fmt.Errorf("%s: %w", logp, err)
}
- boundary := msg.Header.Boundary()
+ boundary = hdr.Boundary()
+
+ body, rest, err = ParseBody(rest, boundary)
+ if err != nil {
+ return nil, rest, fmt.Errorf("%s: %w", logp, err)
+ }
- msg.Body, rest, err = ParseBody(rest, boundary)
+ msg.Header = *hdr
+ msg.Body = *body
- return msg, rest, err
+ return msg, rest, nil
}
//
@@ -280,14 +292,10 @@ func (msg *Message) DKIMVerify() (*dkim.Status, error) {
func (msg *Message) String() string {
var sb strings.Builder
- if msg.Header != nil {
- sb.Write(msg.Header.Relaxed())
- }
+ sb.Write(msg.Header.Relaxed())
sb.WriteByte(cr)
sb.WriteByte(lf)
- if msg.Body != nil {
- sb.WriteString(msg.Body.String())
- }
+ sb.WriteString(msg.Body.String())
return sb.String()
}
diff --git a/lib/email/message_test.go b/lib/email/message_test.go
index 26ad9f81..a3d716e5 100644
--- a/lib/email/message_test.go
+++ b/lib/email/message_test.go
@@ -53,7 +53,7 @@ func TestMessageParseMessage(t *testing.T) {
exp: "\r\n",
}, {
in: "testdata/invalid-header.txt",
- expErr: "email: invalid field value at 'From : John Doe <jdoe@machine(comment). example>'",
+ expErr: "ParseMessage: email: invalid field value at 'From : John Doe <jdoe@machine(comment). example>'",
}, {
in: "testdata/rfc5322-A.6.3.txt",
exp: "from:John Doe <jdoe@machine(comment). example>\r\n" +