Skip to content

Commit

Permalink
(BIDS-2872) wip
Browse files Browse the repository at this point in the history
  • Loading branch information
guybrush committed Feb 9, 2024
1 parent 352634c commit bedae65
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 78 deletions.
2 changes: 0 additions & 2 deletions cmd/misc/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1942,8 +1942,6 @@ func updateRatelimits() error {
err := ratelimit.DBUpdate()
if err != nil {
logrus.WithError(err).Errorf("error in updateRatelimits")
} else {
logrus.Infof("updated ratelimits")
}
time.Sleep(time.Second * 10)
}
Expand Down
8 changes: 8 additions & 0 deletions db/migrations/20240125120000_add_ratelimits.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ CREATE TABLE IF NOT EXISTS
PRIMARY KEY (user_id)
);

CREATE INDEX IF NOT EXISTS idx_api_ratelimits_changed_at ON api_ratelimits (changed_at);

SELECT 'up SQL query - add table api_keys';
CREATE TABLE IF NOT EXISTS
api_keys (
Expand All @@ -22,6 +24,8 @@ CREATE TABLE IF NOT EXISTS
PRIMARY KEY (user_id, api_key)
);

CREATE INDEX IF NOT EXISTS idx_api_keys_changed_at ON api_keys (changed_at);

SELECT 'up SQL query - add table api_weights';
CREATE TABLE IF NOT EXISTS
api_weights (
Expand Down Expand Up @@ -51,8 +55,12 @@ CREATE TABLE IF NOT EXISTS
-- +goose StatementBegin
SELECT 'down SQL query - drop table api_ratelimits';
DROP TABLE IF EXISTS api_ratelimits;
SELECT 'down SQL query - drop index idx_api_ratelimits_changed_at';
DROP INDEX IF EXISTS idx_api_ratelimits_changed_at;
SELECT 'down SQL query - drop table api_keys';
DROP TABLE IF EXISTS api_keys;
SELECT 'down SQL query - drop index idx_api_keys_changed_at';
DROP INDEX IF EXISTS idx_api_keys_changed_at;
SELECT 'down SQL query - drop table api_weights';
DROP TABLE IF EXISTS api_weights;
SELECT 'down SQL query - drop table api_products';
Expand Down
64 changes: 0 additions & 64 deletions db/ratelimit.go

This file was deleted.

30 changes: 18 additions & 12 deletions ratelimit/ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ func HttpMiddleware(next http.Handler) http.Handler {
func updateWeights(firstRun bool) error {
start := time.Now()
defer func() {
logger.Infof("updateWeights took %v", time.Since(start).Seconds())
metrics.TaskDuration.WithLabelValues("ratelimit_updateWeights").Observe(time.Since(start).Seconds())
}()

Expand Down Expand Up @@ -341,6 +342,7 @@ func updateRedisStatus() error {
func updateStats() error {
start := time.Now()
defer func() {
logger.Infof("updateStats took %v", time.Since(start).Seconds())
metrics.TaskDuration.WithLabelValues("ratelimit_updateStats").Observe(time.Since(start).Seconds())
}()

Expand Down Expand Up @@ -505,6 +507,7 @@ func updateStatsEntries(entries []dbEntry) error {
func updateRateLimits() error {
start := time.Now()
defer func() {
logger.Infof("updateRateLimits took %v", time.Since(start).Seconds())
metrics.TaskDuration.WithLabelValues("ratelimit_updateRateLimits").Observe(time.Since(start).Seconds())
}()

Expand Down Expand Up @@ -928,6 +931,7 @@ func DBUpdate() error {
}
logrus.Infof("updated %v api_keys in %v", ra, time.Since(now))

now = time.Now()
_, err = DBUpdateApiRatelimits()
if err != nil {
return err
Expand All @@ -938,15 +942,15 @@ func DBUpdate() error {
}
logrus.Infof("updated %v api_ratelimits in %v", ra, time.Since(now))

_, err = DBInvalidateApiKeys()
if err != nil {
return err
}
ra, err = res.RowsAffected()
if err != nil {
return err
}
logrus.Infof("invalidated %v api_keys in %v", ra, time.Since(now))
// _, err = DBInvalidateApiKeys()
// if err != nil {
// return err
// }
// ra, err = res.RowsAffected()
// if err != nil {
// return err
// }
// logrus.Infof("invalidated %v api_keys in %v", ra, time.Since(now))

return nil
}
Expand All @@ -955,8 +959,7 @@ func DBInvalidateApiKeys() (sql.Result, error) {
return db.FrontendWriterDB.Exec(`
update api_ratelimits
set changed_at = now(), valid_until = now()
where valid_until > now()
and user_id not in (select user_id from api_keys where api_key is not null)`)
where valid_until > now() and not exists (select id from api_keys where api_keys.user_id = api_ratelimits.user_id)`)
}

func DBUpdateApiKeys() (sql.Result, error) {
Expand All @@ -968,7 +971,7 @@ func DBUpdateApiKeys() (sql.Result, error) {
to_timestamp('3000-01-01', 'YYYY-MM-DD') as valid_until,
now() as changed_at
from users
where api_key is not null
where api_key is not null and not exists (select user_id from api_keys where api_keys.user_id = users.id)
on conflict (user_id, api_key) do update set
valid_until = excluded.valid_until,
changed_at = excluded.changed_at
Expand Down Expand Up @@ -999,6 +1002,9 @@ func DBUpdateApiRatelimits() (sql.Result, error) {
left join current_api_products cap1 on cap1.name = coalesce(cap.name,'free')
left join app_subs_view asv on asv.user_id = u.id and asv.active = true
left join current_api_products cap2 on cap2.name = coalesce(asv.product_id,'free')
left join api_ratelimits ar on ar.user_id = u.id
where
cap1.name != 'free' or cap2.name != 'free' or ar.user_id is not null
on conflict (user_id) do update set
second = excluded.second,
hour = excluded.hour,
Expand Down

0 comments on commit bedae65

Please sign in to comment.