Skip to content

Commit

Permalink
add context cancelation support for blocking operations
Browse files Browse the repository at this point in the history
  • Loading branch information
GreenHedgehog authored and vmihailenco committed Sep 17, 2020
1 parent 4b9d6df commit eda1f9c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
48 changes: 44 additions & 4 deletions redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,13 @@ func (hs hooks) process(
ctx context.Context, cmd Cmder, fn func(context.Context, Cmder) error,
) error {
if len(hs.hooks) == 0 {
return fn(ctx, cmd)
return hs.withContext(ctx, func() error {
err := fn(ctx, cmd)
if err != nil {
cmd.SetErr(err)
}
return err
})
}

var hookIndex int
Expand All @@ -63,7 +69,13 @@ func (hs hooks) process(
}

if retErr == nil {
retErr = fn(ctx, cmd)
retErr = hs.withContext(ctx, func() error {
err := fn(ctx, cmd)
if err != nil {
cmd.SetErr(err)
}
return err
})
}

for hookIndex--; hookIndex >= 0; hookIndex-- {
Expand All @@ -80,7 +92,13 @@ func (hs hooks) processPipeline(
ctx context.Context, cmds []Cmder, fn func(context.Context, []Cmder) error,
) error {
if len(hs.hooks) == 0 {
return fn(ctx, cmds)
return hs.withContext(ctx, func() error {
err := fn(ctx, cmds)
if err != nil {
setCmdsErr(cmds, err)
}
return err
})
}

var hookIndex int
Expand All @@ -94,7 +112,13 @@ func (hs hooks) processPipeline(
}

if retErr == nil {
retErr = fn(ctx, cmds)
retErr = hs.withContext(ctx, func() error {
err := fn(ctx, cmds)
if err != nil {
setCmdsErr(cmds, err)
}
return err
})
}

for hookIndex--; hookIndex >= 0; hookIndex-- {
Expand All @@ -114,6 +138,22 @@ func (hs hooks) processTxPipeline(
return hs.processPipeline(ctx, cmds, fn)
}

func (hs hooks) withContext(ctx context.Context, fn func() error) error {
if ctx.Done() == nil {
return fn()
}

errc := make(chan error, 1)
go func() { errc <- fn() }()

select {
case <-ctx.Done():
return ctx.Err()
case err := <-errc:
return err
}
}

//------------------------------------------------------------------------------

type baseClient struct {
Expand Down
25 changes: 25 additions & 0 deletions redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,3 +389,28 @@ var _ = Describe("Client OnConnect", func() {
Expect(name).To(Equal("on_connect"))
})
})

var _ = Describe("Client context cancelation", func() {
var opt *redis.Options
var client *redis.Client

BeforeEach(func() {
opt = redisOptions()
opt.ReadTimeout = -1
opt.WriteTimeout = -1
client = redis.NewClient(opt)
})

AfterEach(func() {
Expect(client.Close()).NotTo(HaveOccurred())
})

It("Blocking operation cancelation", func() {
ctx, cancel := context.WithCancel(ctx)
cancel()

err := client.BLPop(ctx, 1*time.Second, "test").Err()
Expect(err).To(HaveOccurred())
Expect(err).To(BeIdenticalTo(context.Canceled))
})
})

0 comments on commit eda1f9c

Please sign in to comment.