fix: address lints

This commit is contained in:
Steven Allen 2021-07-15 17:47:50 -07:00
parent 90f363555f
commit 4808543c09
7 changed files with 24 additions and 35 deletions

View File

@ -39,12 +39,7 @@ func (al AddrList) Less(i, j int) bool {
fda := isFDCostlyTransport(a) fda := isFDCostlyTransport(a)
fdb := isFDCostlyTransport(b) fdb := isFDCostlyTransport(b)
if !fda { if !fda {
if fdb { return fdb
return true
}
// if neither consume fd's, assume equal ordering
return false
} }
// if 'b' doesnt take a file descriptor // if 'b' doesnt take a file descriptor

View File

@ -31,11 +31,9 @@ func TestLatencyEWMAFun(t *testing.T) {
} }
for { for {
select { time.Sleep(200 * time.Millisecond)
case <-time.After(200 * time.Millisecond): m.RecordLatency(id, next())
m.RecordLatency(id, next()) print()
print()
}
} }
} }
@ -55,10 +53,8 @@ func TestLatencyEWMA(t *testing.T) {
} }
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
select { time.Sleep(200 * time.Millisecond)
case <-time.After(200 * time.Millisecond): m.RecordLatency(id, next())
m.RecordLatency(id, next())
}
} }
lat := m.LatencyEWMA(id) lat := m.LatencyEWMA(id)

View File

