Remove downstreamConn.lock

Everything is now accessed from the user goroutine now that the
per-network ring buffer goroutine is gone.
This commit is contained in:
Simon Ser 2020-04-06 18:23:39 +02:00
parent 3bece53335
commit 40ff14ec6c
No known key found for this signature in database
GPG Key ID: 0FDE7BE0E88F5E48

View File

@ -8,7 +8,6 @@ import (
"net" "net"
"strconv" "strconv"
"strings" "strings"
"sync"
"time" "time"
"github.com/emersion/go-sasl" "github.com/emersion/go-sasl"
@ -68,15 +67,13 @@ type downstreamConn struct {
network *network // can be nil network *network // can be nil
ringConsumers map[*network]*RingConsumer ringConsumers map[*network]*RingConsumer
ourMessages map[*irc.Message]struct{}
caps map[string]bool
negociatingCaps bool negociatingCaps bool
capVersion int capVersion int
saslServer sasl.Server saslServer sasl.Server
lock sync.Mutex
ourMessages map[*irc.Message]struct{}
caps map[string]bool
} }
func newDownstreamConn(srv *Server, netConn net.Conn, id uint64) *downstreamConn { func newDownstreamConn(srv *Server, netConn net.Conn, id uint64) *downstreamConn {
@ -209,20 +206,17 @@ func (dc *downstreamConn) readMessages(ch chan<- event) error {
return nil return nil
} }
func (dc *downstreamConn) getCap(name string) bool { // SendMessage sends an outgoing message.
dc.lock.Lock() //
defer dc.lock.Unlock() // This can only called from the user goroutine.
return dc.caps[name]
}
func (dc *downstreamConn) SendMessage(msg *irc.Message) { func (dc *downstreamConn) SendMessage(msg *irc.Message) {
if !dc.getCap("message-tags") { if !dc.caps["message-tags"] {
msg = msg.Copy() msg = msg.Copy()
for name := range msg.Tags { for name := range msg.Tags {
supported := false supported := false
switch name { switch name {
case "time": case "time":
supported = dc.getCap("server-time") supported = dc.caps["server-time"]
} }
if !supported { if !supported {
delete(msg.Tags, name) delete(msg.Tags, name)
@ -234,11 +228,9 @@ func (dc *downstreamConn) SendMessage(msg *irc.Message) {
} }
func (dc *downstreamConn) sendFromUpstream(msg *irc.Message, uc *upstreamConn) { func (dc *downstreamConn) sendFromUpstream(msg *irc.Message, uc *upstreamConn) {
dc.lock.Lock()
_, ours := dc.ourMessages[msg] _, ours := dc.ourMessages[msg]
delete(dc.ourMessages, msg) delete(dc.ourMessages, msg)
dc.lock.Unlock() if ours && !dc.caps["echo-message"] {
if ours && !dc.getCap("echo-message") {
// The message comes from our connection, don't echo it // The message comes from our connection, don't echo it
// back // back
return return
@ -300,7 +292,7 @@ func (dc *downstreamConn) handleMessageUnregistered(msg *irc.Message) error {
return err return err
} }
case "AUTHENTICATE": case "AUTHENTICATE":
if !dc.getCap("sasl") { if !dc.caps["sasl"] {
return ircError{&irc.Message{ return ircError{&irc.Message{
Command: irc.ERR_SASLFAIL, Command: irc.ERR_SASLFAIL,
Params: []string{"*", "AUTHENTICATE requires the \"sasl\" capability to be enabled"}, Params: []string{"*", "AUTHENTICATE requires the \"sasl\" capability to be enabled"},
@ -441,11 +433,9 @@ func (dc *downstreamConn) handleCapCommand(cmd string, args []string) error {
} }
case "LIST": case "LIST":
var caps []string var caps []string
dc.lock.Lock()
for name := range dc.caps { for name := range dc.caps {
caps = append(caps, name) caps = append(caps, name)
} }
dc.lock.Unlock()
// TODO: multi-line replies // TODO: multi-line replies
dc.SendMessage(&irc.Message{ dc.SendMessage(&irc.Message{
@ -463,7 +453,6 @@ func (dc *downstreamConn) handleCapCommand(cmd string, args []string) error {
caps := strings.Fields(args[0]) caps := strings.Fields(args[0])
ack := true ack := true
dc.lock.Lock()
for _, name := range caps { for _, name := range caps {
name = strings.ToLower(name) name = strings.ToLower(name)
enable := !strings.HasPrefix(name, "-") enable := !strings.HasPrefix(name, "-")
@ -483,7 +472,6 @@ func (dc *downstreamConn) handleCapCommand(cmd string, args []string) error {
ack = false ack = false
} }
} }
dc.lock.Unlock()
reply := "NAK" reply := "NAK"
if ack { if ack {
@ -1212,9 +1200,7 @@ func (dc *downstreamConn) handleMessageRegistered(msg *irc.Message) error {
Command: "PRIVMSG", Command: "PRIVMSG",
Params: []string{upstreamName, text}, Params: []string{upstreamName, text},
} }
dc.lock.Lock()
dc.ourMessages[echoMsg] = struct{}{} dc.ourMessages[echoMsg] = struct{}{}
dc.lock.Unlock()
uc.appendLog(upstreamName, echoMsg) uc.appendLog(upstreamName, echoMsg)