Skip to content

Commit 6038600

Browse files
committed
adding id generator
1 parent f4385dd commit 6038600

File tree

6 files changed

+60
-5
lines changed

6 files changed

+60
-5
lines changed

device/device.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7-
"github.com/segmentio/ksuid"
7+
"github.com/xmidt-org/webpa-common/device/sessionid"
88
"sync/atomic"
99
"time"
1010

@@ -185,7 +185,7 @@ func newDevice(o deviceOptions) *device {
185185
transactions: NewTransactions(),
186186
partnerIDs: partnerIDs,
187187
satClientID: o.SatClientID,
188-
sessionID: ksuid.New().String(),
188+
sessionID: sessionid.GenerateID(),
189189
trust: o.Trust,
190190
}
191191
}

device/device_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,3 +120,4 @@ func TestDeviceSessionID(t *testing.T) {
120120
assert.Equal(sessionOne.ID(), sessionTwo.ID())
121121
assert.NotEqual(sessionOne.SessionID(), sessionTwo.SessionID())
122122
}
123+

device/sessionid/id.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package sessionid
2+
3+
import (
4+
"encoding/base64"
5+
"encoding/binary"
6+
"errors"
7+
"math/rand"
8+
"time"
9+
)
10+
11+
func GenerateID() string {
12+
return GenerateIDWithTime(time.Now())
13+
}
14+
15+
func GenerateIDWithTime(t time.Time) string {
16+
var buffer [16]byte
17+
rand.Read(buffer[:])
18+
ts := uint32(t.Unix())
19+
binary.BigEndian.PutUint32(buffer[:4], ts)
20+
return base64.RawURLEncoding.EncodeToString(buffer[:])
21+
}
22+
23+
func ParseID(id string) (time.Time, error) {
24+
buffer, err := base64.RawURLEncoding.DecodeString(id)
25+
if err != nil {
26+
return time.Time{}, err
27+
}
28+
if len(buffer) != 16 {
29+
return time.Time{}, errors.New("byte array is wrong length")
30+
}
31+
ts := binary.BigEndian.Uint32(buffer[:4])
32+
return time.Unix(int64(ts), 0), nil
33+
}

device/sessionid/id_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package sessionid
2+
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestGenerateID(t *testing.T) {
10+
assert := assert.New(t)
11+
now := time.Now()
12+
id := GenerateIDWithTime(now)
13+
assert.NotEmpty(id)
14+
15+
ts, err := ParseID(id)
16+
assert.NoError(err)
17+
assert.Equal(ts.Unix(), now.Unix())
18+
19+
_, err = ParseID("")
20+
assert.Error(err)
21+
22+
_, err = ParseID("XjS1ECGCZU8WP18PmmIdc")
23+
assert.Error(err)
24+
}

go.mod

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ require (
3939
github.com/prometheus/client_golang v0.9.2
4040
github.com/rubyist/circuitbreaker v2.2.0+incompatible
4141
github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec
42-
github.com/segmentio/ksuid v1.0.2
4342
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
4443
github.com/spaolacci/murmur3 v0.0.0-20150829172844-0d12bf811670 // indirect
4544
github.com/spf13/pflag v1.0.3

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,8 +153,6 @@ github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec h1:6ncX5ko6B9L
153153
github.com/samuel/go-zookeeper v0.0.0-20180130194729-c4fab1ac1bec/go.mod h1:gi+0XIa01GRL2eRQVjQkKGqKF3SF9vZR/HnPullcV2E=
154154
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529 h1:nn5Wsu0esKSJiIVhscUtVbo7ada43DJhG55ua/hjS5I=
155155
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
156-
github.com/segmentio/ksuid v1.0.2 h1:9yBfKyw4ECGTdALaF09Snw3sLJmYIX6AbPJrAy6MrDc=
157-
github.com/segmentio/ksuid v1.0.2/go.mod h1:BXuJDr2byAiHuQaQtSKoXh1J0YmUDurywOXgB2w+OSU=
158156
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
159157
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
160158
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=

0 commit comments

Comments
 (0)