Skip to content

Commit

Permalink
Optimise limits
Browse files Browse the repository at this point in the history
  • Loading branch information
rusq committed Mar 30, 2024
1 parent 0c89921 commit 7526e03
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
6 changes: 3 additions & 3 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ var DefLimits = Limits{
DownloadRetries: 3, // this shouldn't even happen, as we have no limiter on files download.
Tier2: TierLimit{
Boost: 20, // seems to work fine with this boost
Burst: 1, // limiter will wait indefinitely if it is less than 1.
Burst: 3, // limiter will wait indefinitely if it is less than 1.
Retries: 20, // see issue #28, sometimes slack is being difficult
},
Tier3: TierLimit{
Boost: 120, // playing safe there, but generally value of 120 is fine.
Burst: 1, // safe value, who would ever want to modify it? I don't know.
Burst: 5, // safe value, who would ever want to modify it? I don't know.
Retries: 3, // on Tier 3 this was never a problem, even with limiter-boost=120
},
Tier4: TierLimit{
Boost: 10,
Burst: 1,
Burst: 7,
Retries: 3,
},
Request: RequestLimit{
Expand Down
17 changes: 10 additions & 7 deletions internal/network/limiter.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package network

import "golang.org/x/time/rate"
import (
"time"

"golang.org/x/time/rate"
)

// Tier represents rate limit Tier:
// https://api.slack.com/docs/rate-limits
Expand All @@ -14,16 +18,15 @@ const (
Tier2 Tier = 20
Tier3 Tier = 50
Tier4 Tier = 100

// secPerMin is the number of seconds in a minute, it is here to allow easy
// modification of the program, should this value change.
secPerMin = 60.0
)

// NewLimiter returns throttler with rateLimit requests per minute.
// optionally caller may specify the boost
func NewLimiter(t Tier, burst uint, boost int) *rate.Limiter {
callsPerSec := float64(int(t)+boost) / secPerMin
l := rate.NewLimiter(rate.Limit(callsPerSec), int(burst))
l := rate.NewLimiter(rate.Every(every(t, boost)), int(burst))
return l
}

func every(t Tier, boost int) time.Duration {
return time.Minute / time.Duration(int(t)+int(boost))
}
30 changes: 30 additions & 0 deletions internal/network/limiter_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package network

import (
"reflect"
"testing"
"time"

"golang.org/x/time/rate"
)
Expand Down Expand Up @@ -36,3 +38,31 @@ func TestNewLimiter(t *testing.T) {
})
}
}

func Test_every(t *testing.T) {
type args struct {
t Tier
boost int
}
tests := []struct {
name string
args args
want time.Duration
}{
{
name: "tier 2",
args: args{
t: Tier2,
boost: 0,
},
want: time.Minute / 20,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := every(tt.args.t, tt.args.boost); !reflect.DeepEqual(got, tt.want) {
t.Errorf("every() = %s, want %s", got, tt.want)
}
})
}
}

0 comments on commit 7526e03

Please sign in to comment.