Closed chenjie199234 closed 2 years ago
You could use DoContext
with a cancellable context to enable this flow.
p := &redis.Pool{}
c := p.Get()
defer p.Close()
cc, ok := c.(redis.ConnWithContext)
if !ok {
// TODO: Handle
return errors.New("connection doesn't support context")
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
defer cancel()
time.Sleep(time.Second)
}()
v, err := cc.DoContext(ctx, "BLPOP","testlist",0)
if err != nil {
if errors.Is(err, context.Canceled) {
// TODO: Handle cancelled
fmt.Println(err)
}
return err
}
// TODO: process data
fmt.Println(v)
example