Skip to content

Commit 2acc13e

Browse files
Add Permanent Throttle Option (#170)
Update freno to support infinite TTL
1 parent d89b112 commit 2acc13e

File tree

4 files changed

+26
-16
lines changed

4 files changed

+26
-16
lines changed

doc/mysql-backend.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ CREATE TABLE service_election (
7878
CREATE TABLE throttled_apps (
7979
app_name varchar(128) NOT NULL,
8080
throttled_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
81-
expires_at TIMESTAMP NOT NULL,
81+
expires_at TIMESTAMP,
8282
ratio DOUBLE,
8383
PRIMARY KEY (app_name)
8484
);

pkg/group/mysql.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ service_id varchar(128) NOT NULL,
2424
CREATE TABLE throttled_apps (
2525
app_name varchar(128) NOT NULL,
2626
throttled_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
27-
expires_at TIMESTAMP NOT NULL,
27+
expires_at TIMESTAMP,
2828
ratio DOUBLE,
2929
PRIMARY KEY (app_name)
3030
);
@@ -346,7 +346,7 @@ func (backend *MySQLBackend) readThrottledApps() error {
346346
query := `
347347
select
348348
app_name,
349-
timestampdiff(second, now(), expires_at) as ttl_seconds,
349+
timestampdiff(second, now(), coalesce(expires_at,now())) as ttl_seconds,
350350
ratio
351351
from
352352
throttled_apps
@@ -357,6 +357,9 @@ func (backend *MySQLBackend) readThrottledApps() error {
357357
ttlSeconds := m.GetInt64("ttl_seconds")
358358
ratio, _ := strconv.ParseFloat(m.GetString("ratio"), 64)
359359
expiresAt := time.Now().Add(time.Duration(ttlSeconds) * time.Second)
360+
if ttlSeconds == 0 {
361+
expiresAt = time.Time{}
362+
}
360363

361364
go log.Debugf("read-throttled-apps: app=%s, ttlSeconds%+v, expiresAt=%+v, ratio=%+v", appName, ttlSeconds, expiresAt, ratio)
362365
go backend.throttler.ThrottleApp(appName, expiresAt, ratio)
@@ -398,18 +401,19 @@ func (backend *MySQLBackend) ThrottleApp(appName string, ttlMinutes int64, expir
398401
`
399402
args = sqlutils.Args(appName, ttlMinutes, ratio)
400403
} else {
401-
// TTL=0 ; if app is already throttled, keep existing TTL and only update ratio.
402-
// if app does not exist use DefaultThrottleTTL
404+
// TTL=0 ; if app is already throttled, update throttle to infinity and update ratio.
405+
// if app does not exist, TTL should be infinite
403406
query = `
404407
insert into throttled_apps (
405-
app_name, throttled_at, expires_at, ratio
408+
app_name, throttled_at, ratio
406409
) values (
407-
?, now(), now() + interval ? minute, ?
410+
?, now(), ?
408411
)
409412
on duplicate key update
410-
ratio=values(ratio)
413+
ratio=values(ratio),
414+
expires_at=null
411415
`
412-
args = sqlutils.Args(appName, throttle.DefaultThrottleTTLMinutes, ratio)
416+
args = sqlutils.Args(appName, ratio)
413417
}
414418
_, err := sqlutils.ExecNoPrepare(backend.db, query, args...)
415419
backend.throttler.ThrottleApp(appName, expireAt, ratio)

pkg/throttle/throttler.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ const skippedHostsSnapshotInterval = 5 * time.Second
4141
const nonDeprioritizedAppMapExpiration = time.Second
4242
const nonDeprioritizedAppMapInterval = 100 * time.Millisecond
4343

44-
const DefaultThrottleTTLMinutes = 60
4544
const DefaultSkipTTLMinutes = 60
4645
const DefaultThrottleRatio = 1.0
4746

@@ -519,21 +518,24 @@ func (throttler *Throttler) ThrottleApp(appName string, expireAt time.Time, rati
519518
appThrottle = object.(*base.AppThrottle)
520519
if !expireAt.IsZero() {
521520
appThrottle.ExpireAt = expireAt
521+
} else {
522+
appThrottle.ExpireAt = time.Time{}
522523
}
523524
if ratio >= 0 {
524525
appThrottle.Ratio = ratio
525526
}
526527
} else {
527-
if expireAt.IsZero() {
528-
expireAt = now.Add(DefaultThrottleTTLMinutes * time.Minute)
529-
}
530528
if ratio < 0 {
531529
ratio = DefaultThrottleRatio
532530
}
533531
appThrottle = base.NewAppThrottle(expireAt, ratio)
534532
}
535-
if now.Before(appThrottle.ExpireAt) {
536-
throttler.throttledApps.Set(appName, appThrottle, cache.DefaultExpiration)
533+
534+
if expireAt.IsZero() {
535+
//If expires at is zero, update the store to never expire the throttle
536+
throttler.throttledApps.Set(appName, appThrottle, -1)
537+
} else if now.Before(appThrottle.ExpireAt) {
538+
throttler.throttledApps.Set(appName, appThrottle, cache.DefaultExpiration)
537539
} else {
538540
throttler.UnthrottleApp(appName)
539541
}

vendor/github.com/patrickmn/go-cache/cache.go

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)