refactor - moved common code to livekit/protocol
This commit is contained in:
@@ -1,64 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/go-redis/redis/v8"
|
||||
"github.com/karlseguin/ccache/v2"
|
||||
"github.com/pkg/errors"
|
||||
)
|
||||
|
||||
const (
|
||||
defaultCacheTTL = time.Minute
|
||||
)
|
||||
|
||||
type CachedRedis struct {
|
||||
ctx context.Context
|
||||
rc *redis.Client
|
||||
cache *ccache.Cache
|
||||
}
|
||||
|
||||
func NewCachedRedis(ctx context.Context, rc *redis.Client) *CachedRedis {
|
||||
return &CachedRedis{
|
||||
ctx: ctx,
|
||||
rc: rc,
|
||||
cache: ccache.New(ccache.Configure()),
|
||||
}
|
||||
}
|
||||
|
||||
func (r *CachedRedis) CachedHGet(key, hashKey string) (string, error) {
|
||||
cacheKey := key + ":" + hashKey
|
||||
item := r.cache.Get(cacheKey)
|
||||
if item != nil && !item.Expired() {
|
||||
return item.Value().(string), nil
|
||||
}
|
||||
val, err := r.rc.HGet(r.ctx, key, hashKey).Result()
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "could not hget %s[%s]", key, hashKey)
|
||||
}
|
||||
r.cache.Set(key, val, defaultCacheTTL)
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func (r *CachedRedis) CachedGet(key string) (string, error) {
|
||||
item := r.cache.Get(key)
|
||||
if item != nil && !item.Expired() {
|
||||
return item.Value().(string), nil
|
||||
}
|
||||
val, err := r.rc.Get(r.ctx, key).Result()
|
||||
if err != nil {
|
||||
return "", errors.Wrapf(err, "could not get %s", key)
|
||||
}
|
||||
|
||||
r.cache.Set(key, val, defaultCacheTTL)
|
||||
return val, nil
|
||||
}
|
||||
|
||||
func (r *CachedRedis) ExpireHash(key, hashKey string) {
|
||||
r.cache.Delete(key + ":" + hashKey)
|
||||
}
|
||||
|
||||
func (r *CachedRedis) Expire(key string) {
|
||||
r.cache.Delete(key)
|
||||
}
|
||||
@@ -1,29 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/sha1"
|
||||
|
||||
"github.com/lithammer/shortuuid/v3"
|
||||
"github.com/lytics/base62"
|
||||
)
|
||||
|
||||
const (
|
||||
RoomPrefix = "RM_"
|
||||
NodePrefix = "ND_"
|
||||
ParticipantPrefix = "PA_"
|
||||
TrackPrefix = "TR_"
|
||||
APIKeyPrefix = "API"
|
||||
)
|
||||
|
||||
func NewGuid(prefix string) string {
|
||||
return prefix + shortuuid.New()[:12]
|
||||
}
|
||||
|
||||
// Creates a hashed ID from a unique string
|
||||
func HashedID(str string) string {
|
||||
h := sha1.New()
|
||||
h.Write([]byte(str))
|
||||
val := h.Sum(nil)
|
||||
|
||||
return base62.StdEncoding.EncodeToString(val)
|
||||
}
|
||||
@@ -1,19 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"crypto/rand"
|
||||
"io"
|
||||
|
||||
"github.com/lytics/base62"
|
||||
)
|
||||
|
||||
func RandomSecret() string {
|
||||
// 256 bit secret
|
||||
buf := make([]byte, 32)
|
||||
_, err := io.ReadFull(rand.Reader, buf)
|
||||
// cannot error
|
||||
if err != nil {
|
||||
panic("could not read random")
|
||||
}
|
||||
return base62.StdEncoding.EncodeToString(buf)
|
||||
}
|
||||
@@ -1,63 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
var (
|
||||
ErrChannelClosed = errors.New("cannot write to closed channel")
|
||||
)
|
||||
|
||||
type AtomicFlag struct {
|
||||
val int32
|
||||
}
|
||||
|
||||
// set flag to value if existing flag is different, otherwise return
|
||||
func (b *AtomicFlag) TrySet(bVal bool) bool {
|
||||
var v int32
|
||||
if bVal {
|
||||
v = 1
|
||||
}
|
||||
prev := atomic.SwapInt32(&b.val, v)
|
||||
// already set. unsuccessful
|
||||
if prev == v {
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (b *AtomicFlag) Get() bool {
|
||||
return atomic.LoadInt32(&b.val) == 1
|
||||
}
|
||||
|
||||
// a channel that ignores writes when it closes instead of panic
|
||||
type CalmChannel struct {
|
||||
channel chan interface{}
|
||||
isClosed AtomicFlag
|
||||
}
|
||||
|
||||
func NewCalmChannel(size int) *CalmChannel {
|
||||
return &CalmChannel{
|
||||
channel: make(chan interface{}, size),
|
||||
}
|
||||
}
|
||||
|
||||
func (c *CalmChannel) Close() {
|
||||
if !c.isClosed.TrySet(true) {
|
||||
return
|
||||
}
|
||||
close(c.channel)
|
||||
}
|
||||
|
||||
func (c *CalmChannel) ReadChan() <-chan interface{} {
|
||||
return c.channel
|
||||
}
|
||||
|
||||
func (c *CalmChannel) Write(o interface{}) error {
|
||||
if c.isClosed.Get() {
|
||||
return ErrChannelClosed
|
||||
}
|
||||
c.channel <- o
|
||||
return nil
|
||||
}
|
||||
@@ -1,46 +0,0 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestAtomicFlag(t *testing.T) {
|
||||
t.Run("single thread, basic functionality", func(t *testing.T) {
|
||||
f := AtomicFlag{}
|
||||
assert.False(t, f.TrySet(false))
|
||||
assert.False(t, f.Get())
|
||||
assert.True(t, f.TrySet(true))
|
||||
assert.True(t, f.Get())
|
||||
})
|
||||
|
||||
t.Run("only one thread should succeed", func(t *testing.T) {
|
||||
var numSets uint32
|
||||
f := AtomicFlag{}
|
||||
wg := sync.WaitGroup{}
|
||||
for i := 0; i < 10; i++ {
|
||||
wg.Add(1)
|
||||
go func() {
|
||||
if f.TrySet(true) {
|
||||
atomic.AddUint32(&numSets, 1)
|
||||
}
|
||||
wg.Done()
|
||||
}()
|
||||
}
|
||||
wg.Wait()
|
||||
|
||||
assert.Equal(t, 1, int(numSets))
|
||||
})
|
||||
}
|
||||
|
||||
func TestCalmChannel(t *testing.T) {
|
||||
t.Run("should receive nil after close", func(t *testing.T) {
|
||||
c := NewCalmChannel(1)
|
||||
c.Close()
|
||||
|
||||
assert.Equal(t, nil, <-c.ReadChan())
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user