Browse Source

clean up id generation, add prefixes to IDs.

master
Gisle Aune 4 years ago
parent
commit
450fde2567
  1. 22
      client.go
  2. 10
      go.sum

22
client.go

@ -98,7 +98,7 @@ type Client struct {
// tear down clients upon quitting.
func New(ctx context.Context, config Config) *Client {
client := &Client{
id: generateClientID(),
id: generateClientID("C"),
values: make(map[string]interface{}),
events: make(chan *Event, 64),
sends: make(chan string, 64),
@ -672,7 +672,7 @@ func (client *Client) AddTarget(target Target) (id string, err error) {
}
}
id = generateClientID()
id = generateClientID("T")
client.targets = append(client.targets, target)
client.targetIds[target] = id
@ -1493,22 +1493,16 @@ func (client *Client) handleInTarget(target Target, event *Event) {
client.mutex.RUnlock()
}
func generateClientID() string {
bytes := make([]byte, 12)
_, err := rand.Read(bytes)
func generateClientID(prefix string) string {
buffer := [12]byte{}
_, err := rand.Read(buffer[:])
// Ugly fallback if crypto rand doesn't work.
if err != nil {
rng := mathRand.NewSource(time.Now().UnixNano())
result := strconv.FormatInt(rng.Int63(), 16)
for len(result) < 24 {
result += strconv.FormatInt(rng.Int63(), 16)
}
return result[:24]
mathRand.Read(buffer[:])
}
binary.BigEndian.PutUint32(bytes[4:], uint32(time.Now().Unix()))
binary.BigEndian.PutUint32(buffer[4:], uint32(time.Now().Unix()))
return hex.EncodeToString(bytes)
return prefix + hex.EncodeToString(buffer[:])[1:]
}

10
go.sum

@ -0,0 +1,10 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0=
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
Loading…
Cancel
Save