From 013a2ef2659d1296457bc6b12c49a9480aa3551e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20=27Necoro=27=20Neumann?= Date: Sun, 28 Feb 2021 13:37:32 +0100 Subject: IMAP: Set a timeout for connection --- internal/imap/imap.go | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/internal/imap/imap.go b/internal/imap/imap.go index 20713a4..51687ff 100644 --- a/internal/imap/imap.go +++ b/internal/imap/imap.go @@ -2,7 +2,9 @@ package imap import ( "fmt" + "net" "strings" + "time" imapClient "github.com/emersion/go-imap/client" @@ -10,6 +12,12 @@ import ( "github.com/Necoro/feed2imap-go/pkg/log" ) +var dialer imapClient.Dialer + +func init() { + dialer = &net.Dialer{Timeout: 30 * time.Second} +} + func newImapClient(url config.Url) (*imapClient.Client, error) { var ( c *imapClient.Client @@ -17,12 +25,12 @@ func newImapClient(url config.Url) (*imapClient.Client, error) { ) if url.ForceTLS() { - if c, err = imapClient.DialTLS(url.HostPort(), nil); err != nil { + if c, err = imapClient.DialWithDialerTLS(dialer, url.HostPort(), nil); err != nil { return nil, fmt.Errorf("connecting (TLS) to %s: %w", url.Host, err) } log.Print("Connected to ", url.HostPort(), " (TLS)") } else { - if c, err = imapClient.Dial(url.HostPort()); err != nil { + if c, err = imapClient.DialWithDialer(dialer, url.HostPort()); err != nil { return nil, fmt.Errorf("connecting to %s: %w", url.Host, err) } } -- cgit v1.2.3