Skip to content

Instantly share code, notes, and snippets.

@debugger22
Last active February 12, 2022 13:39
Show Gist options
  • Save debugger22/0fc37e8575feb0418535c9efee5daa50 to your computer and use it in GitHub Desktop.
Save debugger22/0fc37e8575feb0418535c9efee5daa50 to your computer and use it in GitHub Desktop.
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