From d40d8dc36a608a1349f8353c50c2c71649e6fa75 Mon Sep 17 00:00:00 2001 From: Hubert Hirtz Date: Tue, 16 Nov 2021 09:20:21 +0100 Subject: Allow App.Close() and App.Run() to be run concurrently --- irc/session.go | 1 + irc/typing.go | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 3 deletions(-) (limited to 'irc') diff --git a/irc/session.go b/irc/session.go index 084c6a7..07df816 100644 --- a/irc/session.go +++ b/irc/session.go @@ -170,6 +170,7 @@ func (s *Session) Close() { return } s.closed = true + s.typings.Close() close(s.out) } diff --git a/irc/typing.go b/irc/typing.go index fd1576c..4a8cf55 100644 --- a/irc/typing.go +++ b/irc/typing.go @@ -16,6 +16,7 @@ type Typing struct { // Typings keeps track of typing notification timeouts. type Typings struct { l sync.Mutex + closed bool // whether Close has been called targets map[Typing]time.Time // @+typing TAGMSG timestamps. timeouts chan Typing // transmits unfiltered timeout notifications. stops chan Typing // transmits filtered timeout notifications. @@ -45,10 +46,14 @@ func NewTypings() *Typings { return ts } -// Stop cleanly closes all channels and stops all coroutines. -func (ts *Typings) Stop() { +// Close cleanly closes all channels and stops all goroutines. +func (ts *Typings) Close() { + ts.l.Lock() + defer ts.l.Unlock() + close(ts.timeouts) close(ts.stops) + ts.closed = true } // Stops is a channel that transmits typing timeouts. @@ -65,7 +70,13 @@ func (ts *Typings) Active(target, name string) { go func() { time.Sleep(6 * time.Second) - ts.timeouts <- t + + ts.l.Lock() + defer ts.l.Unlock() + + if !ts.closed { + ts.timeouts <- t + } }() } -- cgit v1.2.3