crusader c1de188784 ing
2017-08-21 19:23:45 +09:00

48 lines
964 B
Go

package redis
import (
"log"
"sync"
"time"
"git.loafle.net/overflow/overflow_gateway_websocket/pubsub"
"github.com/garyburd/redigo/redis"
)
type redisPubSub struct {
pool *redis.Pool
mtx sync.Mutex
}
func New(addr string) (pubsub.PubSub, error) {
r := &redisPubSub{}
r.pool = &redis.Pool{
MaxIdle: 3,
IdleTimeout: 240 * time.Second,
Dial: func() (redis.Conn, error) {
return redis.Dial("tcp", addr)
},
}
return r, nil
}
func (r *redisPubSub) Subscribe(channel string, cb pubsub.OnSubscribeFunc) {
conn := r.pool.Get()
defer conn.Close()
psc := redis.PubSubConn{Conn: conn}
psc.Subscribe(channel)
for {
switch v := psc.Receive().(type) {
case redis.Message:
log.Printf("message: %s: %s\n", v.Channel, string(v.Data))
case redis.Subscription:
log.Printf("subscription message: %s: %s %d\n", v.Channel, v.Kind, v.Count)
case error:
log.Println("error pub/sub, delivery has stopped")
return
default:
}
}
}