1
0
Fork 0
mirror of https://github.com/fluffle/goirc synced 2025-05-12 10:41:42 +00:00

Support context during connect

Signed-off-by: Luca Bigliardi <shammash@google.com>
This commit is contained in:
Luca Bigliardi 2021-02-20 02:54:09 +01:00 committed by Alex Bee
parent a32ccd5931
commit e0c319f8ff
3 changed files with 33 additions and 10 deletions

View file

@ -2,7 +2,9 @@ package client
import (
"bufio"
"context"
"crypto/tls"
"errors"
"fmt"
"io"
"net"
@ -36,7 +38,7 @@ type Conn struct {
// I/O stuff to server
dialer *net.Dialer
proxyDialer proxy.Dialer
proxyDialer proxy.ContextDialer
sock net.Conn
io *bufio.ReadWriter
in chan *Line
@ -304,11 +306,16 @@ func (conn *Conn) initialise() {
// Config.Server to host, Config.Pass to pass if one is provided, and then
// calls Connect.
func (conn *Conn) ConnectTo(host string, pass ...string) error {
return conn.ConnectToContext(context.Background(), host, pass...)
}
// ConnectToContext works like ConnectTo but uses the provided context.
func (conn *Conn) ConnectToContext(ctx context.Context, host string, pass ...string) error {
conn.cfg.Server = host
if len(pass) > 0 {
conn.cfg.Pass = pass[0]
}
return conn.Connect()
return conn.ConnectContext(ctx)
}
// Connect connects the IRC client to the server configured in Config.Server.
@ -323,10 +330,15 @@ func (conn *Conn) ConnectTo(host string, pass ...string) error {
// handler for the CONNECTED event is used to perform any initial client work
// like joining channels and sending messages.
func (conn *Conn) Connect() error {
return conn.ConnectContext(context.Background())
}
// ConnectContext works like Connect but uses the provided context.
func (conn *Conn) ConnectContext(ctx context.Context) error {
// We don't want to hold conn.mu while firing the REGISTER event,
// and it's much easier and less error prone to defer the unlock,
// so the connect mechanics have been delegated to internalConnect.
err := conn.internalConnect()
err := conn.internalConnect(ctx)
if err == nil {
conn.dispatch(&Line{Cmd: REGISTER, Time: time.Now()})
}
@ -334,7 +346,7 @@ func (conn *Conn) Connect() error {
}
// internalConnect handles the work of actually connecting to the server.
func (conn *Conn) internalConnect() error {
func (conn *Conn) internalConnect(ctx context.Context) error {
conn.mu.Lock()
defer conn.mu.Unlock()
conn.initialise()
@ -359,20 +371,24 @@ func (conn *Conn) internalConnect() error {
if err != nil {
return err
}
conn.proxyDialer, err = proxy.FromURL(proxyURL, conn.dialer)
proxyDialer, err := proxy.FromURL(proxyURL, conn.dialer)
if err != nil {
return err
}
contextProxyDialer, ok := proxyDialer.(proxy.ContextDialer)
if !ok {
return errors.New("Dialer for proxy does not support context")
}
conn.proxyDialer = contextProxyDialer
logging.Info("irc.Connect(): Connecting to %s.", conn.cfg.Server)
if s, err := conn.proxyDialer.Dial("tcp", conn.cfg.Server); err == nil {
if s, err := conn.proxyDialer.DialContext(ctx, "tcp", conn.cfg.Server); err == nil {
conn.sock = s
} else {
return err
}
} else {
logging.Info("irc.Connect(): Connecting to %s.", conn.cfg.Server)
if s, err := conn.dialer.Dial("tcp", conn.cfg.Server); err == nil {
if s, err := conn.dialer.DialContext(ctx, "tcp", conn.cfg.Server); err == nil {
conn.sock = s
} else {
return err