Skip to content

Conversation

@OlegErshov
Copy link
Contributor

On-behalf-of: SAP [email protected]

Copy link
Contributor

@aaronschweig aaronschweig left a comment

Choose a reason for hiding this comment

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

Left some question and review suggestions

Comment on lines 35 to 57
func (r *StaticThenExponentialRateLimiter[T]) When(item T) time.Duration {
r.failuresLock.Lock()
defer r.failuresLock.Unlock()

now := r.clock.Now()

first, exists := r.firstAttempt[item]
if !exists {
first = now
r.firstAttempt[item] = first
}

timeSinceFirst := now.Sub(first)
if timeSinceFirst <= r.staticWindow {
return r.staticDelay
}

return r.exponential.When(item)
}
Copy link
Contributor

@aaronschweig aaronschweig Nov 20, 2025

Choose a reason for hiding this comment

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

I would appreciate a bit of a differentiation between the usage of read and write locks in here.

Especially since in this read case now, you are blocking the whole execution of other reads and writes. It would be good to only set the right type of lock where needed, and only set it as shortly as needed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure, thanks, it's done


first, exists := r.firstAttempt[item]
if !exists {
first = now
Copy link
Contributor

Choose a reason for hiding this comment

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

I am not quite sure why this assignment is needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I've double checked the logic, and updated it. Currently the rate limiter will return the static delay for the first run and this assignment is not needed anymore


type StaticThenExponentialRateLimiter[T comparable] struct {
failuresLock sync.Mutex
firstAttempt map[T]time.Time
Copy link
Contributor

Choose a reason for hiding this comment

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

Not quite sure I agree with the naming. I rather have something like staticAttempts or something similiar, as it cannot only hold the first attempt but rather as many attempts as needed until the staticWindow limitation is needed

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I thought that we should only store the first attempt and then check how long it has been since the first attempt and if this time has reached the staticWindow duration, then we should fall into exponential backoff. Does this make sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

I agree with the implementation, I just don't like the naming 😉

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah, sorry, I misunderstood

@pteich
Copy link
Contributor

pteich commented Nov 21, 2025

I had also implemented rate limiting in a way less sophisticated way by back-porting what we did in metadata-operator in this branch: https://github.com/platform-mesh/golang-commons/tree/feat/rate-limiter and also applied it to our operator in a draft PR https://github.tools.sap/dxp/metadata-registry-operator/pull/1522

Not sure if we can join our forces or just skip mine in favor of what you are doing here. It is definitely better to have more options than my static time limiting approach :)

@nexus49 nexus49 force-pushed the feat/custom-rate-limiter branch from 6dd7f00 to dd78274 Compare November 21, 2025 16:16
@OlegErshov OlegErshov marked this pull request as ready for review November 24, 2025 11:16
@OlegErshov OlegErshov requested review from a team as code owners November 24, 2025 11:16
@nexus49 nexus49 merged commit 752be1c into main Nov 25, 2025
10 checks passed
@nexus49 nexus49 deleted the feat/custom-rate-limiter branch November 25, 2025 08:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants