Skip to content

Commit

Permalink
explicit Start method for basic host
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo committed Apr 16, 2019
1 parent 6fb54b0 commit cb712e6
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
3 changes: 3 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,9 @@ func (cfg *Config) NewNode(ctx context.Context) (host.Host, error) {
}
}

// start the host background tasks
h.Start()

if router != nil {
return routed.Wrap(h, router), nil
}
Expand Down
21 changes: 12 additions & 9 deletions p2p/host/basic/basic_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ type BasicHost struct {

proc goprocess.Process

bgcancel func()
ctx context.Context
cancel func()
mx sync.Mutex
lastAddrs []ma.Multiaddr
}
Expand Down Expand Up @@ -171,8 +172,8 @@ func NewHost(ctx context.Context, net inet.Network, opts *HostOpts) (*BasicHost,
net.SetStreamHandler(h.newStreamHandler)

bgctx, cancel := context.WithCancel(ctx)
h.bgcancel = cancel
go h.background(bgctx)
h.ctx = bgctx
h.cancel = cancel

return h, nil
}
Expand Down Expand Up @@ -214,6 +215,11 @@ func New(net inet.Network, opts ...interface{}) *BasicHost {
return h
}

// Start starts background tasks in the host
func (h *BasicHost) Start() {
go h.background()
}

// newConnHandler is the remote-opened conn handler for inet.Network
func (h *BasicHost) newConnHandler(c inet.Conn) {
// Clear protocols on connecting to new peer to avoid issues caused
Expand Down Expand Up @@ -288,10 +294,7 @@ func (h *BasicHost) PushIdentify() {
}
}

func (h *BasicHost) background(ctx context.Context) {
// wait a bit for the host to initialize (avoid race with libp2p constructor)
time.Sleep(1 * time.Second)

func (h *BasicHost) background() {
// periodically schedules an IdentifyPush to update our peers for changes
// in our address set (if needed)
ticker := time.NewTicker(1 * time.Minute)
Expand All @@ -309,7 +312,7 @@ func (h *BasicHost) background(ctx context.Context) {
case <-ticker.C:
h.PushIdentify()

case <-ctx.Done():
case <-h.ctx.Done():
return
}
}
Expand Down Expand Up @@ -719,7 +722,7 @@ func (h *BasicHost) AllAddrs() []ma.Multiaddr {

// Close shuts down the Host's services (network, etc).
func (h *BasicHost) Close() error {
h.bgcancel()
h.cancel()
return h.proc.Close()
}

Expand Down

0 comments on commit cb712e6

Please sign in to comment.