Skip to content

Commit

Permalink
remove oklog dependency for rand string generation (#573)
Browse files Browse the repository at this point in the history
  • Loading branch information
psotou authored Jun 18, 2024
1 parent 4d59a58 commit 87327f9
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 24 deletions.
17 changes: 15 additions & 2 deletions rueidisaside/aside.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ package rueidisaside

import (
"context"
"encoding/binary"
"encoding/hex"
"math/rand"
"strconv"
"strings"
"sync"
"time"
"unsafe"

"github.com/oklog/ulid/v2"
"github.com/redis/rueidis"
)

Expand Down Expand Up @@ -113,7 +116,7 @@ func (c *Client) keepalive() (id string, err error) {
id = c.id
c.mu.Unlock()
if id == "" {
id = PlaceholderPrefix + ulid.Make().String()
id = PlaceholderPrefix + randStr()
if err = c.client.Do(c.ctx, c.client.B().Set().Key(id).Value("").Px(c.ttl).Build()).Error(); err == nil {
c.mu.Lock()
if c.id == "" {
Expand All @@ -128,6 +131,16 @@ func (c *Client) keepalive() (id string, err error) {
return id, err
}

// randStr generates a 24-byte long, random string.
func randStr() string {
b := make([]byte, 24)
binary.LittleEndian.PutUint64(b[12:], rand.Uint64())
binary.LittleEndian.PutUint32(b[20:], rand.Uint32())
hex.Encode(b, b[12:])

return unsafe.String(unsafe.SliceData(b), len(b))
}

func (c *Client) Get(ctx context.Context, ttl time.Duration, key string, fn func(ctx context.Context, key string) (val string, err error)) (string, error) {
ctx, cancel := context.WithTimeout(ctx, ttl)
defer cancel()
Expand Down
12 changes: 0 additions & 12 deletions rueidisaside/go.mod

This file was deleted.

10 changes: 0 additions & 10 deletions rueidisaside/go.sum

This file was deleted.

0 comments on commit 87327f9

Please sign in to comment.