-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
flightcontrol: add cachedgroup struct #5022
Merged
AkihiroSuda
merged 3 commits into
moby:master
from
tonistiigi:flightcontrol-cachedgroup
Jun 19, 2024
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package flightcontrol | ||
|
||
import ( | ||
"context" | ||
"sync" | ||
|
||
"github.com/pkg/errors" | ||
) | ||
|
||
// Group is a flightcontrol synchronization group that memoizes the results of a function | ||
// and returns the cached result if the function is called with the same key. | ||
// Don't use with long-running groups as the results are cached indefinitely. | ||
type CachedGroup[T any] struct { | ||
// CacheError defines if error results should also be cached. | ||
// It is not safe to change this value after the first call to Do. | ||
// Context cancellation errors are never cached. | ||
CacheError bool | ||
g Group[T] | ||
mu sync.Mutex | ||
cache map[string]result[T] | ||
} | ||
|
||
type result[T any] struct { | ||
v T | ||
err error | ||
} | ||
|
||
// Do executes a context function syncronized by the key or returns the cached result for the key. | ||
func (g *CachedGroup[T]) Do(ctx context.Context, key string, fn func(ctx context.Context) (T, error)) (T, error) { | ||
return g.g.Do(ctx, key, func(ctx context.Context) (T, error) { | ||
g.mu.Lock() | ||
if v, ok := g.cache[key]; ok { | ||
g.mu.Unlock() | ||
if v.err != nil { | ||
if g.CacheError { | ||
return v.v, v.err | ||
} | ||
} else { | ||
return v.v, nil | ||
} | ||
} | ||
g.mu.Unlock() | ||
v, err := fn(ctx) | ||
if err != nil { | ||
select { | ||
case <-ctx.Done(): | ||
if errors.Is(err, context.Cause(ctx)) { | ||
return v, err | ||
} | ||
default: | ||
} | ||
} | ||
if err == nil || g.CacheError { | ||
g.mu.Lock() | ||
if g.cache == nil { | ||
g.cache = make(map[string]result[T]) | ||
} | ||
g.cache[key] = result[T]{v: v, err: err} | ||
g.mu.Unlock() | ||
} | ||
return v, err | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package flightcontrol | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/pkg/errors" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestCached(t *testing.T) { | ||
var g CachedGroup[int] | ||
|
||
ctx := context.TODO() | ||
|
||
v, err := g.Do(ctx, "11", func(ctx context.Context) (int, error) { | ||
return 1, nil | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, v) | ||
|
||
v, err = g.Do(ctx, "22", func(ctx context.Context) (int, error) { | ||
return 2, nil | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, 2, v) | ||
|
||
didCall := false | ||
v, err = g.Do(ctx, "11", func(ctx context.Context) (int, error) { | ||
didCall = true | ||
return 3, nil | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, 1, v) | ||
require.Equal(t, false, didCall) | ||
|
||
// by default, errors are not cached | ||
_, err = g.Do(ctx, "33", func(ctx context.Context) (int, error) { | ||
return 0, errors.Errorf("some error") | ||
}) | ||
|
||
require.Error(t, err) | ||
require.ErrorContains(t, err, "some error") | ||
|
||
v, err = g.Do(ctx, "33", func(ctx context.Context) (int, error) { | ||
return 3, nil | ||
}) | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, 3, v) | ||
} | ||
|
||
func TestCachedError(t *testing.T) { | ||
var g CachedGroup[string] | ||
g.CacheError = true | ||
|
||
ctx := context.TODO() | ||
|
||
_, err := g.Do(ctx, "11", func(ctx context.Context) (string, error) { | ||
return "", errors.Errorf("first error") | ||
}) | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, "first error") | ||
|
||
_, err = g.Do(ctx, "11", func(ctx context.Context) (string, error) { | ||
return "never-ran", nil | ||
}) | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, "first error") | ||
|
||
// context errors are never cached | ||
ctx, cancel := context.WithTimeoutCause(context.TODO(), 10*time.Millisecond, nil) | ||
defer cancel() | ||
_, err = g.Do(ctx, "22", func(ctx context.Context) (string, error) { | ||
select { | ||
case <-ctx.Done(): | ||
return "", context.Cause(ctx) | ||
case <-time.After(10 * time.Second): | ||
return "", errors.Errorf("unexpected error") | ||
} | ||
}) | ||
require.Error(t, err) | ||
require.ErrorContains(t, err, "context deadline exceeded") | ||
|
||
select { | ||
case <-ctx.Done(): | ||
default: | ||
require.Fail(t, "expected context to be done") | ||
} | ||
|
||
v, err := g.Do(ctx, "22", func(ctx context.Context) (string, error) { | ||
return "did-run", nil | ||
}) | ||
require.NoError(t, err) | ||
require.Equal(t, "did-run", v) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: this line does not seem needed, as it is covered by the ErrorContains below