-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(CommonService): add channel and use commonService in discv5 (#735)
* feat(CommonService): add channel and use commonService in discv5 * fix: add mutex to PushToChan * fix: remove generic functionality
- Loading branch information
Showing
6 changed files
with
133 additions
and
127 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package peermanager | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/ethereum/go-ethereum/p2p/enode" | ||
"github.com/libp2p/go-libp2p/core/peer" | ||
wps "github.com/waku-org/go-waku/waku/v2/peerstore" | ||
"github.com/waku-org/go-waku/waku/v2/protocol" | ||
) | ||
|
||
// PeerData contains information about a peer useful in establishing connections with it. | ||
type PeerData struct { | ||
Origin wps.Origin | ||
AddrInfo peer.AddrInfo | ||
ENR *enode.Node | ||
PubSubTopics []string | ||
} | ||
|
||
type CommonDiscoveryService struct { | ||
commonService *protocol.CommonService | ||
channel chan PeerData | ||
} | ||
|
||
func NewCommonDiscoveryService() *CommonDiscoveryService { | ||
return &CommonDiscoveryService{ | ||
commonService: protocol.NewCommonService(), | ||
} | ||
} | ||
|
||
func (sp *CommonDiscoveryService) Start(ctx context.Context, fn func() error) error { | ||
return sp.commonService.Start(ctx, func() error { | ||
// currently is used in discv5,peerConnector,rendevzous for returning new discovered Peers to peerConnector for connecting with them | ||
// mutex protection for this operation | ||
sp.channel = make(chan PeerData) | ||
return fn() | ||
}) | ||
} | ||
|
||
func (sp *CommonDiscoveryService) Stop(stopFn func()) { | ||
sp.commonService.Stop(func() { | ||
stopFn() | ||
sp.WaitGroup().Wait() // waitgroup is waited here so that channel can be closed after all the go rountines have stopped in service. | ||
// there is a wait in the CommonService too | ||
close(sp.channel) | ||
}) | ||
} | ||
func (sp *CommonDiscoveryService) GetListeningChan() <-chan PeerData { | ||
return sp.channel | ||
} | ||
func (sp *CommonDiscoveryService) PushToChan(data PeerData) bool { | ||
sp.RLock() | ||
defer sp.RUnlock() | ||
if err := sp.ErrOnNotRunning(); err != nil { | ||
return false | ||
} | ||
select { | ||
case sp.channel <- data: | ||
return true | ||
case <-sp.Context().Done(): | ||
return false | ||
} | ||
} | ||
|
||
func (sp *CommonDiscoveryService) RLock() { | ||
sp.commonService.RLock() | ||
} | ||
func (sp *CommonDiscoveryService) RUnlock() { | ||
sp.commonService.RUnlock() | ||
} | ||
|
||
func (sp *CommonDiscoveryService) Context() context.Context { | ||
return sp.commonService.Context() | ||
} | ||
func (sp *CommonDiscoveryService) ErrOnNotRunning() error { | ||
return sp.commonService.ErrOnNotRunning() | ||
} | ||
func (sp *CommonDiscoveryService) WaitGroup() *sync.WaitGroup { | ||
return sp.commonService.WaitGroup() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,6 +20,7 @@ const ( | |
PeerExchange | ||
DNSDiscovery | ||
Rendezvous | ||
PeerManager | ||
) | ||
|
||
const peerOrigin = "origin" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.