Skip to content

Commit

Permalink
using redis for store roomkeys
Browse files Browse the repository at this point in the history
  • Loading branch information
GNURub committed Apr 8, 2020
1 parent dfa54ff commit ebac4bf
Show file tree
Hide file tree
Showing 9 changed files with 241 additions and 40 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
# Created by .ignore support plugin (hsz.mobi)
.idea
dist
room_keys.json
room_keys.json
.vscode
.tmp
1 change: 1 addition & 0 deletions .livego.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"redis_addr": "localhost:6379",
"server": [
{
"appname": "live",
Expand Down
14 changes: 14 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
]
}
```
- Use redis for store room keys
``` json
// .livego.json
{
"redis_addr": "localhost:6379",
"server": [
{
"appname": "live",
"liveon": "on",
"hlson": "on"
}
]
}
```

### Changed
- Show `players`.
Expand Down
116 changes: 86 additions & 30 deletions configure/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,42 +2,62 @@ package configure

import (
"encoding/json"
"flag"
"fmt"
"io/ioutil"
"log"
"math/rand"
"sync"
"time"
)
var (
roomKeySaveFile = flag.String("keyFile", "room_keys.json", "path to save room keys")

"github.com/go-redis/redis/v7"
)

var RoomKeys = LoadRoomKey(*roomKeySaveFile)
var RoomKeys = LoadRoomKey(*GetKeyFile())

var roomUpdated = false

func init() {
var saveInFile = true
var redisCli *redis.Client

func Init() {
saveInFile = GetRedisAddr() == nil

rand.Seed(time.Now().UnixNano())
go func() {
for {
time.Sleep(15 * time.Second)
if roomUpdated {
RoomKeys.Save(*roomKeySaveFile)
roomUpdated = false
if saveInFile {
go func() {
for {
time.Sleep(15 * time.Second)
if roomUpdated {
RoomKeys.Save(*roomKeySaveFile)
roomUpdated = false
}
}
}
}()
}
}()

return
}

redisCli = redis.NewClient(&redis.Options{
Addr: *GetRedisAddr(),
Password: *GetRedisPwd(),
DB: 0,
})

_, err := redisCli.Ping().Result()
if err != nil {
panic(err)
}

log.Printf("Redis connected")
}

type RoomKeysType struct {
mapChanKey sync.Map
mapKeyChan sync.Map
}

func LoadRoomKey(f string) *RoomKeysType {
result := &RoomKeysType {
result := &RoomKeysType{
mapChanKey: sync.Map{},
mapKeyChan: sync.Map{},
}
Expand Down Expand Up @@ -76,8 +96,24 @@ func (r *RoomKeysType) Save(f string) {
}

// set/reset a random key for channel
func (r *RoomKeysType) SetKey(channel string) string {
var key string
func (r *RoomKeysType) SetKey(channel string) (key string, err error) {
if !saveInFile {
for {
key = randStringRunes(48)
if _, err = redisCli.Get(key).Result(); err == redis.Nil {
err = redisCli.Set(channel, key, 0).Err()
if err != nil {
return
}

err = redisCli.Set(key, channel, 0).Err()
return
} else if err != nil {
return
}
}
}

for {
key = randStringRunes(48)
if _, found := r.mapKeyChan.Load(key); !found {
Expand All @@ -87,31 +123,48 @@ func (r *RoomKeysType) SetKey(channel string) string {
}
}
roomUpdated = true
return key
return
}

func (r *RoomKeysType) GetKey(channel string) string {
func (r *RoomKeysType) GetKey(channel string) (newKey string, err error) {
if !saveInFile {
if newKey, err = redisCli.Get(channel).Result(); err == redis.Nil {
newKey, err = r.SetKey(channel)
log.Printf("[KEY] new channel [%s]: %s", channel, newKey)
return
}

return
}

var key interface{}
var found bool
if key, found = r.mapChanKey.Load(channel); found {
return key.(string)
} else {
newkey := r.SetKey(channel)
log.Printf("[KEY] new channel [%s]: %s", channel, newkey)
return newkey
return key.(string), nil
}
newKey, err = r.SetKey(channel)
log.Printf("[KEY] new channel [%s]: %s", channel, newKey)
return
}

func (r *RoomKeysType) GetChannel(key string) string {
channel, found := r.mapKeyChan.Load(key)
func (r *RoomKeysType) GetChannel(key string) (channel string, err error) {
if !saveInFile {
return redisCli.Get(key).Result()
}

chann, found := r.mapKeyChan.Load(key)
if found {
return channel.(string)
return chann.(string), nil
} else {
return ""
return "", fmt.Errorf("%s does not exists", key)
}
}

func (r *RoomKeysType) DeleteChannel(channel string) bool {
if !saveInFile {
return redisCli.Del(channel).Err() != nil
}

key, ok := r.mapChanKey.Load(channel)
if ok {
r.mapChanKey.Delete(channel)
Expand All @@ -122,6 +175,10 @@ func (r *RoomKeysType) DeleteChannel(channel string) bool {
}

func (r *RoomKeysType) DeleteKey(key string) bool {
if !saveInFile {
return redisCli.Del(key).Err() != nil
}

channel, ok := r.mapKeyChan.Load(key)
if ok {
r.mapChanKey.Delete(channel)
Expand All @@ -131,7 +188,6 @@ func (r *RoomKeysType) DeleteKey(key string) bool {
return false
}


// helpers
var letterRunes = []rune("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

Expand Down
44 changes: 42 additions & 2 deletions configure/liveconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package configure

import (
"encoding/json"
"flag"
"io/ioutil"
"log"
)
Expand All @@ -18,6 +19,11 @@ import (
]
}
*/
var (
roomKeySaveFile = flag.String("KeyFile", "room_keys.json", "path to save room keys")
RedisAddr = flag.String("redis_addr", "", "redis addr to save room keys ex. localhost:6379")
RedisPwd = flag.String("redis_pwd", "", "redis password")
)

type Application struct {
Appname string `json:"appname"`
Expand All @@ -32,8 +38,11 @@ type JWTCfg struct {
}

type ServerCfg struct {
JWTCfg `json:"jwt"`
Server []Application `json:"server"`
KeyFile string `json:"key_file"`
RedisAddr string `json:"redis_addr"`
RedisPwd string `json:"redis_pwd"`
JWTCfg `json:"jwt"`
Server []Application `json:"server"`
}

var RtmpServercfg ServerCfg
Expand All @@ -54,9 +63,40 @@ func LoadConfig(configfilename string) error {
return err
}
log.Printf("get config json data:%v", RtmpServercfg)

Init()

return nil
}

func GetKeyFile() *string {
if len(RtmpServercfg.KeyFile) > 0 {
*roomKeySaveFile = RtmpServercfg.KeyFile
}

return roomKeySaveFile
}

func GetRedisAddr() *string {
if len(RtmpServercfg.RedisAddr) > 0 {
*RedisAddr = RtmpServercfg.RedisAddr
}

if len(*RedisAddr) == 0 {
return nil
}

return RedisAddr
}

func GetRedisPwd() *string {
if len(RtmpServercfg.RedisPwd) > 0 {
*RedisPwd = RtmpServercfg.RedisPwd
}

return RedisPwd
}

func CheckAppName(appname string) bool {
for _, app := range RtmpServercfg.Server {
if (app.Appname == appname) && (app.Liveon == "on") {
Expand Down
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ go 1.13
require (
github.com/auth0/go-jwt-middleware v0.0.0-20190805220309-36081240882b
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/go-redis/redis/v7 v7.2.0
github.com/gorilla/mux v1.7.4 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/labstack/echo/v4 v4.1.16 // indirect
github.com/orcaman/concurrent-map v0.0.0-20190826125027-8c72a8bb44f6
github.com/satori/go.uuid v1.2.0
github.com/smartystreets/goconvey v1.6.4 // indirect
github.com/stretchr/testify v1.4.0
github.com/urfave/negroni v1.0.0 // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
)
Loading

0 comments on commit ebac4bf

Please sign in to comment.