Skip to content

Commit

Permalink
fix: remove generic functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh-98 committed Sep 18, 2023
1 parent 9980125 commit 089cfd3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
36 changes: 18 additions & 18 deletions waku/v2/peermanager/peer_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import (
"github.com/libp2p/go-libp2p/p2p/discovery/backoff"
"github.com/waku-org/go-waku/logging"
wps "github.com/waku-org/go-waku/waku/v2/peerstore"
"github.com/waku-org/go-waku/waku/v2/protocol"

"go.uber.org/zap"

Expand All @@ -34,7 +33,7 @@ type PeerConnectionStrategy struct {

paused atomic.Bool
dialTimeout time.Duration
*protocol.CommonService[peer.AddrInfo]
*CommonDiscoveryService
subscriptions []<-chan PeerData

backoff backoff.BackoffFactory
Expand Down Expand Up @@ -64,12 +63,12 @@ func NewPeerConnectionStrategy(pm *PeerManager,
}
//
pc := &PeerConnectionStrategy{
cache: cache,
dialTimeout: dialTimeout,
CommonService: protocol.NewCommonService[peer.AddrInfo](),
pm: pm,
backoff: getBackOff(),
logger: logger.Named("discovery-connector"),
cache: cache,
dialTimeout: dialTimeout,
CommonDiscoveryService: NewCommonDiscoveryService(),
pm: pm,
backoff: getBackOff(),
logger: logger.Named("discovery-connector"),
}
pm.SetPeerConnector(pc)
return pc, nil
Expand All @@ -84,9 +83,9 @@ type connCacheData struct {
func (c *PeerConnectionStrategy) Subscribe(ctx context.Context, ch <-chan PeerData) {
// if not running yet, store the subscription and return
if err := c.ErrOnNotRunning(); err != nil {
c.Lock()
c.mux.Lock()
c.subscriptions = append(c.subscriptions, ch)
c.Unlock()
c.mux.Unlock()
return
}
// if running start a goroutine to consume the subscription
Expand Down Expand Up @@ -115,7 +114,7 @@ func (c *PeerConnectionStrategy) consumeSubscription(ch <-chan PeerData) {
return
}
c.pm.AddDiscoveredPeer(p)
c.PushToChan(p.AddrInfo)
c.PushToChan(p)
case <-time.After(1 * time.Second):
// This timeout is to not lock the goroutine
break
Expand All @@ -134,7 +133,7 @@ func (c *PeerConnectionStrategy) SetHost(h host.Host) {
// Start attempts to connect to the peers passed in by peerCh.
// Will not connect to peers if they are within the backoff period.
func (c *PeerConnectionStrategy) Start(ctx context.Context) error {
return c.CommonService.Start(ctx, c.start)
return c.CommonDiscoveryService.Start(ctx, c.start)

}
func (c *PeerConnectionStrategy) start() error {
Expand All @@ -149,7 +148,7 @@ func (c *PeerConnectionStrategy) start() error {

// Stop terminates the peer-connector
func (c *PeerConnectionStrategy) Stop() {
c.CommonService.Stop(func() {})
c.CommonDiscoveryService.Stop(func() {})
}

func (c *PeerConnectionStrategy) isPaused() bool {
Expand Down Expand Up @@ -221,20 +220,21 @@ func (c *PeerConnectionStrategy) dialPeers() {

for {
select {
case pi, ok := <-c.GetListeningChan():
case pd, ok := <-c.GetListeningChan():
if !ok {
return
}
addrInfo := pd.AddrInfo

if pi.ID == c.host.ID() || pi.ID == "" ||
c.host.Network().Connectedness(pi.ID) == network.Connected {
if addrInfo.ID == c.host.ID() || addrInfo.ID == "" ||
c.host.Network().Connectedness(addrInfo.ID) == network.Connected {
continue
}

if c.canDialPeer(pi) {
if c.canDialPeer(addrInfo) {
sem <- struct{}{}
c.WaitGroup().Add(1)
go c.dialPeer(pi, sem)
go c.dialPeer(addrInfo, sem)
}
case <-c.Context().Done():
return
Expand Down
16 changes: 11 additions & 5 deletions waku/v2/peermanager/peer_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,13 +172,19 @@ func (pm *PeerManager) connectToRelayPeers() {
} //Else: Should we raise some sort of unhealthy event??
}

func addrInfoToPeerData(origin wps.Origin, peerID peer.ID, host host.Host) PeerData {
return PeerData{
Origin: origin,
AddrInfo: peer.AddrInfo{
ID: peerID,
Addrs: host.Peerstore().Addrs(peerID),
},
}
}
func (pm *PeerManager) connectToPeers(peers peer.IDSlice) {
for _, peerID := range peers {
peerInfo := peer.AddrInfo{
ID: peerID,
Addrs: pm.host.Peerstore().Addrs(peerID),
}
pm.peerConnector.PushToChan(peerInfo)
peerData := addrInfoToPeerData(wps.PeerManager, peerID, pm.host)
pm.peerConnector.PushToChan(peerData)
}
}

Expand Down

0 comments on commit 089cfd3

Please sign in to comment.