Skip to content

Commit

Permalink
Add Permanent Throttle Option (#170)
Browse files Browse the repository at this point in the history
Update freno to support infinite TTL
  • Loading branch information
kristinaElizDev authored Sep 24, 2024
1 parent d89b112 commit 2acc13e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 16 deletions.
2 changes: 1 addition & 1 deletion doc/mysql-backend.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ CREATE TABLE service_election (
CREATE TABLE throttled_apps (
app_name varchar(128) NOT NULL,
throttled_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP,
ratio DOUBLE,
PRIMARY KEY (app_name)
);
Expand Down
20 changes: 12 additions & 8 deletions pkg/group/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ service_id varchar(128) NOT NULL,
CREATE TABLE throttled_apps (
app_name varchar(128) NOT NULL,
throttled_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
expires_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP,
ratio DOUBLE,
PRIMARY KEY (app_name)
);
Expand Down Expand Up @@ -346,7 +346,7 @@ func (backend *MySQLBackend) readThrottledApps() error {
query := `
select
app_name,
timestampdiff(second, now(), expires_at) as ttl_seconds,
timestampdiff(second, now(), coalesce(expires_at,now())) as ttl_seconds,
ratio
from
throttled_apps
Expand All @@ -357,6 +357,9 @@ func (backend *MySQLBackend) readThrottledApps() error {
ttlSeconds := m.GetInt64("ttl_seconds")
ratio, _ := strconv.ParseFloat(m.GetString("ratio"), 64)
expiresAt := time.Now().Add(time.Duration(ttlSeconds) * time.Second)
if ttlSeconds == 0 {
expiresAt = time.Time{}
}

go log.Debugf("read-throttled-apps: app=%s, ttlSeconds%+v, expiresAt=%+v, ratio=%+v", appName, ttlSeconds, expiresAt, ratio)
go backend.throttler.ThrottleApp(appName, expiresAt, ratio)
Expand Down Expand Up @@ -398,18 +401,19 @@ func (backend *MySQLBackend) ThrottleApp(appName string, ttlMinutes int64, expir
`
args = sqlutils.Args(appName, ttlMinutes, ratio)
} else {
// TTL=0 ; if app is already throttled, keep existing TTL and only update ratio.
// if app does not exist use DefaultThrottleTTL
// TTL=0 ; if app is already throttled, update throttle to infinity and update ratio.
// if app does not exist, TTL should be infinite
query = `
insert into throttled_apps (
app_name, throttled_at, expires_at, ratio
app_name, throttled_at, ratio
) values (
?, now(), now() + interval ? minute, ?
?, now(), ?
)
on duplicate key update
ratio=values(ratio)
ratio=values(ratio),
expires_at=null
`
args = sqlutils.Args(appName, throttle.DefaultThrottleTTLMinutes, ratio)
args = sqlutils.Args(appName, ratio)
}
_, err := sqlutils.ExecNoPrepare(backend.db, query, args...)
backend.throttler.ThrottleApp(appName, expireAt, ratio)
Expand Down
14 changes: 8 additions & 6 deletions pkg/throttle/throttler.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const skippedHostsSnapshotInterval = 5 * time.Second
const nonDeprioritizedAppMapExpiration = time.Second
const nonDeprioritizedAppMapInterval = 100 * time.Millisecond

const DefaultThrottleTTLMinutes = 60
const DefaultSkipTTLMinutes = 60
const DefaultThrottleRatio = 1.0

Expand Down Expand Up @@ -519,21 +518,24 @@ func (throttler *Throttler) ThrottleApp(appName string, expireAt time.Time, rati
appThrottle = object.(*base.AppThrottle)
if !expireAt.IsZero() {
appThrottle.ExpireAt = expireAt
} else {
appThrottle.ExpireAt = time.Time{}
}
if ratio >= 0 {
appThrottle.Ratio = ratio
}
} else {
if expireAt.IsZero() {
expireAt = now.Add(DefaultThrottleTTLMinutes * time.Minute)
}
if ratio < 0 {
ratio = DefaultThrottleRatio
}
appThrottle = base.NewAppThrottle(expireAt, ratio)
}
if now.Before(appThrottle.ExpireAt) {
throttler.throttledApps.Set(appName, appThrottle, cache.DefaultExpiration)

if expireAt.IsZero() {
//If expires at is zero, update the store to never expire the throttle
throttler.throttledApps.Set(appName, appThrottle, -1)
} else if now.Before(appThrottle.ExpireAt) {
throttler.throttledApps.Set(appName, appThrottle, cache.DefaultExpiration)
} else {
throttler.UnthrottleApp(appName)
}
Expand Down
6 changes: 5 additions & 1 deletion vendor/github.com/patrickmn/go-cache/cache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 2acc13e

Please sign in to comment.