mirror of
https://github.com/logos-messaging/go-libp2p-pubsub.git
synced 2026-01-03 21:33:07 +00:00
notify: handle new connections asynchronously
Trying to create a stream from with a connection notifier is *not* allowed (and will deadlock). Also, use the pubsub context when trying to open the stream.
This commit is contained in:
parent
704754bd7a
commit
5ee3bddaad
24
notify.go
24
notify.go
@ -1,8 +1,6 @@
|
|||||||
package floodsub
|
package floodsub
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
|
|
||||||
inet "github.com/libp2p/go-libp2p-net"
|
inet "github.com/libp2p/go-libp2p-net"
|
||||||
ma "github.com/multiformats/go-multiaddr"
|
ma "github.com/multiformats/go-multiaddr"
|
||||||
)
|
)
|
||||||
@ -18,17 +16,19 @@ func (p *PubSubNotif) ClosedStream(n inet.Network, s inet.Stream) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (p *PubSubNotif) Connected(n inet.Network, c inet.Conn) {
|
func (p *PubSubNotif) Connected(n inet.Network, c inet.Conn) {
|
||||||
s, err := p.host.NewStream(context.Background(), c.RemotePeer(), ID)
|
go func() {
|
||||||
if err != nil {
|
s, err := p.host.NewStream(p.ctx, c.RemotePeer(), ID)
|
||||||
log.Warning("opening new stream to peer: ", err, c.LocalPeer(), c.RemotePeer())
|
if err != nil {
|
||||||
return
|
log.Warning("opening new stream to peer: ", err, c.LocalPeer(), c.RemotePeer())
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case p.newPeers <- s:
|
case p.newPeers <- s:
|
||||||
case <-p.ctx.Done():
|
case <-p.ctx.Done():
|
||||||
s.Close()
|
s.Close()
|
||||||
}
|
}
|
||||||
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *PubSubNotif) Disconnected(n inet.Network, c inet.Conn) {
|
func (p *PubSubNotif) Disconnected(n inet.Network, c inet.Conn) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user