diff --git a/README.md b/README.md index 3cebbb9..f2d755b 100644 --- a/README.md +++ b/README.md @@ -22,6 +22,9 @@ functions and other additions will most likely not result in a major version increase unless they break the API. This library aims to follow the semver recommendations mentioned on gopkg.in. +Due to complications in how to support x/net/context vs the built-in context +package, only go 1.7+ is officially supported. + ## Import Paths All development happens on the `master` branch and when features are diff --git a/client.go b/client.go index 5c137a8..a8a835d 100644 --- a/client.go +++ b/client.go @@ -1,6 +1,7 @@ package irc import ( + "context" "errors" "fmt" "io" @@ -357,6 +358,12 @@ func (c *Client) startReadLoop(wg *sync.WaitGroup) { // strange and unexpected ways if it is called again before the first connection // exits. func (c *Client) Run() error { + return c.RunContext(context.TODO()) +} + +// RunContext is the same as Run but a context.Context can be passed in for +// cancelation. +func (c *Client) RunContext(ctx context.Context) error { // exiting is used by the main goroutine here to ensure any sub-goroutines // get closed when exiting. exiting := make(chan struct{}) @@ -382,9 +389,14 @@ func (c *Client) Run() error { // messages. c.startReadLoop(&wg) - // Wait for an error from any goroutine, then signal we're exiting and wait - // for the goroutines to exit. - err := <-c.errChan + // Wait for an error from any goroutine or for the context to time out, then + // signal we're exiting and wait for the goroutines to exit. + var err error + select { + case err = <-c.errChan: + case <-ctx.Done(): + } + close(exiting) wg.Wait()