Skip to content

Commit

Permalink
optimize p2p k-bucket refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
c20liu committed May 17, 2019
1 parent dd77bb3 commit 10216be
Showing 1 changed file with 37 additions and 18 deletions.
55 changes: 37 additions & 18 deletions vntp2p/discovery.go
Expand Up @@ -58,7 +58,26 @@ func (vdht *VNTDht) Start(ctx context.Context) error {
// init

// loop
go vdht.loop(ctx)
//go vdht.loop(ctx)
var bootStrapConfig = dht.DefaultBootstrapConfig
bootStrapConfig.Period = time.Duration(refreshInterval)
bootStrapConfig.Timeout = time.Duration(searchTimeOut)
proc, err := vdht.table.BootstrapWithConfig(bootStrapConfig)
if err != nil {
log.Debug("Start refresh k-bucket error", "error", err)
return err
}

// wait till ctx or dht.Context exits.
// we have to do it this way to satisfy the Routing interface (contexts)
go func() {
defer proc.Close()
select {
case <-ctx.Done():
case <-vdht.table.Context().Done():
}
}()

return nil
}

Expand All @@ -77,23 +96,23 @@ func randomID() peer.ID {
return peer.ID(id)
}

func (vdht *VNTDht) loop(ctx context.Context) {
var (
refresh = time.NewTicker(refreshInterval)
refreshDone = make(chan struct{})
)
go vdht.doRefresh(ctx, refreshDone)
// loop:
for {
// 开始搜寻

select {
case <-refresh.C:
go vdht.doRefresh(ctx, refreshDone)
}
// 刷新K桶
}
}
//func (vdht *VNTDht) loop(ctx context.Context) {
// var (
// refresh = time.NewTicker(refreshInterval)
// refreshDone = make(chan struct{})
// )
// go vdht.doRefresh(ctx, refreshDone)
// // loop:
// for {
// // 开始搜寻
//
// select {
// case <-refresh.C:
// go vdht.doRefresh(ctx, refreshDone)
// }
// // 刷新K桶
// }
//}

func (vdht *VNTDht) Lookup(ctx context.Context, targetID NodeID) []*NodeID {
// vdht.table.GetClosestPeers(vdht.Context, )
Expand Down

0 comments on commit 10216be

Please sign in to comment.