Skip to content

Make sure to call after hook on error #1479

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 11, 2020
Merged

Conversation

vmihailenco
Copy link
Collaborator

Fixes #1478

@kaka19ace please review.

for ; hookIndex < len(hs.hooks); hookIndex++ {
ctx, retErr = hs.hooks[hookIndex].BeforeProcess(ctx, cmd)
if retErr != nil {
cmd.SetErr(retErr)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

break will not trigger idx++

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but why that is a problem? I call AfterProcess for all functions except the last one that returned the error.

cmd.SetErr(err)
return err
if retErr == nil {
retErr = fn(ctx, cmd)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if retErr != nil -> cmd.SetErr(retErr)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cm.SetErr occurs in fn?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, cmd.SetErr is not needed here - fn sets the cmd.err

redis.go Outdated
var hookIndex int
var retErr error

for ; hookIndex < len(hs.hooks); hookIndex++ {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe
for ; retErr == nil && hookIndex < len(hs.hooks); hookIndex++
looks nice,
and idx-- is not required at afterprocess starts.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe with retErr == nil I need idx -= 2 if I don't want to include the hook that returned the error. Am I missing something?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example:
beforeProcess is stop at i'th idx (i>=0), afterProcess should starts at hooks[i], not hooks[i-1]

Copy link

@kaka19ace kaka19ace Sep 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

code:
we assume beforehook would stop at 5'th hook, all hooks=[0, 1, 2, 4, 5, 6, 7]

// before hook [0, 1, 2, 3, 4, 5]
for ;  hookIndex < len(hs.hooks); hookIndex++ {
    if err != nil { // err at 5'th hook
      break
   }
}

// hookIndex = 5

// after hook,  only executes hooks [4, 3, 2, 1], 5'th is lost.
for hookIndex--; hookIndex >= 0; hookIndex-- {
    // it will start from hookIndex=4, 5'th hook is lost.
}

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I intentionally skipped the AfterProcess hook that returned the error from BeforeProcess. Since I don't have strong preference I've changed the code to include it.

Please take another look.

Copy link

@kaka19ace kaka19ace Sep 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think afterhook focus on handling result and error.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

example:
there is a hook registered in hooks at i'th index,
beforehook is focus on authentification,
afterhook records log and emit metrics, the error msg is used in afterhook.

redis.go Outdated
ctx, err = h.BeforeProcess(ctx, cmd)
if err != nil {
return nil, err
for ; hookIndex >= 0; hookIndex-- {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

another issues:
if beforehooks run all success, hookIndex is equals to len(hooks),
afterhooks will be run out of range.

that's why
using
for ; err == nil && hookIndex < len(hs.hooks); hookIndex++
instead of
for ; hookIndex < len(hs.hooks); hookIndex++ is better.

you don't need to check hookIndex is out of range. :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right - fixed the code

@vmihailenco vmihailenco force-pushed the fix/hook-call-after branch 2 times, most recently from 8fe4c4b to c30c531 Compare September 10, 2020 13:43
redis.go Outdated
ctx, retErr = hs.hooks[hookIndex].BeforeProcessPipeline(ctx, cmds)
if retErr != nil {
setCmdsErr(cmds, retErr)
break

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

there is no need break here

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, removed

@vmihailenco vmihailenco merged commit b67982d into master Sep 11, 2020
@vmihailenco vmihailenco deleted the fix/hook-call-after branch September 11, 2020 06:25
@kaka19ace
Copy link

LGTM 👍

@vmihailenco
Copy link
Collaborator Author

@kaka19ace thanks for the review!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

afterhook could not excute when beforehook returns error
2 participants