Browse Source

moved string creation helper to utils

pull/88/head
Ruben Cid 5 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 @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
- JSON Web Token support.
``` json
// .livego.json
// livego.json
{
"jwt": {
"secret": "testing",
@ -26,7 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 @@ -26,7 +26,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
```
- Use redis for store room keys
``` json
// .livego.json
// livego.json
{
"redis_addr": "localhost:6379",
"server": [
@ -42,4 +42,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 @@ -42,4 +42,4 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Changed
- Show `players`.
- 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 @@ -3,16 +3,14 @@ package configure
import (
"fmt"
"log"
"math/rand"
"livego/utils/uid"
"github.com/go-redis/redis/v7"
"github.com/patrickmn/go-cache"
)
var RoomKeys *RoomKeysType
var roomUpdated = false
var saveInLocal = true
type RoomKeysType struct {
@ -49,7 +47,7 @@ func Init() { @@ -49,7 +47,7 @@ func Init() {
func (r *RoomKeysType) SetKey(channel string) (key string, err error) {
if !saveInLocal {
for {
key = randStringRunes(48)
key = uid.RandStringRunes(48)
if _, err = r.redisCli.Get(key).Result(); err == redis.Nil {
err = r.redisCli.Set(channel, key, 0).Err()
if err != nil {
@ -65,14 +63,13 @@ func (r *RoomKeysType) SetKey(channel string) (key string, err error) { @@ -65,14 +63,13 @@ func (r *RoomKeysType) SetKey(channel string) (key string, err error) {
}
for {
key = randStringRunes(48)
key = uid.RandStringRunes(48)
if _, found := r.localCache.Get(key); !found {
r.localCache.SetDefault(channel, key)
r.localCache.SetDefault(key, channel)
break
}
}
roomUpdated = true
return
}
@ -137,14 +134,3 @@ func (r *RoomKeysType) DeleteKey(key string) bool { @@ -137,14 +134,3 @@ func (r *RoomKeysType) DeleteKey(key string) bool {
}
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 @@ @@ -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