Drop user.forEachDownstream

It's just a for loop.
This commit is contained in:
Simon Ser 2022-04-15 10:32:28 +02:00
parent a6636a9b0d
commit f3932ab500
2 changed files with 28 additions and 34 deletions

View File

@ -1609,11 +1609,11 @@ func (dc *downstreamConn) welcome(ctx context.Context) error {
// Only send history if we're the first connected client with that name // Only send history if we're the first connected client with that name
// for the network // for the network
firstClient := true firstClient := true
dc.user.forEachDownstream(func(c *downstreamConn) { for _, c := range dc.user.downstreamConns {
if c != dc && c.clientName == dc.clientName && c.network == dc.network { if c != dc && c.clientName == dc.clientName && c.network == dc.network {
firstClient = false firstClient = false
} }
}) }
if firstClient { if firstClient {
net.delivered.ForEachTarget(func(target string) { net.delivered.ForEachTarget(func(target string) {
lastDelivered := net.delivered.LoadID(target, dc.clientName) lastDelivered := net.delivered.LoadID(target, dc.clientName)

58
user.go
View File

@ -156,7 +156,7 @@ func newNetwork(user *user, record *Network, channels []Channel) *network {
} }
func (net *network) forEachDownstream(f func(*downstreamConn)) { func (net *network) forEachDownstream(f func(*downstreamConn)) {
net.user.forEachDownstream(func(dc *downstreamConn) { for _, dc := range net.user.downstreamConns {
if dc.network == nil && !dc.isMultiUpstream { if dc.network == nil && !dc.isMultiUpstream {
return return
} }
@ -164,7 +164,7 @@ func (net *network) forEachDownstream(f func(*downstreamConn)) {
return return
} }
f(dc) f(dc)
}) }
} }
func (net *network) isStopped() bool { func (net *network) isStopped() bool {
@ -484,12 +484,6 @@ func (u *user) forEachUpstream(f func(uc *upstreamConn)) {
} }
} }
func (u *user) forEachDownstream(f func(dc *downstreamConn)) {
for _, dc := range u.downstreamConns {
f(dc)
}
}
func (u *user) getNetwork(name string) *network { func (u *user) getNetwork(name string) *network {
for _, network := range u.networks { for _, network := range u.networks {
if network.Addr == name { if network.Addr == name {
@ -580,7 +574,7 @@ func (u *user) run() {
dc.updateRealname() dc.updateRealname()
dc.updateAccount() dc.updateAccount()
}) })
u.forEachDownstream(func(dc *downstreamConn) { for _, dc := range u.downstreamConns {
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") { if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") {
dc.SendMessage(&irc.Message{ dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(), Prefix: dc.srv.prefix(),
@ -591,7 +585,7 @@ func (u *user) run() {
}.String()}, }.String()},
}) })
} }
}) }
uc.network.lastError = nil uc.network.lastError = nil
case eventUpstreamDisconnected: case eventUpstreamDisconnected:
u.handleUpstreamDisconnected(e.uc) u.handleUpstreamDisconnected(e.uc)
@ -611,7 +605,7 @@ func (u *user) run() {
}) })
} }
net.lastError = e.err net.lastError = e.err
u.forEachDownstream(func(dc *downstreamConn) { for _, dc := range u.downstreamConns {
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") { if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") {
dc.SendMessage(&irc.Message{ dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(), Prefix: dc.srv.prefix(),
@ -621,7 +615,7 @@ func (u *user) run() {
}.String()}, }.String()},
}) })
} }
}) }
case eventUpstreamError: case eventUpstreamError:
uc := e.uc uc := e.uc
@ -629,7 +623,7 @@ func (u *user) run() {
sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", uc.network.GetName(), e.err)) sendServiceNOTICE(dc, fmt.Sprintf("disconnected from %s: %v", uc.network.GetName(), e.err))
}) })
uc.network.lastError = e.err uc.network.lastError = e.err
u.forEachDownstream(func(dc *downstreamConn) { for _, dc := range u.downstreamConns {
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") { if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") {
dc.SendMessage(&irc.Message{ dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(), Prefix: dc.srv.prefix(),
@ -639,7 +633,7 @@ func (u *user) run() {
}.String()}, }.String()},
}) })
} }
}) }
case eventUpstreamMessage: case eventUpstreamMessage:
msg, uc := e.msg, e.uc msg, uc := e.msg, e.uc
if uc.isClosed() { if uc.isClosed() {
@ -728,9 +722,9 @@ func (u *user) run() {
} }
case eventBroadcast: case eventBroadcast:
msg := e.msg msg := e.msg
u.forEachDownstream(func(dc *downstreamConn) { for _, dc := range u.downstreamConns {
dc.SendMessage(msg) dc.SendMessage(msg)
}) }
case eventUserUpdate: case eventUserUpdate:
// copy the user record because we'll mutate it // copy the user record because we'll mutate it
record := u.User record := u.User
@ -747,14 +741,14 @@ func (u *user) run() {
// If the password was updated, kill all downstream connections to // If the password was updated, kill all downstream connections to
// force them to re-authenticate with the new credentials. // force them to re-authenticate with the new credentials.
if e.password != nil { if e.password != nil {
u.forEachDownstream(func(dc *downstreamConn) { for _, dc := range u.downstreamConns {
dc.Close() dc.Close()
}) }
} }
case eventStop: case eventStop:
u.forEachDownstream(func(dc *downstreamConn) { for _, dc := range u.downstreamConns {
dc.Close() dc.Close()
}) }
for _, n := range u.networks { for _, n := range u.networks {
n.stop() n.stop()
@ -796,7 +790,7 @@ func (u *user) handleUpstreamDisconnected(uc *upstreamConn) {
return return
} }
u.forEachDownstream(func(dc *downstreamConn) { for _, dc := range u.downstreamConns {
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") { if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") {
dc.SendMessage(&irc.Message{ dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(), Prefix: dc.srv.prefix(),
@ -804,7 +798,7 @@ func (u *user) handleUpstreamDisconnected(uc *upstreamConn) {
Params: []string{"NETWORK", netIDStr, "state=disconnected"}, Params: []string{"NETWORK", netIDStr, "state=disconnected"},
}) })
} }
}) }
if uc.network.lastError == nil { if uc.network.lastError == nil {
uc.forEachDownstream(func(dc *downstreamConn) { uc.forEachDownstream(func(dc *downstreamConn) {
@ -828,11 +822,11 @@ func (u *user) addNetwork(network *network) {
func (u *user) removeNetwork(network *network) { func (u *user) removeNetwork(network *network) {
network.stop() network.stop()
u.forEachDownstream(func(dc *downstreamConn) { for _, dc := range u.downstreamConns {
if dc.network != nil && dc.network == network { if dc.network != nil && dc.network == network {
dc.Close() dc.Close()
} }
}) }
for i, net := range u.networks { for i, net := range u.networks {
if net == network { if net == network {
@ -917,7 +911,7 @@ func (u *user) createNetwork(ctx context.Context, record *Network) (*network, er
idStr := fmt.Sprintf("%v", network.ID) idStr := fmt.Sprintf("%v", network.ID)
attrs := getNetworkAttrs(network) attrs := getNetworkAttrs(network)
u.forEachDownstream(func(dc *downstreamConn) { for _, dc := range u.downstreamConns {
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") { if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") {
dc.SendMessage(&irc.Message{ dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(), Prefix: dc.srv.prefix(),
@ -925,7 +919,7 @@ func (u *user) createNetwork(ctx context.Context, record *Network) (*network, er
Params: []string{"NETWORK", idStr, attrs.String()}, Params: []string{"NETWORK", idStr, attrs.String()},
}) })
} }
}) }
return network, nil return network, nil
} }
@ -973,11 +967,11 @@ func (u *user) updateNetwork(ctx context.Context, record *Network) (*network, er
} }
// Patch downstream connections to use our fresh updated network // Patch downstream connections to use our fresh updated network
u.forEachDownstream(func(dc *downstreamConn) { for _, dc := range u.downstreamConns {
if dc.network != nil && dc.network == network { if dc.network != nil && dc.network == network {
dc.network = updatedNetwork dc.network = updatedNetwork
} }
}) }
// We need to remove the network after patching downstream connections, // We need to remove the network after patching downstream connections,
// otherwise they'll get closed // otherwise they'll get closed
@ -998,7 +992,7 @@ func (u *user) updateNetwork(ctx context.Context, record *Network) (*network, er
// TODO: only broadcast attributes that have changed // TODO: only broadcast attributes that have changed
idStr := fmt.Sprintf("%v", updatedNetwork.ID) idStr := fmt.Sprintf("%v", updatedNetwork.ID)
attrs := getNetworkAttrs(updatedNetwork) attrs := getNetworkAttrs(updatedNetwork)
u.forEachDownstream(func(dc *downstreamConn) { for _, dc := range u.downstreamConns {
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") { if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") {
dc.SendMessage(&irc.Message{ dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(), Prefix: dc.srv.prefix(),
@ -1006,7 +1000,7 @@ func (u *user) updateNetwork(ctx context.Context, record *Network) (*network, er
Params: []string{"NETWORK", idStr, attrs.String()}, Params: []string{"NETWORK", idStr, attrs.String()},
}) })
} }
}) }
return updatedNetwork, nil return updatedNetwork, nil
} }
@ -1024,7 +1018,7 @@ func (u *user) deleteNetwork(ctx context.Context, id int64) error {
u.removeNetwork(network) u.removeNetwork(network)
idStr := fmt.Sprintf("%v", network.ID) idStr := fmt.Sprintf("%v", network.ID)
u.forEachDownstream(func(dc *downstreamConn) { for _, dc := range u.downstreamConns {
if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") { if dc.caps.IsEnabled("soju.im/bouncer-networks-notify") {
dc.SendMessage(&irc.Message{ dc.SendMessage(&irc.Message{
Prefix: dc.srv.prefix(), Prefix: dc.srv.prefix(),
@ -1032,7 +1026,7 @@ func (u *user) deleteNetwork(ctx context.Context, id int64) error {
Params: []string{"NETWORK", idStr, "*"}, Params: []string{"NETWORK", idStr, "*"},
}) })
} }
}) }
return nil return nil
} }