Updates based on code review

This commit is contained in:
Jeffrey Picard 2021-12-03 11:52:21 -05:00
parent 2e52c1639c
commit 2a1d6fa7d4
5 changed files with 25 additions and 23 deletions

View file

@ -11,4 +11,4 @@ jobs:
with: with:
go-version: 1.16.5 go-version: 1.16.5
- run: go build . - run: go build .
- run: cd server && go test -v -race - run: go test -v -race ./...

View file

@ -30,4 +30,4 @@ jobs:
- run: go get github.com/golang/protobuf/protoc-gen-go google.golang.org/grpc/cmd/protoc-gen-go-grpc - run: go get github.com/golang/protobuf/protoc-gen-go google.golang.org/grpc/cmd/protoc-gen-go-grpc
- run: go build . - run: go build .
- run: ./protobuf/build.sh - run: ./protobuf/build.sh
- run: cd server && go test -v -race - run: go test -v -race ./...

View file

@ -18,9 +18,9 @@ import (
// Peer holds relevant information about peers that we know about. // Peer holds relevant information about peers that we know about.
type Peer struct { type Peer struct {
Address string Address string
Port string Port string
Ts time.Time LastSeen time.Time
} }
var ( var (
@ -68,8 +68,7 @@ func (s *Server) getNumSubs() int64 {
return *s.NumPeerSubs return *s.NumPeerSubs
} }
// getAndSetExternalIp takes the ip and port of a peer running a UDP server and // getAndSetExternalIp detects the server's external IP and stores it.
// pings it, so we can determine our own external IP address.
func (s *Server) getAndSetExternalIp(ip, port string) error { func (s *Server) getAndSetExternalIp(ip, port string) error {
pong, err := UDPPing(ip, port) pong, err := UDPPing(ip, port)
if err != nil { if err != nil {
@ -146,9 +145,9 @@ retry:
} }
newPeer := &Peer{ newPeer := &Peer{
Address: ipPort[0], Address: ipPort[0],
Port: ipPort[1], Port: ipPort[1],
Ts: time.Now(), LastSeen: time.Now(),
} }
log.Printf("pinging peer %+v\n", newPeer) log.Printf("pinging peer %+v\n", newPeer)
err = s.addPeer(newPeer, true, true) err = s.addPeer(newPeer, true, true)
@ -377,7 +376,7 @@ func (s *Server) addPeer(newPeer *Peer, ping bool, subscribe bool) error {
} }
} }
} else { } else {
oldServer.Ts = time.Now() oldServer.LastSeen = time.Now()
} }
return nil return nil
} }
@ -387,9 +386,9 @@ func (s *Server) addPeer(newPeer *Peer, ping bool, subscribe bool) error {
func (s *Server) mergePeers(servers []*pb.ServerMessage) { func (s *Server) mergePeers(servers []*pb.ServerMessage) {
for _, srvMsg := range servers { for _, srvMsg := range servers {
newPeer := &Peer{ newPeer := &Peer{
Address: srvMsg.Address, Address: srvMsg.Address,
Port: srvMsg.Port, Port: srvMsg.Port,
Ts: time.Now(), LastSeen: time.Now(),
} }
err := s.addPeer(newPeer, false, true) err := s.addPeer(newPeer, false, true)
// This shouldn't happen because we're not pinging them. // This shouldn't happen because we're not pinging them.

View file

@ -246,9 +246,9 @@ func (s *Server) Hello(ctx context.Context, args *pb.HelloMessage) (*pb.HelloMes
port := args.Port port := args.Port
host := args.Host host := args.Host
newPeer := &Peer{ newPeer := &Peer{
Address: host, Address: host,
Port: port, Port: port,
Ts: time.Now(), LastSeen: time.Now(),
} }
log.Println(newPeer) log.Println(newPeer)
@ -270,9 +270,9 @@ func (s *Server) PeerSubscribe(ctx context.Context, in *pb.ServerMessage) (*pb.S
metrics.RequestsCount.With(prometheus.Labels{"method": "peer_subscribe"}).Inc() metrics.RequestsCount.With(prometheus.Labels{"method": "peer_subscribe"}).Inc()
var msg = "Success" var msg = "Success"
peer := &Peer{ peer := &Peer{
Address: in.Address, Address: in.Address,
Port: in.Port, Port: in.Port,
Ts: time.Now(), LastSeen: time.Now(),
} }
if _, loaded := s.PeerSubsLoadOrStore(peer); !loaded { if _, loaded := s.PeerSubsLoadOrStore(peer); !loaded {
@ -290,9 +290,9 @@ func (s *Server) AddPeer(ctx context.Context, args *pb.ServerMessage) (*pb.Strin
metrics.RequestsCount.With(prometheus.Labels{"method": "add_peer"}).Inc() metrics.RequestsCount.With(prometheus.Labels{"method": "add_peer"}).Inc()
var msg = "Success" var msg = "Success"
newPeer := &Peer{ newPeer := &Peer{
Address: args.Address, Address: args.Address,
Port: args.Port, Port: args.Port,
Ts: time.Now(), LastSeen: time.Now(),
} }
err := s.addPeer(newPeer, true, true) err := s.addPeer(newPeer, true, true)
if err != nil { if err != nil {

View file

@ -14,6 +14,9 @@ import (
const maxBufferSize = 1024 const maxBufferSize = 1024
// genesis blocktime (which is actually wrong) // genesis blocktime (which is actually wrong)
// magic constant for the UDPPing protocol. The above comment is taken from
// the python code this was implemented off of.
// https://github.com/lbryio/lbry-sdk/blob/7d49b046d44a4b7067d5dc1d6cd65ff0475c71c8/lbry/wallet/server/udp.py#L12
const magic = 1446058291 const magic = 1446058291
const protocolVersion = 1 const protocolVersion = 1
const defaultFlags = 0b00000000 const defaultFlags = 0b00000000