Browse Source

moved string creation helper to utils

pull/88/head
Ruben Cid 6 years ago
parent
commit
a15aa45484
  1. 6
      CHANGELOG.md
  2. 22
      configure/channel.go
  3. 13
      utils/uid/rand.go

6
CHANGELOG.md

@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added ### Added
- JSON Web Token support. - JSON Web Token support.
``` json ``` json
// .livego.json // livego.json
{ {
"jwt": { "jwt": {
"secret": "testing", "secret": "testing",
@ -26,7 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
``` ```
- Use redis for store room keys - Use redis for store room keys
``` json ``` json
// .livego.json // livego.json
{ {
"redis_addr": "localhost:6379", "redis_addr": "localhost:6379",
"server": [ "server": [
@ -42,4 +42,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed ### Changed
- Show `players`. - Show `players`.
- Show `stream_id`. - Show `stream_id`.
- Deleted keys saved in physical file, now the keys are in cached using `go-cache` - Deleted keys saved in physical file, now the keys are in cached using `go-cache` by default.

22
configure/channel.go

@ -3,16 +3,14 @@ package configure
import ( import (
"fmt" "fmt"
"log" "log"
"math/rand"
"livego/utils/uid"
"github.com/go-redis/redis/v7" "github.com/go-redis/redis/v7"
"github.com/patrickmn/go-cache" "github.com/patrickmn/go-cache"
) )
var RoomKeys *RoomKeysType var RoomKeys *RoomKeysType
var roomUpdated = false
var saveInLocal = true var saveInLocal = true
type RoomKeysType struct { type RoomKeysType struct {
@ -49,7 +47,7 @@ func Init() {
func (r *RoomKeysType) SetKey(channel string) (key string, err error) { func (r *RoomKeysType) SetKey(channel string) (key string, err error) {
if !saveInLocal { if !saveInLocal {
for { for {
key = randStringRunes(48) key = uid.RandStringRunes(48)
if _, err = r.redisCli.Get(key).Result(); err == redis.Nil { if _, err = r.redisCli.Get(key).Result(); err == redis.Nil {
err = r.redisCli.Set(channel, key, 0).Err() err = r.redisCli.Set(channel, key, 0).Err()
if err != nil { if err != nil {
@ -65,14 +63,13 @@ func (r *RoomKeysType) SetKey(channel string) (key string, err error) {
} }
for { for {
key = randStringRunes(48) key = uid.RandStringRunes(48)
if _, found := r.localCache.Get(key); !found { if _, found := r.localCache.Get(key); !found {
r.localCache.SetDefault(channel, key) r.localCache.SetDefault(channel, key)
r.localCache.SetDefault(key, channel) r.localCache.SetDefault(key, channel)
break break
} }
} }
roomUpdated = true
return return
} }
@ -137,14 +134,3 @@ func (r *RoomKeysType) DeleteKey(key string) bool {
} }
return false return false
} }
// helpers
var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}

13
utils/uid/rand.go

@ -0,0 +1,13 @@
package uid
import "math/rand"
var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func RandStringRunes(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letterRunes[rand.Intn(len(letterRunes))]
}
return string(b)
}
Loading…
Cancel
Save