Skip to content

Commit

Permalink
moved string creation helper to utils
Browse files Browse the repository at this point in the history
  • Loading branch information
GNURub committed Apr 10, 2020
1 parent ffc86e7 commit a15aa45
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 21 deletions.
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand All @@ -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": [
Expand All @@ -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 changes: 4 additions & 18 deletions configure/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand All @@ -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
}

Expand Down Expand Up @@ -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 changes: 13 additions & 0 deletions utils/uid/rand.go
Original file line number Diff line number Diff line change
@@ -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)
}

0 comments on commit a15aa45

Please sign in to comment.