mirror of
https://github.com/raz-varren/sacrificial-socket.git
synced 2025-12-24 12:38:09 +08:00
fixed ssredis uid generator
This commit is contained in:
@@ -7,7 +7,11 @@ import (
|
||||
"github.com/go-redis/redis"
|
||||
ss "github.com/raz-varren/sacrificial-socket"
|
||||
"github.com/raz-varren/log"
|
||||
"github.com/raz-varren/sacrificial-socket/tools"
|
||||
"encoding/hex"
|
||||
)
|
||||
|
||||
var(
|
||||
rng = ss.NewRNG()
|
||||
)
|
||||
|
||||
const (
|
||||
@@ -60,7 +64,9 @@ func NewBackend(rOpts *redis.Options, ssrOpts *Options) (*RMHB, error) {
|
||||
}
|
||||
|
||||
if ssrOpts.ServerName == "" {
|
||||
ssrOpts.ServerName = tools.UID()
|
||||
uid := make([]byte, 16)
|
||||
rng.Read(uid)
|
||||
ssrOpts.ServerName = hex.EncodeToString(uid)
|
||||
}
|
||||
|
||||
roomPSName := ssrOpts.ServerGroup + ":_ss_roomcasts"
|
||||
|
||||
28
rng.go
Normal file
28
rng.go
Normal file
@@ -0,0 +1,28 @@
|
||||
package ss
|
||||
|
||||
import(
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
//RNG is a random number generator that is safe for concurrent use by multiple go routines
|
||||
type RNG struct {
|
||||
r *rand.Rand
|
||||
mu *sync.Mutex
|
||||
}
|
||||
|
||||
//Read reads len(b) random bytes into b and never returns a nil error
|
||||
func (r *RNG) Read(b []byte) (int, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.r.Read(b)
|
||||
}
|
||||
|
||||
//NewRNG creates a new random number generator
|
||||
func NewRNG() *RNG {
|
||||
return &RNG{
|
||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
mu: &sync.Mutex{},
|
||||
}
|
||||
}
|
||||
22
socket.go
22
socket.go
@@ -6,13 +6,11 @@ import (
|
||||
"encoding/json"
|
||||
"github.com/gorilla/websocket"
|
||||
"github.com/raz-varren/log"
|
||||
"math/rand"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
socketRNG = newRNG()
|
||||
socketRNG = NewRNG()
|
||||
)
|
||||
|
||||
//Socket represents a websocket connection
|
||||
@@ -190,21 +188,3 @@ func (s *Socket) Close() {
|
||||
|
||||
s.serv.hub.removeSocket(s)
|
||||
}
|
||||
|
||||
type rng struct {
|
||||
r *rand.Rand
|
||||
mu *sync.Mutex
|
||||
}
|
||||
|
||||
func (r *rng) Read(b []byte) (int, error) {
|
||||
r.mu.Lock()
|
||||
defer r.mu.Unlock()
|
||||
return r.r.Read(b)
|
||||
}
|
||||
|
||||
func newRNG() *rng {
|
||||
return &rng{
|
||||
r: rand.New(rand.NewSource(time.Now().UnixNano())),
|
||||
mu: &sync.Mutex{},
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user