-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4dcb31f
commit eecd5ba
Showing
5 changed files
with
88 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package logger | ||
|
||
import ( | ||
"encoding/binary" | ||
"sync/atomic" | ||
"time" | ||
"unsafe" | ||
) | ||
|
||
var streamIDCounter = uint64(time.Now().Unix()) | ||
|
||
func hexEncode(dst *[32]byte, src *[8]byte, cnt uint64) int { | ||
const hexDigits = "0123456789abcdef" | ||
for i, b := range src[:] { | ||
dst[i*2] = hexDigits[b>>4] | ||
dst[i*2+1] = hexDigits[b&0x0F] | ||
} | ||
idx := 16 | ||
for cnt > 0 { | ||
dst[idx] = hexDigits[cnt&0xF] | ||
cnt >>= 4 | ||
idx++ | ||
} | ||
for i, j := 16, idx-1; i < j; i, j = i+1, j-1 { | ||
dst[i], dst[j] = dst[j], dst[i] | ||
} | ||
return idx | ||
} | ||
|
||
func bytesToString(b *[32]byte, length int) string { | ||
return unsafe.String(&b[0], length) | ||
} | ||
|
||
func generateStreamID() string { | ||
var id [8]byte | ||
var hexID [32]byte | ||
|
||
cnt := atomic.AddUint64(&streamIDCounter, 1) | ||
timestamp := time.Now().UnixNano() | ||
binary.BigEndian.PutUint64(id[:], uint64(timestamp)) | ||
length := hexEncode(&hexID, &id, cnt) | ||
|
||
return bytesToString(&hexID, length) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package logger | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestGenerateStreamID(t *testing.T) { | ||
id1 := generateStreamID() | ||
id2 := generateStreamID() | ||
|
||
assert.NotEqual(t, id1, id2) | ||
} | ||
|
||
func BenchmarkGenerateStreamID(b *testing.B) { | ||
for i := 0; i < b.N; i++ { | ||
_ = generateStreamID() | ||
} | ||
} |