Compare commits
4 Commits
461b6948d6
...
4574b32242
| Author | SHA1 | Date | |
|---|---|---|---|
| 4574b32242 | |||
| 6d89e0ea3c | |||
| ff8f2c6ea1 | |||
| 83921c1e13 |
@@ -15,7 +15,8 @@ TMPDIR ?= .tmp
|
||||
REVIVE ?= $(GOBIN)/revive
|
||||
REVIVE_CONF ?= $(TOOLSDIR)/revive.toml
|
||||
REVIVE_RUN_ARGS ?= -config $(REVIVE_CONF) -formatter friendly
|
||||
REVIVE_INSTALL_URL ?= github.com/mgechev/revive@master
|
||||
REVIVE_VERSION ?= v1.3.7
|
||||
REVIVE_INSTALL_URL ?= github.com/mgechev/revive@$(REVIVE_VERSION)
|
||||
|
||||
GO_INSTALL_URLS = \
|
||||
$(REVIVE_INSTALL_URL) \
|
||||
|
||||
+49
-2
@@ -3,21 +3,25 @@
|
||||
package rings
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"syscall"
|
||||
|
||||
"darvaza.org/core"
|
||||
)
|
||||
|
||||
const (
|
||||
// RingMax indicates the highest [Ring] identifier
|
||||
RingMax = 4
|
||||
// RegionMax indicates the highest number that can be used for a [RegionID].
|
||||
RegionMax = (1 << 4) - 1
|
||||
// ZoneMax indicates the highest number that can be used for a [ZoneID].
|
||||
ZoneMax = (1 << 4) - 1
|
||||
// NodeMax indicates the highest number that can be used for a [NodeID].
|
||||
NodeMax = (1 << 12) - 1
|
||||
NodeMax = (1 << 12) - 2
|
||||
// NodeZeroMax indicates the highest number that can be used for a [NodeID]
|
||||
// when its a gateway connected to Ring 0 (backbone).
|
||||
NodeZeroMax = (1 << 8) - 1
|
||||
NodeZeroMax = (1 << 8) - 2
|
||||
|
||||
// RingZeroBits indicates the size of the prefix on the ring 0 (backbone) network.
|
||||
RingZeroBits = 16
|
||||
@@ -31,18 +35,41 @@ const (
|
||||
RingThreeBits = 12
|
||||
)
|
||||
|
||||
// RingID identifies a Ring
|
||||
type RingID int
|
||||
|
||||
// Valid tells a [RingID] is within the valid range.
|
||||
func (n RingID) Valid() bool { return n > 0 && n <= RingMax }
|
||||
|
||||
func (n RingID) String() string {
|
||||
return idString(n)
|
||||
}
|
||||
|
||||
// A Ring identifies what ring an address belongs to
|
||||
type Ring interface {
|
||||
ID() RingID
|
||||
}
|
||||
|
||||
// RegionID is the identifier of a region, valid between 1 and [RegionMax].
|
||||
type RegionID int
|
||||
|
||||
// Valid tells a [RegionID] is within the valid range.
|
||||
func (n RegionID) Valid() bool { return n > 0 && n <= RegionMax }
|
||||
|
||||
func (n RegionID) String() string {
|
||||
return idString(n)
|
||||
}
|
||||
|
||||
// ZoneID is the identifier of a zone within a region, valid between 1 and [ZoneMax].
|
||||
type ZoneID int
|
||||
|
||||
// Valid tells a [ZoneID] is within the valid range.
|
||||
func (n ZoneID) Valid() bool { return n > 0 && n <= ZoneMax }
|
||||
|
||||
func (n ZoneID) String() string {
|
||||
return idString(n)
|
||||
}
|
||||
|
||||
// NodeID is the identifier of a machine within a zone of a region, valid between
|
||||
// 1 and [NodeMax], but between 1 and [NodeZeroMax] if it will be a zone gateway.
|
||||
type NodeID int
|
||||
@@ -53,8 +80,28 @@ func (n NodeID) Valid() bool { return n > 0 && n <= NodeMax }
|
||||
// ValidZero tells a [NodeID] is within the valid range for a gateway.
|
||||
func (n NodeID) ValidZero() bool { return n > 0 && n <= NodeZeroMax }
|
||||
|
||||
func (n NodeID) String() string {
|
||||
return idString(n)
|
||||
}
|
||||
|
||||
// ErrOutOfRange is an error indicating the value of a field
|
||||
// is out of range.
|
||||
func ErrOutOfRange[T ~int | ~uint32](value T, field string) error {
|
||||
return core.Wrap(syscall.EINVAL, "%s out of range (%v)", field, value)
|
||||
}
|
||||
|
||||
type intID interface {
|
||||
~int
|
||||
Valid() bool
|
||||
}
|
||||
|
||||
func idString[T intID](p T) string {
|
||||
switch {
|
||||
case p == 0:
|
||||
return "unspecified"
|
||||
case p.Valid():
|
||||
return strconv.Itoa(int(p))
|
||||
default:
|
||||
return fmt.Sprintf("invalid (%v)", int(p))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user