Commit 6e226d95 authored by Aarsh Shah's avatar Aarsh Shah

changes as per review

parent a58d5968
......@@ -21,10 +21,12 @@ var log = logging.Logger("table")
var ErrPeerRejectedHighLatency = errors.New("peer rejected; latency too high")
var ErrPeerRejectedNoCapacity = errors.New("peer rejected; insufficient capacity")
// MaxCplForRefresh is the maximum cpl we support for refresh.
// This limit exists because we can only generate 'MaxCplForRefresh' bit prefixes for now.
var MaxCplForRefresh uint = 15
// maxCplForRefresh is the maximum cpl we support for refresh.
// This limit exists because we can only generate 'maxCplForRefresh' bit prefixes for now.
const maxCplForRefresh uint = 15
// CplRefresh contains a CPL(common prefix length) with the host & the last time
// we refreshed that cpl/searched for an ID which has that cpl with the host.
type CplRefresh struct {
Cpl uint
LastRefreshAt time.Time
......@@ -74,14 +76,16 @@ func NewRoutingTable(bucketsize int, localID ID, latency time.Duration, m peerst
// GetTrackedCplsForRefresh returns the Cpl's we are tracking for refresh.
// Caller is free to modify the returned slice as it is a defensive copy.
func (rt *RoutingTable) GetTrackedCplsForRefresh() []*CplRefresh {
func (rt *RoutingTable) GetTrackedCplsForRefresh() []CplRefresh {
rt.cplRefreshLk.RLock()
defer rt.cplRefreshLk.RUnlock()
var cpls []*CplRefresh
cpls := make([]CplRefresh, len(rt.cplRefreshedAt))
i := 0
for c, t := range rt.cplRefreshedAt {
cpls = append(cpls, &CplRefresh{c, t})
cpls[i] = CplRefresh{c, t}
i++
}
return cpls
......@@ -89,8 +93,8 @@ func (rt *RoutingTable) GetTrackedCplsForRefresh() []*CplRefresh {
// GenRandPeerID generates a random peerID for a given Cpl
func (rt *RoutingTable) GenRandPeerID(targetCpl uint) (peer.ID, error) {
if targetCpl > MaxCplForRefresh {
return "", fmt.Errorf("cannot generate peer ID for Cpl greater than %d", MaxCplForRefresh)
if targetCpl > maxCplForRefresh {
return "", fmt.Errorf("cannot generate peer ID for Cpl greater than %d", maxCplForRefresh)
}
localPrefix := binary.BigEndian.Uint16(rt.local)
......@@ -116,7 +120,7 @@ func (rt *RoutingTable) GenRandPeerID(targetCpl uint) (peer.ID, error) {
// ResetCplRefreshedAtForID resets the refresh time for the Cpl of the given ID.
func (rt *RoutingTable) ResetCplRefreshedAtForID(id ID, newTime time.Time) {
cpl := CommonPrefixLen(id, rt.local)
if uint(cpl) > MaxCplForRefresh {
if uint(cpl) > maxCplForRefresh {
return
}
......
......@@ -58,13 +58,13 @@ func TestGenRandPeerID(t *testing.T) {
m := pstore.NewMetrics()
rt := NewRoutingTable(1, ConvertPeerID(local), time.Hour, m)
// generate above MaxCplForRefresh fails
p, err := rt.GenRandPeerID(MaxCplForRefresh + 1)
// generate above maxCplForRefresh fails
p, err := rt.GenRandPeerID(maxCplForRefresh + 1)
require.Error(t, err)
require.Empty(t, p)
// test generate rand peer ID
for cpl := uint(0); cpl <= MaxCplForRefresh; cpl++ {
for cpl := uint(0); cpl <= maxCplForRefresh; cpl++ {
peerID, err := rt.GenRandPeerID(cpl)
require.NoError(t, err)
......@@ -80,7 +80,7 @@ func TestRefreshAndGetTrackedCpls(t *testing.T) {
rt := NewRoutingTable(1, ConvertPeerID(local), time.Hour, m)
// add cpl's for tracking
for cpl := uint(0); cpl < MaxCplForRefresh; cpl++ {
for cpl := uint(0); cpl < maxCplForRefresh; cpl++ {
peerID, err := rt.GenRandPeerID(cpl)
require.NoError(t, err)
rt.ResetCplRefreshedAtForID(ConvertPeerID(peerID), time.Now())
......@@ -88,13 +88,13 @@ func TestRefreshAndGetTrackedCpls(t *testing.T) {
// fetch cpl's
trackedCpls := rt.GetTrackedCplsForRefresh()
require.Len(t, trackedCpls, int(MaxCplForRefresh))
require.Len(t, trackedCpls, int(maxCplForRefresh))
actualCpls := make(map[uint]struct{})
for i := 0; i < len(trackedCpls); i++ {
actualCpls[trackedCpls[i].Cpl] = struct{}{}
}
for i := uint(0); i < MaxCplForRefresh; i++ {
for i := uint(0); i < maxCplForRefresh; i++ {
_, ok := actualCpls[i]
require.True(t, ok, "tracked cpl's should have cpl %d", i)
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment