Consume ring buffer for networks added on-the-fly

This commit is contained in:
Simon Ser 2020-03-25 11:28:25 +01:00
parent 293a0e8e20
commit 21241c2009
No known key found for this signature in database
GPG key ID: 0FDE7BE0E88F5E48
2 changed files with 71 additions and 47 deletions

View file

@ -702,11 +702,27 @@ func (dc *downstreamConn) register() error {
})
dc.forEachNetwork(func(net *network) {
// TODO: need to take dc.network into account when deciding whether or
// not to load history
dc.runNetwork(net, firstDownstream)
})
return nil
}
// runNetwork starts listening for messages coming from the network's ring
// buffer.
//
// It panics if the network is not suitable for the downstream connection.
func (dc *downstreamConn) runNetwork(net *network, loadHistory bool) {
if dc.network != nil && net != dc.network {
panic("network not suitable for downstream connection")
}
historyName := dc.rawUsername
// TODO: need to take dc.network into account here
var seqPtr *uint64
if firstDownstream {
if loadHistory {
net.lock.Lock()
seq, ok := net.history[historyName]
net.lock.Unlock()
@ -715,7 +731,6 @@ func (dc *downstreamConn) register() error {
}
}
// TODO: we need to create a consumer when adding networks on-the-fly
consumer, ch := net.ring.NewConsumer(seqPtr)
go func() {
for {
@ -749,9 +764,6 @@ func (dc *downstreamConn) register() error {
net.lock.Unlock()
}
}()
})
return nil
}
func (dc *downstreamConn) runUntilRegistered() error {

12
user.go
View file

@ -198,14 +198,26 @@ func (u *user) removeDownstream(dc *downstreamConn) {
}
func (u *user) createNetwork(net *Network) (*network, error) {
if net.ID != 0 {
panic("tried creating an already-existing network")
}
network := newNetwork(u, net)
err := u.srv.db.StoreNetwork(u.Username, &network.Network)
if err != nil {
return nil, err
}
u.forEachDownstream(func(dc *downstreamConn) {
if dc.network == nil {
dc.runNetwork(network, false)
}
})
u.lock.Lock()
u.networks = append(u.networks, network)
u.lock.Unlock()
go network.run()
return network, nil
}