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 13, 2024
1 parent 3bb0463 commit a52d540
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 21 deletions.
17 changes: 0 additions & 17 deletions db/migrations/20240125120000_add_ratelimits.sql
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,6 @@ CREATE TABLE IF NOT EXISTS
valid_from TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT TO_TIMESTAMP(0),
PRIMARY KEY (name, valid_from)
);

SELECT 'up SQL query - add table api_stats';
CREATE TABLE IF NOT EXISTS
api_stats (
ts TIMESTAMP WITHOUT TIME ZONE NOT NULL DEFAULT NOW(),
user_id INT NOT NULL,
api_key VARCHAR(256) NOT NULL,
endpoint TEXT NOT NULL,
count INT NOT NULL,
PRIMARY KEY (ts, user_id, api_key, endpoint)
);

CREATE INDEX IF NOT EXISTS idx_api_stats_ts_user_id ON api_stats (ts, user_id);
-- +goose StatementEnd

-- +goose Down
Expand All @@ -78,8 +65,4 @@ SELECT 'down SQL query - drop table api_weights';
DROP TABLE IF EXISTS api_weights;
SELECT 'down SQL query - drop table api_products';
DROP TABLE IF EXISTS api_products;
SELECT 'down SQL query - drop table api_stats';
DROP TABLE IF EXISTS api_stats;
SELECT 'down SQL query - drop index idx_api_stats_ts_user_id';
DROP INDEX IF EXISTS idx_api_stats_ts_user_id;
-- +goose StatementEnd
7 changes: 3 additions & 4 deletions ratelimit/ratelimit.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const (

statsTruncateDuration = time.Hour * 1 // ratelimit-stats are truncated to this duration

updateInterval = time.Second * 60 // how often to update ratelimits, weights and stats
updateInterval = time.Second * 1 // how often to update ratelimits, weights and stats
)

var NoKeyRateLimit = &RateLimit{
Expand Down Expand Up @@ -493,7 +493,7 @@ func updateStatsEntries(entries []DbEntry) error {
}
defer tx.Rollback()

numArgs := 5
numArgs := 4
batchSize := 65535 / numArgs // max 65535 params per batch, since postgres uses int16 for binding input params
valueArgs := make([]interface{}, 0, batchSize*numArgs)
valueStrings := make([]string, 0, batchSize)
Expand All @@ -506,7 +506,6 @@ func updateStatsEntries(entries []DbEntry) error {

valueStrings = append(valueStrings, "("+strings.Join(valueStringArr, ",")+")")
valueArgs = append(valueArgs, entry.Date)
valueArgs = append(valueArgs, entry.UserId)
valueArgs = append(valueArgs, entry.ApiKey)
valueArgs = append(valueArgs, entry.Endpoint)
valueArgs = append(valueArgs, entry.Count)
Expand All @@ -517,7 +516,7 @@ func updateStatsEntries(entries []DbEntry) error {
allIdx++

if batchIdx >= batchSize || allIdx >= len(entries) {
stmt := fmt.Sprintf(`INSERT INTO api_stats (ts, user_id, api_key, endpoint, count) VALUES %s ON CONFLICT (ts, user_id, api_key, endpoint) DO UPDATE SET count = EXCLUDED.count`, strings.Join(valueStrings, ","))
stmt := fmt.Sprintf(`INSERT INTO api_statistics (ts, apikey, call, count) VALUES %s ON CONFLICT (ts, apikey, call) DO UPDATE SET count = EXCLUDED.count`, strings.Join(valueStrings, ","))
_, err := tx.Exec(stmt, valueArgs...)
if err != nil {
return err
Expand Down

0 comments on commit a52d540

Please sign in to comment.