Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: redis conn bug #434

Merged
merged 1 commit into from
Sep 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 24 additions & 4 deletions pkg/exchange/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ func NewRedisExchange(cfg Config) (*redisExchange, error) {
}

var connFunc radix.ConnFunc

if len(cfg.Clusters) > 0 {
cluster, err := radix.NewCluster(cfg.Clusters)
if err != nil {
Expand Down Expand Up @@ -147,6 +146,11 @@ func (exc *redisExchange) JoinRoom(receiveChan chan<- model.RoomMessage, roomId
if !exc.checkRoomExist(roomId) {
return nil, fmt.Errorf("no redisRoom")
}
conn, err := exc.connFunc("", "")
if err != nil {
logger.Errorf("Create redis conn for room %s err: %s", roomId, err)
return nil, err
}
redisMsgCh := make(chan radix.PubSubMessage)
go func() {
for redisMsg := range redisMsgCh {
Expand All @@ -158,7 +162,7 @@ func (exc *redisExchange) JoinRoom(receiveChan chan<- model.RoomMessage, roomId
logger.Infof("Joined Room %s stop receive message", roomId)
}()

pubSub := radix.PersistentPubSub("", "", exc.connFunc)
pubSub := radix.PubSub(conn)
writeChannel := fmt.Sprintf("%s.write", roomId)
readChannel := fmt.Sprintf("%s.read", roomId)
s := &redisRoom{
Expand Down Expand Up @@ -224,6 +228,14 @@ func (exc *redisExchange) DestroyRoom(exRoom Room) {
func (exc *redisExchange) CreateRoom(receiveChan chan<- model.RoomMessage, roomId string) Room {
exc.mu.Lock()
defer exc.mu.Unlock()
conn, err := exc.connFunc("", "")
if err != nil {
logger.Errorf("Create redis conn for room %s err: %s", roomId, err)
return &fakeRoom{
roomId: roomId,
err: err,
}
}
redisMsgCh := make(chan radix.PubSubMessage)
go func() {
for redisMsg := range redisMsgCh {
Expand All @@ -233,8 +245,7 @@ func (exc *redisExchange) CreateRoom(receiveChan chan<- model.RoomMessage, roomI
}
logger.Infof("Redis room %s stop receive message", roomId)
}()

pubSub := radix.PersistentPubSub("", "", exc.connFunc)
pubSub := radix.PubSub(conn)
writeChannel := fmt.Sprintf("%s.read", roomId)
readChannel := fmt.Sprintf("%s.write", roomId)
r := &redisRoom{
Expand Down Expand Up @@ -285,3 +296,12 @@ func (s *redisRoom) Publish(msg model.RoomMessage) {
logger.Errorf("Redis publish message err: %s", err)
}
}

type fakeRoom struct {
roomId string
err error
}

func (f *fakeRoom) Publish(msg model.RoomMessage) {
logger.Errorf("Fake room %s discard message as err %s", f.roomId, f.err)
}