diff --git a/config.go b/config.go index 2e8c1bb5..4a53fd6a 100644 --- a/config.go +++ b/config.go @@ -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{ diff --git a/internal/network/limiter.go b/internal/network/limiter.go index 0c3b4325..49288119 100644 --- a/internal/network/limiter.go +++ b/internal/network/limiter.go @@ -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 @@ -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)) +} diff --git a/internal/network/limiter_test.go b/internal/network/limiter_test.go index 2d6b42a0..032e9c36 100644 --- a/internal/network/limiter_test.go +++ b/internal/network/limiter_test.go @@ -1,7 +1,9 @@ package network import ( + "reflect" "testing" + "time" "golang.org/x/time/rate" ) @@ -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) + } + }) + } +}