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

filters/auth: cache yaml config #3225

Merged
merged 2 commits into from
Sep 19, 2024
Merged

Conversation

AlexanderYastrebov
Copy link
Member

@AlexanderYastrebov AlexanderYastrebov commented Sep 11, 2024

Cache parsed yaml config for jwtMetrics and oauthTokeninfoValidate filters to avoid parsing the same configuration over and over when these filters are appended as default filters to all routes.

                          │     HEAD~1     │                 HEAD                 │
                          │     sec/op     │    sec/op     vs base                │
JwtMetrics_CreateFilter-8   30777.00n ± 6%   18.45n ± 11%  -99.94% (p=0.000 n=10)

                          │    HEAD~1    │                 HEAD                 │
                          │     B/op     │    B/op      vs base                 │
JwtMetrics_CreateFilter-8   24.33Ki ± 0%   0.00Ki ± 0%  -100.00% (p=0.000 n=10)

                          │   HEAD~1   │                HEAD                │
                          │ allocs/op  │ allocs/op  vs base                 │
JwtMetrics_CreateFilter-8   180.0 ± 0%    0.0 ± 0%  -100.00% (p=0.000 n=10)

@AlexanderYastrebov AlexanderYastrebov added minor no risk changes, for example new filters performance labels Sep 11, 2024
@AlexanderYastrebov AlexanderYastrebov force-pushed the filters/auth/yamlconfig branch 5 times, most recently from c6f326f to fab869c Compare September 11, 2024 14:06
@szuecs
Copy link
Member

szuecs commented Sep 12, 2024

👍

@@ -27,34 +28,31 @@ type (
)

func NewJwtMetrics() filters.Spec {
return &jwtMetricsSpec{}
return &jwtMetricsSpec{
yamlConfig: newYamlConfig[jwtMetricsFilter](64),
Copy link
Member Author

@AlexanderYastrebov AlexanderYastrebov Sep 12, 2024

Choose a reason for hiding this comment

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

64 is an arbitrary cache size here. In case number of distinct yaml configurations exceeds it the caching still has value because it will hold recently parsed configurations.

Copy link
Member Author

@AlexanderYastrebov AlexanderYastrebov Sep 12, 2024

Choose a reason for hiding this comment

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

Decided that currently it does not make sense to make it configurable, we can do it later if needed.

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
@AlexanderYastrebov AlexanderYastrebov force-pushed the filters/auth/yamlconfig branch 2 times, most recently from 42b81bd to c6228d1 Compare September 13, 2024 12:22
Cache parsed yaml config for `jwtMetrics` and `oauthTokeninfoValidate`
filters to avoid parsing the same configuration over and over
when these filters are appended as default filters to all routes.

```
                          │     HEAD~1     │                 HEAD                 │
                          │     sec/op     │    sec/op     vs base                │
JwtMetrics_CreateFilter-8   30777.00n ± 6%   18.45n ± 11%  -99.94% (p=0.000 n=10)

                          │    HEAD~1    │                 HEAD                 │
                          │     B/op     │    B/op      vs base                 │
JwtMetrics_CreateFilter-8   24.33Ki ± 0%   0.00Ki ± 0%  -100.00% (p=0.000 n=10)

                          │   HEAD~1   │                HEAD                │
                          │ allocs/op  │ allocs/op  vs base                 │
JwtMetrics_CreateFilter-8   180.0 ± 0%    0.0 ± 0%  -100.00% (p=0.000 n=10)
```

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
// 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
Copy link
Member

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?

Copy link
Member Author

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.

@MustafaSaber
Copy link
Member

👍

1 similar comment
@AlexanderYastrebov
Copy link
Member Author

👍

@AlexanderYastrebov AlexanderYastrebov merged commit 7b74d47 into master Sep 19, 2024
14 checks passed
@AlexanderYastrebov AlexanderYastrebov deleted the filters/auth/yamlconfig branch September 19, 2024 15:38
Pushpalanka pushed a commit that referenced this pull request Oct 11, 2024
* filters/auth: benchmark jwtMetrics filter creation

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>

* filters/auth: cache yaml config

Cache parsed yaml config for `jwtMetrics` and `oauthTokeninfoValidate`
filters to avoid parsing the same configuration over and over
when these filters are appended as default filters to all routes.

```
                          │     HEAD~1     │                 HEAD                 │
                          │     sec/op     │    sec/op     vs base                │
JwtMetrics_CreateFilter-8   30777.00n ± 6%   18.45n ± 11%  -99.94% (p=0.000 n=10)

                          │    HEAD~1    │                 HEAD                 │
                          │     B/op     │    B/op      vs base                 │
JwtMetrics_CreateFilter-8   24.33Ki ± 0%   0.00Ki ± 0%  -100.00% (p=0.000 n=10)

                          │   HEAD~1   │                HEAD                │
                          │ allocs/op  │ allocs/op  vs base                 │
JwtMetrics_CreateFilter-8   180.0 ± 0%    0.0 ± 0%  -100.00% (p=0.000 n=10)
```

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>

---------

Signed-off-by: Alexander Yastrebov <alexander.yastrebov@zalando.de>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor no risk changes, for example new filters performance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants