provide direct quit reason support (closes #16)
This commit is contained in:
parent
8adc1b3054
commit
05b8be7a4b
12
client.go
12
client.go
@ -351,6 +351,18 @@ func (c *Client) Close() {
|
|||||||
c.mu.RUnlock()
|
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
|
// 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 response. The string returned contains the trailing text from the
|
||||||
// message.
|
// message.
|
||||||
|
24
conn.go
24
conn.go
@ -455,22 +455,20 @@ func (c *Client) Send(event *Event) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
<-time.After(delay)
|
<-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.write(event)
|
||||||
c.mu.RUnlock()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// write is the lower level function to write an event. It does not have a
|
// write is the lower level function to write an event. It does not have a
|
||||||
// write-delay when sending events.
|
// write-delay when sending events.
|
||||||
func (c *Client) write(event *Event) {
|
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
|
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 {
|
if err != nil {
|
||||||
errs <- err
|
errs <- err
|
||||||
wg.Done()
|
wg.Done()
|
||||||
|
Loading…
Reference in New Issue
Block a user