A method of P2P to connect to service peers. This method uses the Advertise() functionality of the Peer Discovery Service to advertise the service and then disovers all peers advertising the same. The peer discovery is handled by a go-routine that will read from a channel of peer address information
()
| 101 | // The peer discovery is handled by a go-routine that will read from a channel |
| 102 | // of peer address information until the peer channel closes |
| 103 | func (p2p *P2P) AdvertiseConnect() { |
| 104 | // Advertise the availability of the service on this node |
| 105 | ttl, err := p2p.Discovery.Advertise(p2p.Ctx, p2p.service) |
| 106 | // Debug log |
| 107 | logrus.Debugln("Advertised the p2p Service.") |
| 108 | // Sleep to give time for the advertisement to propagate |
| 109 | time.Sleep(time.Second * 5) |
| 110 | // Debug log |
| 111 | logrus.Debugf("Service Time-to-Live is %s", ttl) |
| 112 | |
| 113 | // Find all peers advertising the same service |
| 114 | peerchan, err := p2p.Discovery.FindPeers(p2p.Ctx, p2p.service) |
| 115 | // Handle any potential error |
| 116 | if err != nil { |
| 117 | logrus.WithFields(logrus.Fields{ |
| 118 | "error": err.Error(), |
| 119 | }).Fatalln("P2P Peer Discovery Failed!") |
| 120 | } |
| 121 | // Trace log |
| 122 | logrus.Traceln("Discovered p2p Service Peers.") |
| 123 | |
| 124 | // Connect to peers as they are discovered |
| 125 | go handlePeerDiscovery(p2p.Host, peerchan) |
| 126 | // Trace log |
| 127 | logrus.Traceln("Started Peer Connection Handler.") |
| 128 | } |
| 129 | |
| 130 | // A method of P2P to connect to service peers. |
| 131 | // This method uses the Provide() functionality of the Kademlia DHT directly to announce |
no test coverage detected