overflow_subscriber/redis/subscribers.go

163 lines
3.0 KiB
Go
Raw Normal View History

2017-09-01 06:13:18 +00:00
package redis
import (
2017-09-27 08:07:34 +00:00
"encoding/json"
"fmt"
2017-11-09 09:39:21 +00:00
"sync"
2017-09-01 06:13:18 +00:00
2017-09-27 08:07:34 +00:00
"git.loafle.net/commons_go/logging"
2017-11-09 09:39:21 +00:00
cuc "git.loafle.net/commons_go/util/channel"
oos "git.loafle.net/overflow/overflow_subscriber"
2017-09-01 06:13:18 +00:00
"github.com/garyburd/redigo/redis"
)
type channelAction struct {
2017-11-09 09:39:21 +00:00
cuc.Action
h oos.SubscriberHandler
2017-09-01 06:13:18 +00:00
}
type subscribers struct {
conn redis.PubSubConn
2017-11-09 09:39:21 +00:00
subHandlers map[string]oos.SubscriberHandler
2017-09-01 06:13:18 +00:00
isListen bool
subCh chan channelAction
2017-11-09 09:39:21 +00:00
stopChan chan struct{}
stopWg sync.WaitGroup
2017-09-01 06:13:18 +00:00
}
2017-11-09 09:39:21 +00:00
func New(conn redis.Conn) oos.Subscriber {
2017-09-01 06:13:18 +00:00
s := &subscribers{
2017-11-09 09:39:21 +00:00
isListen: false,
2017-09-01 06:13:18 +00:00
}
s.conn = redis.PubSubConn{Conn: conn}
2017-11-09 09:39:21 +00:00
return s
}
func (s *subscribers) Start() error {
if s.stopChan != nil {
panic("Redis Subscriber: subscriber is already running. Stop it before starting it again")
}
2017-09-01 06:13:18 +00:00
2017-11-09 09:39:21 +00:00
s.stopChan = make(chan struct{})
s.subHandlers = make(map[string]oos.SubscriberHandler)
s.subCh = make(chan channelAction)
2017-09-01 06:13:18 +00:00
2017-11-09 09:39:21 +00:00
s.stopWg.Add(1)
go handleSubscriber(s)
return nil
}
func (s *subscribers) Stop() {
if s.stopChan == nil {
panic("Redis Subscriber: subscriber must be started before stopping it")
}
close(s.stopChan)
s.stopWg.Wait()
s.stopChan = nil
}
func (s *subscribers) Subscribe(h oos.SubscriberHandler) error {
if _, ok := s.subHandlers[h.GetChannel()]; ok {
return oos.ChannelExistError{Channel: h.GetChannel()}
}
ca := channelAction{
h: h,
}
ca.Type = cuc.ActionTypeCreate
s.subCh <- ca
return nil
2017-09-01 06:13:18 +00:00
}
2017-11-09 09:39:21 +00:00
func (s *subscribers) Unsubscribe(h oos.SubscriberHandler) error {
if _, ok := s.subHandlers[h.GetChannel()]; !ok {
return oos.ChannelIsNotExistError{Channel: h.GetChannel()}
}
ca := channelAction{
h: h,
}
ca.Type = cuc.ActionTypeDelete
s.subCh <- ca
return nil
}
func handleSubscriber(s *subscribers) {
defer s.stopWg.Done()
2017-09-01 06:13:18 +00:00
for {
select {
case ca := <-s.subCh:
switch ca.Type {
2017-11-09 09:39:21 +00:00
case cuc.ActionTypeCreate:
2017-09-01 06:13:18 +00:00
s.subHandlers[ca.h.GetChannel()] = ca.h
s.conn.Subscribe(ca.h.GetChannel())
2017-11-09 09:39:21 +00:00
listenSubscriptions(s)
2017-09-01 06:13:18 +00:00
break
2017-11-09 09:39:21 +00:00
case cuc.ActionTypeDelete:
2017-09-01 06:13:18 +00:00
s.conn.Unsubscribe(ca.h.GetChannel())
delete(s.subHandlers, ca.h.GetChannel())
break
}
2017-11-09 09:39:21 +00:00
case <-s.stopChan:
s.conn.Close()
2017-09-01 06:13:18 +00:00
return
}
}
}
2017-11-09 09:39:21 +00:00
func listenSubscriptions(s *subscribers) {
2017-09-01 06:13:18 +00:00
if s.isListen {
return
}
2017-11-09 09:39:21 +00:00
s.stopWg.Add(1)
2017-09-01 06:13:18 +00:00
go func() {
2017-11-09 09:39:21 +00:00
defer s.stopWg.Done()
2017-09-01 06:13:18 +00:00
for {
switch v := s.conn.Receive().(type) {
case redis.Message:
if h, ok := s.subHandlers[v.Channel]; ok {
2017-11-09 09:39:21 +00:00
if message, err := unmarshalMessage(v.Data); nil != err {
logging.Logger().Error(fmt.Sprintf("Subscriber Unmarshal error:%v", err))
2017-09-27 08:07:34 +00:00
break
2017-09-27 08:30:32 +00:00
} else {
h.OnSubscribe(v.Channel, message)
2017-09-27 08:07:34 +00:00
}
2017-09-01 06:13:18 +00:00
}
break
case redis.Subscription:
break
case error:
2017-11-09 09:39:21 +00:00
s.Stop()
2017-09-01 06:13:18 +00:00
return
default:
}
}
}()
s.isListen = true
}
2017-11-09 09:39:21 +00:00
func unmarshalMessage(data []byte) (oos.SubscribeMessage, error) {
2017-09-27 08:30:32 +00:00
var err error
2017-11-09 09:39:21 +00:00
var message oos.SubscribeMessage
2017-09-27 08:30:32 +00:00
if err = json.Unmarshal(data, &message); nil != err {
return message, err
}
if message.Message, err = message.MessageRaw.MarshalJSON(); nil != err {
return message, err
}
return message, nil
}