Skip to content

Commit b58e9bc

Browse files
authored
Merge pull request #39 from systemli/connection-closing
♻️ Ensure connections are closed properly in handlers
2 parents d10b4b0 + 9216abf commit b58e9bc

File tree

2 files changed

+21
-10
lines changed

2 files changed

+21
-10
lines changed

adapter.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,11 @@ func NewPostfixAdapter(client UserliService) *PostfixAdapter {
5353
// It fetches the destinations for the given alias.
5454
// The response is a comma separated list of destinations.
5555
func (p *PostfixAdapter) AliasHandler(conn net.Conn) {
56+
defer func() {
57+
if err := conn.Close(); err != nil {
58+
log.WithError(err).Error("Error closing connection")
59+
}
60+
}()
5661
now := time.Now()
5762

5863
payload, err := p.payload(conn)
@@ -80,6 +85,11 @@ func (p *PostfixAdapter) AliasHandler(conn net.Conn) {
8085
// It checks if the domain exists.
8186
// The response is a single line with the status code.
8287
func (p *PostfixAdapter) DomainHandler(conn net.Conn) {
88+
defer func() {
89+
if err := conn.Close(); err != nil {
90+
log.WithError(err).Error("Error closing connection")
91+
}
92+
}()
8393
now := time.Now()
8494

8595
payload, err := p.payload(conn)
@@ -108,6 +118,11 @@ func (p *PostfixAdapter) DomainHandler(conn net.Conn) {
108118
// It checks if the mailbox exists.
109119
// The response is a single line with the status code.
110120
func (p *PostfixAdapter) MailboxHandler(conn net.Conn) {
121+
defer func() {
122+
if err := conn.Close(); err != nil {
123+
log.WithError(err).Error("Error closing connection")
124+
}
125+
}()
111126
now := time.Now()
112127

113128
payload, err := p.payload(conn)
@@ -136,6 +151,11 @@ func (p *PostfixAdapter) MailboxHandler(conn net.Conn) {
136151
// It fetches the senders for the given email.
137152
// The response is a comma separated list of senders.
138153
func (p *PostfixAdapter) SendersHandler(conn net.Conn) {
154+
defer func() {
155+
if err := conn.Close(); err != nil {
156+
log.WithError(err).Error("Error closing connection")
157+
}
158+
}()
139159
now := time.Now()
140160

141161
payload, err := p.payload(conn)

server.go

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,6 @@ func StartTCPServer(ctx context.Context, wg *sync.WaitGroup, addr string, handle
4040
continue
4141
}
4242

43-
go func() {
44-
defer func() {
45-
log.Debug("Closing connection")
46-
if err := conn.Close(); err != nil {
47-
log.WithError(err).Error("Error closing connection")
48-
}
49-
}()
50-
51-
handler(conn)
52-
}()
43+
go handler(conn)
5344
}
5445
}

0 commit comments

Comments
 (0)