Supercharge Go Dialer
with DialContext
, providing support for context.Context
in libraries with only Dial
or DialTimeout
methods. Extracted from and used in production at domainr.com.
go get github.com/nbio/contextual
// Example context
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
// SSH into a proxy host
client, err := ssh.Dial("tcp", "yourserver.com:22", sshConfig)
if err != nil {
return err
}
// Wrap the ssh.Client in contextual.Dialer to get DialContext
conn, err := contextual.Dialer{client}.DialContext(ctx, "tcp", "otherserver.com:12345")
if err != nil {
return err // Could be context.DeadlineExceeded or context.Canceled
}
// Do stuff
go, golang, context
© 2017 nb.io, LLC