fix(imapx): honor context in Connect with bounded dial retry
This commit is contained in:
+26
-9
@@ -4,6 +4,7 @@ import (
|
|||||||
"context"
|
"context"
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/emersion/go-imap/v2/imapclient"
|
"github.com/emersion/go-imap/v2/imapclient"
|
||||||
)
|
)
|
||||||
@@ -16,29 +17,45 @@ type Endpoint struct {
|
|||||||
|
|
||||||
func (e Endpoint) addr() string { return fmt.Sprintf("%s:%d", e.Host, e.Port) }
|
func (e Endpoint) addr() string { return fmt.Sprintf("%s:%d", e.Host, e.Port) }
|
||||||
|
|
||||||
func Connect(ctx context.Context, ep Endpoint) (*imapclient.Client, error) {
|
func dialOnce(ep Endpoint) (*imapclient.Client, error) {
|
||||||
var (
|
|
||||||
c *imapclient.Client
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
switch ep.TLSMode {
|
switch ep.TLSMode {
|
||||||
case "ssl":
|
case "ssl":
|
||||||
c, err = imapclient.DialTLS(ep.addr(), &imapclient.Options{
|
return imapclient.DialTLS(ep.addr(), &imapclient.Options{
|
||||||
TLSConfig: &tls.Config{ServerName: ep.Host},
|
TLSConfig: &tls.Config{ServerName: ep.Host},
|
||||||
})
|
})
|
||||||
case "starttls":
|
case "starttls":
|
||||||
c, err = imapclient.DialStartTLS(ep.addr(), &imapclient.Options{
|
return imapclient.DialStartTLS(ep.addr(), &imapclient.Options{
|
||||||
TLSConfig: &tls.Config{ServerName: ep.Host},
|
TLSConfig: &tls.Config{ServerName: ep.Host},
|
||||||
})
|
})
|
||||||
case "plain":
|
case "plain":
|
||||||
c, err = imapclient.DialInsecure(ep.addr(), nil)
|
return imapclient.DialInsecure(ep.addr(), nil)
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("unknown tls_mode %q", ep.TLSMode)
|
return nil, fmt.Errorf("unknown tls_mode %q", ep.TLSMode)
|
||||||
}
|
}
|
||||||
if err != nil {
|
}
|
||||||
|
|
||||||
|
func Connect(ctx context.Context, ep Endpoint) (*imapclient.Client, error) {
|
||||||
|
const attempts = 3
|
||||||
|
var lastErr error
|
||||||
|
for i := 0; i < attempts; i++ {
|
||||||
|
if err := ctx.Err(); err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
c, err := dialOnce(ep)
|
||||||
|
if err == nil {
|
||||||
return c, nil
|
return c, nil
|
||||||
|
}
|
||||||
|
lastErr = err
|
||||||
|
if i < attempts-1 {
|
||||||
|
backoff := time.Duration(200*(i+1)) * time.Millisecond
|
||||||
|
select {
|
||||||
|
case <-ctx.Done():
|
||||||
|
return nil, ctx.Err()
|
||||||
|
case <-time.After(backoff):
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil, lastErr
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestEndpoint(ctx context.Context, ep Endpoint) error {
|
func TestEndpoint(ctx context.Context, ep Endpoint) error {
|
||||||
|
|||||||
@@ -40,3 +40,12 @@ func TestTestLoginListsFolders(t *testing.T) {
|
|||||||
t.Fatalf("INBOX not in folders: %v", folders)
|
t.Fatalf("INBOX not in folders: %v", folders)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestConnectHonorsCancelledContext(t *testing.T) {
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
cancel()
|
||||||
|
_, err := Connect(ctx, Endpoint{Host: "10.255.255.1", Port: 993, TLSMode: "ssl"})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error for cancelled context")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user