Skip to content

Commit

Permalink
fix: avoid spurious rate limit
Browse files Browse the repository at this point in the history
Previously, group data retrieved from in-memory cache was counted
towards the keycloak API rate limit.

With this change, data retrieved from cache does not count towards the
rate limit because the cache does not actually hit the keycloak API.
  • Loading branch information
smlx committed Aug 6, 2024
1 parent dda6f2e commit 6d773d0
Showing 1 changed file with 3 additions and 4 deletions.
7 changes: 3 additions & 4 deletions internal/keycloak/groups.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,15 +55,14 @@ func (c *Client) rawGroups(ctx context.Context) ([]byte, error) {
func (c *Client) GroupNameGroupIDMap(
ctx context.Context,
) (map[string]string, error) {
// rate limit keycloak API access
if err := c.limiter.Wait(ctx); err != nil {
return nil, fmt.Errorf("couldn't wait for limiter: %v", err)
}
// prefer to use cached value
if groupNameGroupIDMap, ok := c.groupCache.Get(); ok {
return groupNameGroupIDMap, nil
}
// otherwise get data from keycloak
if err := c.limiter.Wait(ctx); err != nil {
return nil, fmt.Errorf("couldn't wait for limiter: %v", err)
}
data, err := c.rawGroups(ctx)
if err != nil {
return nil, fmt.Errorf("couldn't get groups from Keycloak API: %v", err)
Expand Down

0 comments on commit 6d773d0

Please sign in to comment.