Introduce 'Count' type to avoid abusing 'Offset'

This commit is contained in:
Bryan Boreham authored and Matthias Radestock committed 2016-03-19 11:47:34 +00:00
1 parent 7b6364696c
commit f11314ed3e
9 files changed
+45 -38

No files matched your search

+1
View File
@@ -15,6 +15,7 @@ type entry struct {
Peer mesh.PeerName // Who owns this range
Version uint32 // Version of this range
Free address.Offset // Number of free IPs in this range
// Note: Free should perhaps be an address.Count, but we can't change the wire protocol
}
func (e *entry) Equal(e2 *entry) bool {
+4 -4
View File
@@ -393,7 +393,7 @@ func (r *Ring) String() string {
// ReportFree is used by the allocator to tell the ring how many free
// ips are in a given range, so that ChoosePeersToAskForSpace can make
// more intelligent decisions.
func (r *Ring) ReportFree(freespace map[address.Address]address.Offset) {
func (r *Ring) ReportFree(freespace map[address.Address]address.Count) {
r.assertInvariants()
defer r.assertInvariants()
defer r.updateExportedVariables()
@@ -423,13 +423,13 @@ func (r *Ring) ReportFree(freespace map[address.Address]address.Offset) {
// Check we're not reporting more space than the range
entry, next := entries.entry(i), entries.entry(i+1)
maxSize := r.distance(entry.Token, next.Token)
common.Assert(free <= maxSize)
common.Assert(free <= address.Count(maxSize))
if entries[i].Free == free {
if address.Count(entries[i].Free) == free {
return
}
entries[i].Free = free
entries[i].Free = address.Offset(free)
entries[i].Version++
}
}
+2 -2
View File
@@ -374,7 +374,7 @@ func TestFindFree(t *testing.T) {
assertPeersWithSpace(t, ring1, start, end, 0)
// We shouldn't return outselves
ring1.ReportFree(map[address.Address]address.Offset{start: 10})
ring1.ReportFree(map[address.Address]address.Count{start: 10})
assertPeersWithSpace(t, ring1, start, end, 0)
ring1.Entries = []*entry{{Token: start, Peer: peer1name, Free: 1},
@@ -409,7 +409,7 @@ func TestReportFree(t *testing.T) {
ring1.GrantRangeToHost(middle, end, peer2name)
require.NoError(t, ring2.Merge(*ring1))
freespace := make(map[address.Address]address.Offset)
freespace := make(map[address.Address]address.Count)
for _, r := range ring2.OwnedRanges() {
freespace[r.Start] = 0
}