From 7835d39dda5f7a1dde3056de3dd8a84913b94c14 Mon Sep 17 00:00:00 2001 From: Hubert Hirtz Date: Tue, 18 May 2021 21:26:03 +0200 Subject: Fix races conditions Refactor: - Split out reads/writes from irc.Session to irc.ChanInOut, - Message handling is now manual, messages must be passed to irc.Session.HandleMessage for its state to change, - Remove data-race-prone App.addLineNow (called from both the main eventLoop and irc loops) and add App.addStatusLine (to be called from the main event loop) and App.queueStatusLine (to be called from other goroutines). These two functions now write to both the current buffer and the home buffer, - add a irc.Typings.List function that locks the list of typings before accessing it. Changes as I went through the whole code... - CAP handling is fixed (especially CAP DEL and CAP ACK), - irc.Session now handles PREFIX, - unhandled messages are now shown, except for some rare cases where it's completely useless to show them. --- cmd/test/main.go | 37 +++++++++++++++++-------------------- 1 file changed, 17 insertions(+), 20 deletions(-) (limited to 'cmd/test/main.go') diff --git a/cmd/test/main.go b/cmd/test/main.go index 6ed7f4e..804937f 100644 --- a/cmd/test/main.go +++ b/cmd/test/main.go @@ -56,17 +56,24 @@ func main() { if password != "" { auth = &irc.SASLPlain{Username: nick, Password: password} } - cli, err := irc.NewSession(conn, irc.SessionParams{ + + in, out := irc.ChanInOut(conn) + debugOut := make(chan irc.Message, 64) + go func() { + for msg := range debugOut { + fmt.Fprintf(t, "C > S: %s\n", msg.String()) + out <- msg + } + close(out) + }() + + cli := irc.NewSession(debugOut, irc.SessionParams{ Nickname: nick, Username: nick, RealName: nick, Auth: auth, - Debug: true, }) - if err != nil { - panic(fmt.Sprintf("Failed to connect to %s: %v", address, err)) - } - defer cli.Stop() + defer cli.Close() go func() { for { @@ -76,22 +83,12 @@ func main() { } cli.SendRaw(line) } - cli.Stop() + cli.Close() }() - for ev := range cli.Poll() { - switch ev := ev.(type) { - case irc.RawMessageEvent: - if ev.Outgoing { - fmt.Fprintf(t, "C > S: %s\n", ev.Message) - } else { - fmt.Fprintf(t, "C < S: %s\n", ev.Message) - } - case error: - panic(ev) - default: - fmt.Fprintf(t, "=EVENT: %T%+v\n", ev, ev) - } + for msg := range in { + cli.HandleMessage(msg) + fmt.Fprintf(t, "C < S: %s\n", msg.String()) } t.SetPrompt("") fmt.Fprintln(t, "Disconnected") -- cgit v1.2.3