git » chasquid » commit 2943f99

Use context.WithTimeout instead of context.WithDeadline

author Alberto Bertogli
2019-07-13 12:44:25 UTC
committer Alberto Bertogli
2019-07-13 12:44:25 UTC
parent a92497aef0b9c0a5e4cd770bb5fcdedf712d2a7a

Use context.WithTimeout instead of context.WithDeadline

There are a few context.WithDeadline calls that can be simplified by
using context.WithTimeout.

At the time they were added, WithTimeout was too new so we didn't want
to depend on it. But now that the minimum Go version has been raised to
1.9, we can simplify the calls.

This patch does that simplification, which is purely mechanical, and
does not change the logic itself.

internal/courier/procmail.go +1 -2
internal/queue/queue.go +1 -2
internal/smtpsrv/conn.go +1 -2

diff --git a/internal/courier/procmail.go b/internal/courier/procmail.go
index 9167a84..b357e07 100644
--- a/internal/courier/procmail.go
+++ b/internal/courier/procmail.go
@@ -58,8 +58,7 @@ func (p *Procmail) Deliver(from string, to string, data []byte) (error, bool) {
 	}
 	tr.Debugf("%s %q", p.Binary, args)
 
-	ctx, cancel := context.WithDeadline(context.Background(),
-		time.Now().Add(p.Timeout))
+	ctx, cancel := context.WithTimeout(context.Background(), p.Timeout)
 	defer cancel()
 	cmd := exec.CommandContext(ctx, p.Binary, args...)
 	cmd.Stdin = bytes.NewReader(data)
diff --git a/internal/queue/queue.go b/internal/queue/queue.go
index ec5818f..802a494 100644
--- a/internal/queue/queue.go
+++ b/internal/queue/queue.go
@@ -381,8 +381,7 @@ func (item *Item) deliver(q *Queue, rcpt *Recipient) (err error, permanent bool)
 		if len(c) == 0 {
 			return fmt.Errorf("empty pipe"), true
 		}
-		ctx, cancel := context.WithDeadline(context.Background(),
-			time.Now().Add(30*time.Second))
+		ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
 		defer cancel()
 		cmd := exec.CommandContext(ctx, c[0], c[1:]...)
 		cmd.Stdin = bytes.NewReader(item.Data)
diff --git a/internal/smtpsrv/conn.go b/internal/smtpsrv/conn.go
index d3fe30a..29ea5a2 100644
--- a/internal/smtpsrv/conn.go
+++ b/internal/smtpsrv/conn.go
@@ -743,8 +743,7 @@ func (c *Conn) runPostDataHook(data []byte) ([]byte, bool, error) {
 	defer tr.Finish()
 	tr.Debugf("running")
 
-	ctx, cancel := context.WithDeadline(context.Background(),
-		time.Now().Add(1*time.Minute))
+	ctx, cancel := context.WithTimeout(context.Background(), 1*time.Minute)
 	defer cancel()
 	cmd := exec.CommandContext(ctx, c.postDataHook)
 	cmd.Stdin = bytes.NewReader(data)