@ -103,6 +103,7 @@ func badgerStore(tb testing.TB) (ds.Batching, func()) {
return store, closer return store, closer
} }
//lint:ignore U1000 disabled for now
func leveldbStore(tb testing.TB) (ds.TxnDatastore, func()) { func leveldbStore(tb testing.TB) (ds.TxnDatastore, func()) {
dataPath, err := ioutil.TempDir(os.TempDir(), "leveldb") dataPath, err := ioutil.TempDir(os.TempDir(), "leveldb")
if err != nil { if err != nil {

View File

@ -68,7 +68,7 @@ func NewAddrBook() *memoryAddrBook {
ab := &memoryAddrBook{ ab := &memoryAddrBook{
segments: func() (ret addrSegments) { segments: func() (ret addrSegments) {
for i, _ := range ret { for i := range ret {
ret[i] = &addrSegment{ ret[i] = &addrSegment{
addrs: make(map[peer.ID]map[string]*expiringAddr), addrs: make(map[peer.ID]map[string]*expiringAddr),
signedPeerRecords: make(map[peer.ID]*peerRecordState)} signedPeerRecords: make(map[peer.ID]*peerRecordState)}
@ -136,7 +136,7 @@ func (mab *memoryAddrBook) PeersWithAddrs() peer.IDSlice {
for _, s := range mab.segments { for _, s := range mab.segments {
s.RLock() s.RLock()
for pid, amap := range s.addrs { for pid, amap := range s.addrs {
if amap != nil && len(amap) > 0 { if len(amap) > 0 {
pidSet.Add(pid) pidSet.Add(pid)
} }
} }
@ -196,7 +196,7 @@ func (mab *memoryAddrBook) ConsumePeerRecord(recordEnvelope *record.Envelope, tt
func (mab *memoryAddrBook) addAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) { func (mab *memoryAddrBook) addAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
if err := p.Validate(); err != nil { if err := p.Validate(); err != nil {
log.Warningf("tried to set addrs for invalid peer ID %s: %s", p, err) log.Warnw("tried to set addrs for invalid peer ID", "peer", p, "error", err)
return return
} }
@ -223,7 +223,7 @@ func (mab *memoryAddrBook) addAddrsUnlocked(s *addrSegment, p peer.ID, addrs []m
addrSet := make(map[string]struct{}, len(addrs)) addrSet := make(map[string]struct{}, len(addrs))
for _, addr := range addrs { for _, addr := range addrs {
if addr == nil { if addr == nil {
log.Warnf("was passed nil multiaddr for %s", p) log.Warnw("was passed nil multiaddr", "peer", p)
continue continue
} }
k := string(addr.Bytes()) k := string(addr.Bytes())
@ -253,7 +253,7 @@ func (mab *memoryAddrBook) addAddrsUnlocked(s *addrSegment, p peer.ID, addrs []m
// SetAddr calls mgr.SetAddrs(p, addr, ttl) // SetAddr calls mgr.SetAddrs(p, addr, ttl)
func (mab *memoryAddrBook) SetAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration) { func (mab *memoryAddrBook) SetAddr(p peer.ID, addr ma.Multiaddr, ttl time.Duration) {
if err := p.Validate(); err != nil { if err := p.Validate(); err != nil {
log.Warningf("tried to set addrs for invalid peer ID %s: %s", p, err) log.Warnw("tried to set addrs for invalid peer ID", "peer", p, "error", err)
return return
} }
@ -264,7 +264,7 @@ func (mab *memoryAddrBook) SetAddr(p peer.ID, addr ma.Multiaddr, ttl time.Durati
// This is used when we receive the best estimate of the validity of an address. // This is used when we receive the best estimate of the validity of an address.
func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) { func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Duration) {
if err := p.Validate(); err != nil { if err := p.Validate(); err != nil {
log.Warningf("tried to set addrs for invalid peer ID %s: %s", p, err) log.Warnw("tried to set addrs for invalid peer ID", "peer", p, "error", err)
return return
} }
@ -281,7 +281,7 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
exp := time.Now().Add(ttl) exp := time.Now().Add(ttl)
for _, addr := range addrs { for _, addr := range addrs {
if addr == nil { if addr == nil {
log.Warnf("was passed nil multiaddr for %s", p) log.Warnw("was passed nil multiaddr", "peer", p)
continue continue
} }
aBytes := addr.Bytes() aBytes := addr.Bytes()
@ -306,7 +306,7 @@ func (mab *memoryAddrBook) SetAddrs(p peer.ID, addrs []ma.Multiaddr, ttl time.Du
// the given oldTTL to have the given newTTL. // the given oldTTL to have the given newTTL.
func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL time.Duration) { func (mab *memoryAddrBook) UpdateAddrs(p peer.ID, oldTTL time.Duration, newTTL time.Duration) {
if err := p.Validate(); err != nil { if err := p.Validate(); err != nil {
log.Warningf("tried to set addrs for invalid peer ID %s: %s", p, err) log.Warnw("tried to set addrs for invalid peer ID", "peer", p, "error", err)
return return
} }
@ -412,7 +412,7 @@ func (mab *memoryAddrBook) ClearAddrs(p peer.ID) {
// given peer ID will be published. // given peer ID will be published.
func (mab *memoryAddrBook) AddrStream(ctx context.Context, p peer.ID) <-chan ma.Multiaddr { func (mab *memoryAddrBook) AddrStream(ctx context.Context, p peer.ID) <-chan ma.Multiaddr {
if err := p.Validate(); err != nil { if err := p.Validate(); err != nil {
log.Warningf("tried to get addrs for invalid peer ID %s: %s", p, err) log.Warnw("tried to set addrs for invalid peer ID", "peer", p, "error", err)
ch := make(chan ma.Multiaddr) ch := make(chan ma.Multiaddr)
close(ch) close(ch)
return ch return ch
@ -432,10 +432,8 @@ func (mab *memoryAddrBook) AddrStream(ctx context.Context, p peer.ID) <-chan ma.
} }
type addrSub struct { type addrSub struct {
pubch chan ma.Multiaddr pubch chan ma.Multiaddr
lk sync.Mutex ctx context.Context
buffer []ma.Multiaddr
ctx context.Context
} }
func (s *addrSub) pubAddr(a ma.Multiaddr) { func (s *addrSub) pubAddr(a ma.Multiaddr) {
@ -503,11 +501,7 @@ func (mgr *AddrSubManager) AddrStream(ctx context.Context, p peer.ID, initial []
out := make(chan ma.Multiaddr) out := make(chan ma.Multiaddr)
mgr.mu.Lock() mgr.mu.Lock()
if _, ok := mgr.subs[p]; ok { mgr.subs[p] = append(mgr.subs[p], sub)
mgr.subs[p] = append(mgr.subs[p], sub)
} else {
mgr.subs[p] = []*addrSub{sub}
}
mgr.mu.Unlock() mgr.mu.Unlock()
sort.Sort(addr.AddrList(initial)) sort.Sort(addr.AddrList(initial))

View File

@ -123,7 +123,7 @@ func testAddAddress(ab pstore.AddrBook) func(*testing.T) {
// 1 second left // 1 second left
ab.AddAddrs(id, addrs, 3*time.Second) ab.AddAddrs(id, addrs, 3*time.Second)
// 3 seconds left // 3 seconds left
time.Sleep(2) time.Sleep(2 * time.Second)
// 1 seconds left. // 1 seconds left.
// We still have the address. // We still have the address.

View File

@ -110,7 +110,7 @@ func benchmarkAddGetAndClearAddrs(ps pstore.Peerstore, addrs chan *peerpair) fun
func benchmarkGet1000PeersWithAddrs(ps pstore.Peerstore, addrs chan *peerpair) func(*testing.B) { func benchmarkGet1000PeersWithAddrs(ps pstore.Peerstore, addrs chan *peerpair) func(*testing.B) {
return func(b *testing.B) { return func(b *testing.B) {
var peers = make([]*peerpair, 1000) var peers = make([]*peerpair, 1000)
for i, _ := range peers { for i := range peers {
pp := <-addrs pp := <-addrs
ps.AddAddrs(pp.ID, pp.Addr, pstore.PermanentAddrTTL) ps.AddAddrs(pp.ID, pp.Addr, pstore.PermanentAddrTTL)
peers[i] = pp peers[i] = pp

View File

@ -126,6 +126,9 @@ func testKeyBookPeers(kb pstore.KeyBook) func(t *testing.T) {
// Add a private key. // Add a private key.
priv, _, err := pt.RandTestKeyPair(ic.RSA, 2048) priv, _, err := pt.RandTestKeyPair(ic.RSA, 2048)
if err != nil {
t.Fatal(err)
}
p2, err := peer.IDFromPrivateKey(priv) p2, err := peer.IDFromPrivateKey(priv)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)