-
Notifications
You must be signed in to change notification settings - Fork 1
/
partition.go
46 lines (34 loc) · 976 Bytes
/
partition.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package ksuid
import (
"encoding/binary"
"net"
)
// PartitionFunc is a func which returns a partition for the
type PartitionFunc func() uint32
// StringPartitioner uses the first four bytes of the given string to partition.
func StringPartitioner(v string) PartitionFunc {
val := binary.BigEndian.Uint32([]byte(v))
return func() uint32 {
return val
}
}
// NilPartitioner places everything into partition zero, disabling partitioning
func NilPartitioner() uint32 {
return 0
}
// MacPartitioner places everything into a partition based on the last 4 bytes of the primary network mac address
// It's a wrapped in a func which may error if
func MacPartitioner() (PartitionFunc, error) {
interfaces, err := net.Interfaces()
if err != nil { return nil, err }
for _, i := range interfaces {
if len(i.HardwareAddr) > 0 {
a := i.HardwareAddr[2:]
v := binary.BigEndian.Uint32(a)
return func() uint32 {
return v
}, nil
}
}
return nil, nil
}