summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorShulhan <ms@kilabit.info>2022-02-18 01:11:00 +0700
committerShulhan <ms@kilabit.info>2022-02-18 01:37:13 +0700
commitbda173f464e1f69e8a97c3193ace94e68678653e (patch)
tree21ebae7c23846bc27bf28867bf043d347fb59a2a
parent511c473760df3303dd67d21cb379bcdd10747656 (diff)
downloadpakakeh.go-bda173f464e1f69e8a97c3193ace94e68678653e.tar.xz
lib/smtp: realign all structs
The goal is to minimize memory consumed by struct instance. Changes, * Client: from 112 to 80 bytes (-32 bytes) * Command: from 48 to 32 bytes (-8 bytes) * Domain: from 32 to 24 bytes (-8 bytes) * LocalStorage: from 72 to 40 bytes (-32 bytes) * MailTx: from 128 to 112 bytes (-16 bytes) * receiver: from 152 to 104 bytes (-48 bytes) * Response: from 32 to 24 bytes (-8 bytes) * Server: from 144 to 128 bytes (-16 bytes) * ServerInfo: from 40 to 32 bytes (-8 bytes)
-rw-r--r--lib/smtp/client.go7
-rw-r--r--lib/smtp/client_test.go25
-rw-r--r--lib/smtp/command.go4
-rw-r--r--lib/smtp/command_test.go4
-rw-r--r--lib/smtp/domain.go5
-rw-r--r--lib/smtp/localstorage.go4
-rw-r--r--lib/smtp/localstorage_test.go4
-rw-r--r--lib/smtp/mail_tx.go13
-rw-r--r--lib/smtp/receiver.go14
-rw-r--r--lib/smtp/response.go2
-rw-r--r--lib/smtp/server.go40
-rw-r--r--lib/smtp/serverinfo.go2
12 files changed, 67 insertions, 57 deletions
diff --git a/lib/smtp/client.go b/lib/smtp/client.go
index 6620fcbd..791c3212 100644
--- a/lib/smtp/client.go
+++ b/lib/smtp/client.go
@@ -28,12 +28,13 @@ type Client struct {
// EHLO command.
ServerInfo *ServerInfo
+ conn net.Conn
+ raddr *net.TCPAddr
+ serverName string
+
data []byte
buf bytes.Buffer
- serverName string
- raddr *net.TCPAddr
- conn net.Conn
insecure bool
isTLS bool
isStartTLS bool
diff --git a/lib/smtp/client_test.go b/lib/smtp/client_test.go
index 33f49baa..e8df931e 100644
--- a/lib/smtp/client_test.go
+++ b/lib/smtp/client_test.go
@@ -13,10 +13,11 @@ import (
func TestEhlo(t *testing.T) {
cases := []struct {
- desc string
- arg string
exp *Response
expServerInfo *ServerInfo
+
+ desc string
+ arg string
}{{
desc: "With no argument",
exp: &Response{
@@ -55,12 +56,14 @@ func TestEhlo(t *testing.T) {
func TestAuth(t *testing.T) {
cases := []struct {
+ exp *Response
+
desc string
- mech Mechanism
username string
password string
expErr string
- exp *Response
+
+ mech Mechanism
}{{
desc: "With invalid mechanism",
username: testAccountFirst.Short(),
@@ -142,9 +145,9 @@ func TestAuth2(t *testing.T) {
func TestExpand(t *testing.T) {
cases := []struct {
+ exp *Response
desc string
mlist string
- exp *Response
}{{
desc: "With mailing-list",
mlist: "mailing-list@test",
@@ -168,9 +171,9 @@ func TestExpand(t *testing.T) {
func TestHelp(t *testing.T) {
cases := []struct {
+ exp *Response
desc string
arg string
- exp *Response
}{{
desc: "Without any argument",
exp: &Response{
@@ -193,9 +196,9 @@ func TestHelp(t *testing.T) {
func TestSendCommand(t *testing.T) {
cases := []struct {
+ exp *Response
desc string
cmd []byte
- exp *Response
}{{
desc: "Send HELO",
cmd: []byte("HELO 192.168.10.1\r\n"),
@@ -233,9 +236,10 @@ func TestSendCommand(t *testing.T) {
func TestMailTx(t *testing.T) {
cases := []struct {
+ mail *MailTx
+ exp *Response
+
desc string
- mail *MailTx
- exp *Response
expErr string
}{{
desc: "With empty mail",
@@ -278,9 +282,10 @@ func TestMailTx(t *testing.T) {
func TestVerify(t *testing.T) {
cases := []struct {
+ exp *Response
+
desc string
mailbox string
- exp *Response
}{{
desc: "With mailbox exist",
mailbox: testAccountFirst.Short(),
diff --git a/lib/smtp/command.go b/lib/smtp/command.go
index a21363bd..84457d51 100644
--- a/lib/smtp/command.go
+++ b/lib/smtp/command.go
@@ -33,10 +33,10 @@ const (
// parameters.
//
type Command struct {
- Kind CommandKind
+ Params map[string]string
Arg string
Param string
- Params map[string]string
+ Kind CommandKind
}
//
diff --git a/lib/smtp/command_test.go b/lib/smtp/command_test.go
index 950bd4cf..2c66cd51 100644
--- a/lib/smtp/command_test.go
+++ b/lib/smtp/command_test.go
@@ -13,10 +13,10 @@ import (
func TestUnpack(t *testing.T) {
cases := []struct {
+ expCmd *Command
+ expErr error
desc string
b string
- expErr error
- expCmd *Command
}{{
desc: "With invalid length",
b: "DAT\r\n",
diff --git a/lib/smtp/domain.go b/lib/smtp/domain.go
index 284e7471..cc5a20c7 100644
--- a/lib/smtp/domain.go
+++ b/lib/smtp/domain.go
@@ -9,9 +9,10 @@ package smtp
// DKIM feature.
//
type Domain struct {
- Name string
- Accounts map[string]*Account
dkimOpts *DKIMOptions
+
+ Accounts map[string]*Account
+ Name string
}
//
diff --git a/lib/smtp/localstorage.go b/lib/smtp/localstorage.go
index 002e066f..6fb4a7b0 100644
--- a/lib/smtp/localstorage.go
+++ b/lib/smtp/localstorage.go
@@ -23,10 +23,10 @@ const (
// retrieved in file system inside a directory.
//
type LocalStorage struct {
- dir string
- buff bytes.Buffer
enc *gob.Encoder
dec *gob.Decoder
+ dir string
+ buff bytes.Buffer
}
//
diff --git a/lib/smtp/localstorage_test.go b/lib/smtp/localstorage_test.go
index b65d6738..5fecfd1d 100644
--- a/lib/smtp/localstorage_test.go
+++ b/lib/smtp/localstorage_test.go
@@ -17,9 +17,9 @@ func TestStore(t *testing.T) {
}
cases := []struct {
- desc string
- mail *MailTx
expErr error
+ mail *MailTx
+ desc string
}{{
desc: "With nil",
}, {
diff --git a/lib/smtp/mail_tx.go b/lib/smtp/mail_tx.go
index d4bebb65..d7a40781 100644
--- a/lib/smtp/mail_tx.go
+++ b/lib/smtp/mail_tx.go
@@ -15,6 +15,12 @@ import (
// MailTx define a mail transaction.
//
type MailTx struct {
+ Postpone time.Time
+
+ // Received contains the time when the message arrived on server.
+ // This field is ignored in Client.Send().
+ Received time.Time
+
// ID of message.
// This field is ignored in Client.Send().
ID string
@@ -31,12 +37,7 @@ type MailTx struct {
// This field is optional in Client.Send().
Data []byte
- Postpone time.Time
-
- // Received contains the time when the message arrived on server.
- // This field is ignored in Client.Send().
- Received time.Time
- Retry int
+ Retry int
}
//
diff --git a/lib/smtp/receiver.go b/lib/smtp/receiver.go
index 044c9f74..eae934a4 100644
--- a/lib/smtp/receiver.go
+++ b/lib/smtp/receiver.go
@@ -31,17 +31,19 @@ const (
// receiver represent a connection that receive incoming email in server.
//
type receiver struct {
- mode receiverMode
+ conn net.Conn
+ mail *MailTx
- data []byte
- buff bytes.Buffer
- conn net.Conn
clientDomain string
clientAddress string
localAddress string
- state CommandKind
- mail *MailTx
+ data []byte
+ buff bytes.Buffer
+
+ mode receiverMode
+ state CommandKind
+
authenticated bool
}
diff --git a/lib/smtp/response.go b/lib/smtp/response.go
index 1bff05ee..68f6fb72 100644
--- a/lib/smtp/response.go
+++ b/lib/smtp/response.go
@@ -17,9 +17,9 @@ import (
// Response represent a generic single or multilines response from server.
//
type Response struct {
- Code int
Message string
Body []string
+ Code int
}
//
diff --git a/lib/smtp/server.go b/lib/smtp/server.go
index 5854bf61..89c25b37 100644
--- a/lib/smtp/server.go
+++ b/lib/smtp/server.go
@@ -25,30 +25,10 @@ const (
// Server defines parameters for running an SMTP server.
//
type Server struct {
- // address to listen for incoming connections.
- // This field is optional and exist only for the purpose of testing.
- // If its empty, it will set default to ":25".
- address string
-
- // tlsAddress define an address when listening with security layer.
- // This field is optional and exist only for the purpose of testing.
- // If its empty, it will set default to ":465".
- tlsAddress string
-
- // TLSCert the server certificate for TLS or nil if no certificate.
- // This field is optional, if its non nil, the server will also listen
- // on address defined in TLSAddress.
- TLSCert *tls.Certificate
-
// Env contains server environment.
Env *Environment
//
- // Exts define list of custom extensions that the server will provide.
- //
- Exts []Extension
-
- //
// Handler define an interface that will process the bouncing email,
// incoming email, EXPN command, and VRFY command.
// This field is optional, if not set, it will default to
@@ -73,6 +53,26 @@ type Server struct {
// relayQueue hold mail objects that need to be relayed to other MTA.
relayQueue chan *MailTx
+ // TLSCert the server certificate for TLS or nil if no certificate.
+ // This field is optional, if its non nil, the server will also listen
+ // on address defined in TLSAddress.
+ TLSCert *tls.Certificate
+
+ // address to listen for incoming connections.
+ // This field is optional and exist only for the purpose of testing.
+ // If its empty, it will set default to ":25".
+ address string
+
+ // tlsAddress define an address when listening with security layer.
+ // This field is optional and exist only for the purpose of testing.
+ // If its empty, it will set default to ":465".
+ tlsAddress string
+
+ //
+ // Exts define list of custom extensions that the server will provide.
+ //
+ Exts []Extension
+
wg sync.WaitGroup
running bool
}
diff --git a/lib/smtp/serverinfo.go b/lib/smtp/serverinfo.go
index 3c4dbe30..facaeef2 100644
--- a/lib/smtp/serverinfo.go
+++ b/lib/smtp/serverinfo.go
@@ -13,9 +13,9 @@ import (
// command.
//
type ServerInfo struct {
+ Exts map[string][]string
Domain string
Info string
- Exts map[string][]string
}
//