mirror of
https://github.com/redis/go-redis.git
synced 2025-04-19 07:22:17 +03:00
Properly propagate context error
This commit is contained in:
parent
eda1f9c6ad
commit
297e671f5e
53
redis.go
53
redis.go
@ -49,13 +49,13 @@ func (hs hooks) process(
|
|||||||
ctx context.Context, cmd Cmder, fn func(context.Context, Cmder) error,
|
ctx context.Context, cmd Cmder, fn func(context.Context, Cmder) error,
|
||||||
) error {
|
) error {
|
||||||
if len(hs.hooks) == 0 {
|
if len(hs.hooks) == 0 {
|
||||||
return hs.withContext(ctx, func() error {
|
err, canceled := hs.withContext(ctx, func() error {
|
||||||
err := fn(ctx, cmd)
|
return fn(ctx, cmd)
|
||||||
if err != nil {
|
})
|
||||||
|
if canceled {
|
||||||
cmd.SetErr(err)
|
cmd.SetErr(err)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var hookIndex int
|
var hookIndex int
|
||||||
@ -69,13 +69,13 @@ func (hs hooks) process(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if retErr == nil {
|
if retErr == nil {
|
||||||
retErr = hs.withContext(ctx, func() error {
|
var canceled bool
|
||||||
err := fn(ctx, cmd)
|
retErr, canceled = hs.withContext(ctx, func() error {
|
||||||
if err != nil {
|
return fn(ctx, cmd)
|
||||||
cmd.SetErr(err)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
})
|
})
|
||||||
|
if canceled {
|
||||||
|
cmd.SetErr(retErr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for hookIndex--; hookIndex >= 0; hookIndex-- {
|
for hookIndex--; hookIndex >= 0; hookIndex-- {
|
||||||
@ -92,13 +92,13 @@ func (hs hooks) processPipeline(
|
|||||||
ctx context.Context, cmds []Cmder, fn func(context.Context, []Cmder) error,
|
ctx context.Context, cmds []Cmder, fn func(context.Context, []Cmder) error,
|
||||||
) error {
|
) error {
|
||||||
if len(hs.hooks) == 0 {
|
if len(hs.hooks) == 0 {
|
||||||
return hs.withContext(ctx, func() error {
|
err, canceled := hs.withContext(ctx, func() error {
|
||||||
err := fn(ctx, cmds)
|
return fn(ctx, cmds)
|
||||||
if err != nil {
|
})
|
||||||
|
if canceled {
|
||||||
setCmdsErr(cmds, err)
|
setCmdsErr(cmds, err)
|
||||||
}
|
}
|
||||||
return err
|
return err
|
||||||
})
|
|
||||||
}
|
}
|
||||||
|
|
||||||
var hookIndex int
|
var hookIndex int
|
||||||
@ -112,13 +112,13 @@ func (hs hooks) processPipeline(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if retErr == nil {
|
if retErr == nil {
|
||||||
retErr = hs.withContext(ctx, func() error {
|
var canceled bool
|
||||||
err := fn(ctx, cmds)
|
retErr, canceled = hs.withContext(ctx, func() error {
|
||||||
if err != nil {
|
return fn(ctx, cmds)
|
||||||
setCmdsErr(cmds, err)
|
|
||||||
}
|
|
||||||
return err
|
|
||||||
})
|
})
|
||||||
|
if canceled {
|
||||||
|
setCmdsErr(cmds, retErr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for hookIndex--; hookIndex >= 0; hookIndex-- {
|
for hookIndex--; hookIndex >= 0; hookIndex-- {
|
||||||
@ -138,19 +138,20 @@ func (hs hooks) processTxPipeline(
|
|||||||
return hs.processPipeline(ctx, cmds, fn)
|
return hs.processPipeline(ctx, cmds, fn)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hs hooks) withContext(ctx context.Context, fn func() error) error {
|
func (hs hooks) withContext(ctx context.Context, fn func() error) (_ error, canceled bool) {
|
||||||
if ctx.Done() == nil {
|
done := ctx.Done()
|
||||||
return fn()
|
if done == nil {
|
||||||
|
return fn(), false
|
||||||
}
|
}
|
||||||
|
|
||||||
errc := make(chan error, 1)
|
errc := make(chan error, 1)
|
||||||
go func() { errc <- fn() }()
|
go func() { errc <- fn() }()
|
||||||
|
|
||||||
select {
|
select {
|
||||||
case <-ctx.Done():
|
case <-done:
|
||||||
return ctx.Err()
|
return ctx.Err(), true
|
||||||
case err := <-errc:
|
case err := <-errc:
|
||||||
return err
|
return err, false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user