provide direct quit reason support (closes #16)

This commit is contained in:
Liam Stanley 2019-07-29 20:44:50 -04:00
parent 8adc1b3054
commit 05b8be7a4b
2 changed files with 26 additions and 10 deletions

View File

@ -351,6 +351,18 @@ func (c *Client) Close() {
c.mu.RUnlock()
}
// Quit sends a QUIT message to the server with a given reason to close the
// connection. Underlying this event being sent, Client.Close() is called as well.
// This is different than just calling Client.Close() in that it provides a reason
// as to why the connection was closed (for bots to tell users the bot is restarting,
// or shutting down, etc).
//
// NOTE: servers may delay showing of QUIT reasons, until you've been connected to
// the server for a certain period of time (e.g. 5 minutes). Keep this in mind.
func (c *Client) Quit(reason string) {
c.Send(&Event{Command: QUIT, Params: []string{reason}})
}
// ErrEvent is an error returned when the server (or library) sends an ERROR
// message response. The string returned contains the trailing text from the
// message.

24
conn.go
View File

@ -455,22 +455,20 @@ func (c *Client) Send(event *Event) {
}
<-time.After(delay)
// Relock client again as there may be an extended delay.
c.mu.RLock()
if c.conn == nil {
// Drop the event if disconnected.
c.debugLogEvent(event, true)
c.mu.RUnlock()
return
}
c.write(event)
c.mu.RUnlock()
}
// write is the lower level function to write an event. It does not have a
// write-delay when sending events.
func (c *Client) write(event *Event) {
c.mu.RLock()
defer c.mu.RUnlock()
if c.conn == nil {
// Drop the event if disconnected.
c.debugLogEvent(event, true)
return
}
c.tx <- event
}
@ -538,6 +536,12 @@ func (c *Client) sendLoop(ctx context.Context, errs chan error, wg *sync.WaitGro
}
}
if event.Command == QUIT {
c.Close()
wg.Done()
return
}
if err != nil {
errs <- err
wg.Done()