Skip to content
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

feat: support passing context #126

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mingrammer
Copy link
Contributor

@mingrammer mingrammer commented Nov 30, 2020

Sometimes, we can run an operation that takes longer than the timeout of the incoming request. In this case, the whole request should be canceled along with all subtasks including the cache operations. But current implementation doesn't support passing context, so we can't cancel the cache operations even if the current context is expired.

So I added the passing context feature for all cache operations with this PR. It keeps the backward compatibility, thus does not affect existing code using gomemcache.

import (
    "github.com/bradfitz/gomemcache/memcache"
)
func main() {
    mc := memcache.New("10.0.0.1:11211", "10.0.0.2:11211", "10.0.0.3:11212")
    mc.Set(&memcache.Item{Key: "foo", Value: []byte("my value")})
    it, err := mc.Get("foo")

    // With context
    ctx, cancel := context.WithTimeout(2 * time.Second)
    doLongTimeJob(ctx)
    ...
    it, err = mc.GetWithContext(ctx, "bar")
    ...
}

I would appreciate it if you think about this feature in a good way.

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.

3 participants