Skip to content

Commit

Permalink
update RouteTableFindPeers (#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
AstaFrode authored Jul 7, 2023
1 parent e72eb61 commit a576ca8
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 29 deletions.
20 changes: 2 additions & 18 deletions core/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"crypto/rand"
"errors"
"fmt"
"log"
"net"
"os"
"path/filepath"
Expand Down Expand Up @@ -284,6 +283,7 @@ func (n *Node) DHTFindPeer(peerid string) (peer.AddrInfo, error) {

// RouteTableFindPeers
func (n *Node) RouteTableFindPeers(limit int) (<-chan peer.AddrInfo, error) {
dutil.Advertise(n.ctxQueryFromCtxCancel, n.RoutingDiscovery, n.rendezvousVersion)
if limit <= 0 {
return n.RoutingDiscovery.FindPeers(n.ctxQueryFromCtxCancel, n.rendezvousVersion)
}
Expand Down Expand Up @@ -667,22 +667,6 @@ func verifyWorkspace(ws string) error {
return nil
}

func findPeers(ctx context.Context, routingDiscovery *drouting.RoutingDiscovery, rendezvous string) {
log.Println("Start discover service")

tick := time.NewTicker(time.Minute)
defer tick.Stop()

for {
select {
case <-ctx.Done():
return
case <-tick.C:
routingDiscovery.FindPeers(ctx, rendezvous, discovery.Limit(300))
}
}
}

func (n *Node) initProtocol(protocolPrefix string) {
n.SetProtocolPrefix(protocolPrefix)
n.WriteFileProtocol = n.NewWriteFileProtocol()
Expand Down Expand Up @@ -747,6 +731,6 @@ func (n *Node) initDHT() error {

n.IpfsDHT = kademliaDHT
n.RoutingDiscovery = drouting.NewRoutingDiscovery(n.IpfsDHT)
dutil.Advertise(n.ctxQueryFromCtxCancel, n.RoutingDiscovery, n.rendezvousVersion)

return nil
}
32 changes: 21 additions & 11 deletions examples/node/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
p2pgo "github.com/CESSProject/p2p-go"
"github.com/CESSProject/p2p-go/core"
"github.com/CESSProject/p2p-go/out"
"golang.org/x/time/rate"
)

type Nnode struct {
Expand Down Expand Up @@ -58,27 +59,36 @@ func main() {

nnode.RouteTableFindPeers(0)

tick := time.NewTicker(time.Second * 18)
tick := time.NewTicker(time.Second * 30)
defer tick.Stop()

tickdht := time.NewTicker(time.Minute)
defer tickdht.Stop()
var r = rate.Every(time.Second * 3)
var limit = rate.NewLimiter(r, 1)

for {
select {
case peer := <-nnode.GetDiscoveredPeers():
tick.Reset(time.Second * 18)
case peer, ok := <-nnode.GetDiscoveredPeers():
if !ok {
out.Err("-------------------------Closed DiscoveredPeers chan----------------------")
break
}
if limit.Allow() {
out.Tip("-------------------------Reset----------------------")
tick.Reset(time.Second * 30)
}
if len(peer.Responses) == 0 {
out.Err("-------------------------response is nil----------------------")
break
}
for _, v := range peer.Responses {
log.Println("found: ", v.ID.Pretty(), v.Addrs)
}
case <-tick.C:
out.Tip("-------------------------RouteTableFindPeers----------------------")
nnode.RouteTableFindPeers(0)
case <-tickdht.C:
if _, err := nnode.DHTFindPeer(os.Args[2]); err == nil {
out.Tip(fmt.Sprintf("++++++++++++++++++++++++++++++++++++dht found: %s", os.Args[2]))
} else {
out.Tip(fmt.Sprintf("++++++++++++++++++++++++++++++++++++dht not found: %s", os.Args[2]))
_, err = nnode.RouteTableFindPeers(0)
if err != nil {
out.Tip("-------------------------RouteTableFindPeers err----------------------")
out.Err(err.Error())
}
}
}
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/mr-tron/base58 v1.2.0
github.com/multiformats/go-multiaddr v0.8.0
github.com/pkg/errors v0.9.1
golang.org/x/time v0.0.0-20191024005414-555d28b269f0
google.golang.org/protobuf v1.28.1
)

Expand Down
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,7 @@ golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/time v0.0.0-20180412165947-fbb02b2291d2/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20190308202827-9d24e82272b4/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0 h1:/5xXl8Y5W96D+TtHSlonuFqGHIWVuyCkGJLwGh9JJFs=
golang.org/x/time v0.0.0-20191024005414-555d28b269f0/go.mod h1:tRJNPiyCQ0inRvYxbN9jk5I+vvW/OXSQhTDSoE431IQ=
golang.org/x/tools v0.0.0-20180828015842-6cd1fcedba52/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
Expand Down

0 comments on commit a576ca8

Please sign in to comment.