Skip to content

Commit

Permalink
cmd, dashboard: move subscription to dashboard.New
Browse files Browse the repository at this point in the history
  • Loading branch information
Kurkó Mihály committed Mar 22, 2019
1 parent 4ffab91 commit b36134a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 32 deletions.
30 changes: 1 addition & 29 deletions cmd/geth/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@ import (
"reflect"
"unicode"

"github.com/ethereum/go-ethereum/log"
"github.com/ethereum/go-ethereum/p2p"

cli "gopkg.in/urfave/cli.v1"

"github.com/ethereum/go-ethereum/cmd/utils"
Expand Down Expand Up @@ -163,32 +160,7 @@ func makeFullNode(ctx *cli.Context) *node.Node {
utils.RegisterEthService(stack, &cfg.Eth)

if ctx.GlobalBool(utils.DashboardEnabledFlag.Name) {
// There is a data race between the network layer and the dashboard, which
// can cause some lost peer events, therefore some peers might not appear
// on the dashboard.
// In order to solve this problem, a peer event subscription is registered
// before the network layer starts, and when the dashboard is ready, the
// stored events are passed to it.
peerEventBridge := make(chan p2p.MeteredPeerEvent, 1000) // The events are stored by and passed through this channel.
closePeerEventBridge := make(chan struct{}) // This channel gets a signal from the dashboard when it is ready.
go func() {
peerCh := make(chan p2p.MeteredPeerEvent, 200) // Channel for the initial peer events.
subPeer := p2p.SubscribeMeteredPeerEvent(peerCh) // Subscribe to the peer events.
for {
select {
case event := <-peerCh:
select {
case peerEventBridge <- event:
default:
log.Warn("Too many peer events before the dashboard starts")
}
case <-closePeerEventBridge:
subPeer.Unsubscribe()
return
}
}
}()
utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit, peerEventBridge, closePeerEventBridge)
utils.RegisterDashboardService(stack, &cfg.Dashboard, gitCommit)
}
// Whisper must be explicitly enabled by specifying at least 1 whisper flag or in dev mode
shhEnabled := enableWhisper(ctx)
Expand Down
4 changes: 2 additions & 2 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -1461,9 +1461,9 @@ func RegisterEthService(stack *node.Node, cfg *eth.Config) {
}

// RegisterDashboardService adds a dashboard to the stack.
func RegisterDashboardService(stack *node.Node, cfg *dashboard.Config, commit string, peerEventBridge chan p2p.MeteredPeerEvent, closePeerEventBridge chan struct{}) {
func RegisterDashboardService(stack *node.Node, cfg *dashboard.Config, commit string) {
stack.Register(func(ctx *node.ServiceContext) (node.Service, error) {
return dashboard.New(cfg, commit, ctx.ResolvePath("logs"), peerEventBridge, closePeerEventBridge), nil
return dashboard.New(cfg, commit, ctx.ResolvePath("logs")), nil
})
}

Expand Down
27 changes: 26 additions & 1 deletion dashboard/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,32 @@ type client struct {
}

// New creates a new dashboard instance with the given configuration.
func New(config *Config, commit string, logdir string, peerEventBridge chan p2p.MeteredPeerEvent, closePeerEventBridge chan struct{}) *Dashboard {
func New(config *Config, commit string, logdir string) *Dashboard {
// There is a data race between the network layer and the dashboard, which
// can cause some lost peer events, therefore some peers might not appear
// on the dashboard.
// In order to solve this problem, a peer event subscription is registered
// before the network layer starts, and when the dashboard is ready, the
// stored events are passed to it.
peerEventBridge := make(chan p2p.MeteredPeerEvent, 1000) // The events are stored by and passed through this channel.
closePeerEventBridge := make(chan struct{}) // This channel gets a signal from the dashboard when it is ready.
go func() {
peerCh := make(chan p2p.MeteredPeerEvent, 200) // Channel for the initial peer events.
subPeer := p2p.SubscribeMeteredPeerEvent(peerCh) // Subscribe to the peer events.
for {
select {
case event := <-peerCh:
select {
case peerEventBridge <- event:
default:
log.Warn("Too many peer events before the dashboard starts")
}
case <-closePeerEventBridge:
subPeer.Unsubscribe()
return
}
}
}()
now := time.Now()
versionMeta := ""
if len(params.VersionMeta) > 0 {
Expand Down

0 comments on commit b36134a

Please sign in to comment.