From 2943f994e7d6d8154814db6344d9714a41806186 Mon Sep 17 00:00:00 2001 From: Alberto Bertogli Date: Sat, 13 Jul 2019 13:44:25 +0100 Subject: [PATCH] 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 | 3 +-- internal/queue/queue.go | 3 +-- internal/smtpsrv/conn.go | 3 +-- 3 files changed, 3 insertions(+), 6 deletions(-) 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)