-
Notifications
You must be signed in to change notification settings - Fork 351
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
filters/auth: cache yaml config #3225
Merged
Merged
Changes from all commits
Commits
Show all changes
2 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,84 @@ | ||
package auth | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/ghodss/yaml" | ||
) | ||
|
||
// yamlConfigParser parses and caches yaml configurations of type T. | ||
// Use [newYamlConfigParser] to create instances and ensure that *T implements [yamlConfig]. | ||
type yamlConfigParser[T any] struct { | ||
initialize func(*T) error | ||
cacheSize int | ||
cache map[string]*T | ||
} | ||
|
||
// yamlConfig must be implemented by config value pointer type. | ||
// It is used to initialize the value after parsing. | ||
type yamlConfig interface { | ||
initialize() error | ||
} | ||
|
||
// newYamlConfigParser creates a new parser with a given cache size. | ||
func newYamlConfigParser[T any, PT interface { | ||
*T | ||
yamlConfig | ||
}](cacheSize int) yamlConfigParser[T] { | ||
// We want user to specify config type T but ensure that *T implements [yamlConfig]. | ||
// | ||
// Type inference only works for functions but not for types | ||
// (see https://github.com/golang/go/issues/57270 and https://github.com/golang/go/issues/51527) | ||
// therefore we create instances using function with two type parameters | ||
// but second parameter is inferred from the first so the caller does not have to specify it. | ||
// | ||
// To use *T.initialize we setup initialize field | ||
return yamlConfigParser[T]{ | ||
initialize: func(v *T) error { return PT(v).initialize() }, | ||
cacheSize: cacheSize, | ||
cache: make(map[string]*T, cacheSize), | ||
} | ||
} | ||
|
||
// parseSingleArg calls [yamlConfigParser.parse] with the first string argument. | ||
// If args slice does not contain a single string, it returns an error. | ||
func (p *yamlConfigParser[T]) parseSingleArg(args []any) (*T, error) { | ||
if len(args) != 1 { | ||
return nil, fmt.Errorf("requires single string argument") | ||
} | ||
config, ok := args[0].(string) | ||
if !ok { | ||
return nil, fmt.Errorf("requires single string argument") | ||
} | ||
return p.parse(config) | ||
} | ||
|
||
// parse parses a yaml configuration or returns a cached value | ||
// if the exact configuration was already parsed before. | ||
// Returned value is shared by multiple callers and therefore must not be modified. | ||
func (p *yamlConfigParser[T]) parse(config string) (*T, error) { | ||
if v, ok := p.cache[config]; ok { | ||
return v, nil | ||
} | ||
|
||
v := new(T) | ||
if err := yaml.Unmarshal([]byte(config), v); err != nil { | ||
return nil, err | ||
} | ||
|
||
if err := p.initialize(v); err != nil { | ||
return nil, err | ||
} | ||
|
||
// evict random element if cache is full | ||
if p.cacheSize > 0 && len(p.cache) == p.cacheSize { | ||
for k := range p.cache { | ||
delete(p.cache, k) | ||
break | ||
} | ||
} | ||
|
||
p.cache[config] = v | ||
|
||
return v, nil | ||
} |
Oops, something went wrong.
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.
why do we need to enforce this?
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.
The idea is to ensure that user of yamlConfigParser is aware that config values must be immutable.