Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Permanent Throttle Option #170

Merged
merged 7 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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

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

Loading