Last active
February 12, 2022 13:39
-
-
Save debugger22/0fc37e8575feb0418535c9efee5daa50 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func startRedisListener() { | |
ctx, cancelFn = context.WithCancel(context.Background()) | |
defer cancelFn() | |
onUserChannelMessage(ctx, r, w) | |
for { | |
// ...do usual stuff | |
} | |
} | |
func onUserChannelMessage(ctx context.Context, r <-chan *redis.Message, w chan string) { | |
go func() { | |
for { | |
select{ | |
case m, ok := <-r: | |
if !ok{ | |
return | |
} | |
w <- m.Payload | |
// ctx.Done returns a channel which is closed when cancel function of that context is called | |
case <-ctx.Done(): | |
return | |
} | |
} | |
}() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment