Move upstreamConn.history to network

This commit is contained in:
Simon Ser 2020-03-20 22:48:17 +01:00
parent 7714c84669
commit 45d118dd12
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
3 changed files with 18 additions and 16 deletions

View file

@ -708,9 +708,9 @@ func (dc *downstreamConn) register() error {
var seqPtr *uint64
if firstDownstream {
uc.lock.Lock()
seq, ok := uc.history[historyName]
uc.lock.Unlock()
uc.network.lock.Lock()
seq, ok := uc.network.history[historyName]
uc.network.lock.Unlock()
if ok {
seqPtr = &seq
}
@ -738,9 +738,9 @@ func (dc *downstreamConn) register() error {
dc.user.lock.Unlock()
if lastDownstream {
uc.lock.Lock()
uc.history[historyName] = seq
uc.lock.Unlock()
uc.network.lock.Lock()
uc.network.history[historyName] = seq
uc.network.lock.Unlock()
}
}()
})

View file

@ -8,7 +8,6 @@ import (
"net"
"strconv"
"strings"
"sync"
"time"
"github.com/emersion/go-sasl"
@ -53,9 +52,6 @@ type upstreamConn struct {
saslClient sasl.Client
saslStarted bool
lock sync.Mutex
history map[string]uint64 // TODO: move to network
}
func connectToUpstream(network *network) (*upstreamConn, error) {
@ -85,7 +81,6 @@ func connectToUpstream(network *network) (*upstreamConn, error) {
outgoing: outgoing,
ring: NewRing(network.user.srv.RingCap),
channels: make(map[string]*upstreamChannel),
history: make(map[string]uint64),
caps: make(map[string]string),
}

17
user.go
View file

@ -20,13 +20,17 @@ type downstreamIncomingMessage struct {
type network struct {
Network
user *user
conn *upstreamConn
lock sync.Mutex
conn *upstreamConn
history map[string]uint64
}
func newNetwork(user *user, record *Network) *network {
return &network{
Network: *record,
user: user,
history: make(map[string]uint64),
}
}
@ -48,18 +52,18 @@ func (net *network) run() {
uc.register()
net.user.lock.Lock()
net.lock.Lock()
net.conn = uc
net.user.lock.Unlock()
net.lock.Unlock()
if err := uc.readMessages(net.user.upstreamIncoming); err != nil {
uc.logger.Printf("failed to handle messages: %v", err)
}
uc.Close()
net.user.lock.Lock()
net.lock.Lock()
net.conn = nil
net.user.lock.Unlock()
net.lock.Unlock()
}
}
@ -95,7 +99,10 @@ func (u *user) forEachNetwork(f func(*network)) {
func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
u.lock.Lock()
for _, network := range u.networks {
network.lock.Lock()
uc := network.conn
network.lock.Unlock()
if uc == nil || !uc.registered || uc.closed {
continue
}