redis / rueidis

A fast Golang Redis client that supports Client Side Caching, Auto Pipelining, Generics OM, RedisJSON, RedisBloom, RediSearch, etc.
Apache License 2.0
2.44k stars 156 forks source link

Need ability to return a custom `RedisResult` from `rueidisHook` #527

Closed WilliamDeBruin closed 7 months ago

WilliamDeBruin commented 7 months ago

As title states, when wrapping a Rueidis client with a hook, the hook should be able to return a custom error from func (r rueidisHook) Do(...) and similar functions.

Consider this trivial example, modified from the rueidishook package:

type hook struct{
        protectedKey string
}

func (h *hook) Do(client rueidis.Client, ctx context.Context, cmd rueidis.Completed) (resp rueidis.RedisResult) {
    commands := cmd.Commands()

        if commands[0] == "set" && commands[1] == h.protectedKey {
                 // should return an error trying to set protected key
                ...
        }

    resp = client.Do(ctx, cmd)
    // do whatever you want after client.Do
    return
}

What is the recommended strategy to pass a useful message to the caller in this case?

rueian commented 7 months ago

Hi @WilliamDeBruin, thank you for requesting this. Indeed, we need this functionality in rueidishook. We have done this in the mock package: https://github.com/redis/rueidis/blob/6a8ba489f1974430cd5032e72a65a2010bf68601/mock/result.go#L22-L25

I think we can copy the mock.ErrorResult to rueidishook as well.

WilliamDeBruin commented 7 months ago

Yes that would be super helpful! Thanks for the quick response!