Skip to content

Commit

Permalink
fixbug-限流sql
Browse files Browse the repository at this point in the history
  • Loading branch information
bingxindan committed Jul 18, 2023
1 parent ad9d132 commit 6b40d42
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 8 deletions.
32 changes: 24 additions & 8 deletions store/postgresql/ratelimit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,18 @@ func (rls *rateLimitStore) enableRateLimit(limit *model.RateLimit) error {
}()

etimeStr := limitToEtimeStr(limit)
str := fmt.Sprintf(
`update ratelimit_config set disable = $1, revision = $2, mtime = $3, etime=%s where id = $4`, etimeStr)
disable := 0
if limit.Disable {
disable = 1
}

str := "update ratelimit_config set disable = $1, revision = $2, mtime = $3, " +
"etime = $4 where id = $5"
stmt, err := tx.Prepare(str)
if err != nil {
return err
}
if _, err = stmt.Exec(limit.Disable, limit.Revision, GetCurrentTimeFormat(), limit.ID); err != nil {
if _, err = stmt.Exec(disable, limit.Revision, GetCurrentTimeFormat(), etimeStr, limit.ID); err != nil {
log.Errorf("[Store][database] update rate limit(%+v), sql %s, err: %s", limit, str, err)
return err
}
Expand All @@ -152,6 +157,7 @@ func (rls *rateLimitStore) enableRateLimit(limit *model.RateLimit) error {
log.Errorf("[Store][database] update rate limit(%+v) commit tx err: %s", limit, err.Error())
return err
}

return nil
}

Expand All @@ -168,14 +174,19 @@ func (rls *rateLimitStore) updateRateLimit(limit *model.RateLimit) error {
}()

etimeStr := limitToEtimeStr(limit)
str := fmt.Sprintf(`update ratelimit_config set name = $1, service_id = $2, disable = $3, method= $4,
labels = $5, priority = $6, rule = $7, revision = $8, mtime = $9, etime=%s where id = $10`, etimeStr)
disable := 0
if limit.Disable {
disable = 1
}
str := "update ratelimit_config set name = $1, service_id = $2, disable = $3, " +
"method = $4, labels = $5, priority = $6, rule = $7, revision = $8, " +
"mtime = $9, etime = $10 where id = $11"
stmt, err := tx.Prepare(str)
if err != nil {
return err
}
if _, err = stmt.Exec(limit.Name, limit.ServiceID, limit.Disable, limit.Method, limit.Labels,
limit.Priority, limit.Rule, limit.Revision, GetCurrentTimeFormat(), limit.ID); err != nil {
if _, err = stmt.Exec(limit.Name, limit.ServiceID, disable, limit.Method, limit.Labels,
limit.Priority, limit.Rule, limit.Revision, GetCurrentTimeFormat(), etimeStr, limit.ID); err != nil {
log.Errorf("[Store][database] update rate limit(%+v), sql %s, err: %s", limit, str, err)
return err
}
Expand Down Expand Up @@ -313,8 +324,9 @@ func fetchRateLimitCacheRows(rows *sql.Rows) ([]*model.RateLimit, error) {
rateLimit model.RateLimit
serviceID string
flag int
disable int
)
err := rows.Scan(&rateLimit.ID, &rateLimit.Name, &rateLimit.Disable, &serviceID, &rateLimit.Method,
err := rows.Scan(&rateLimit.ID, &rateLimit.Name, &disable, &serviceID, &rateLimit.Method,
&rateLimit.Labels, &rateLimit.Priority, &rateLimit.Rule, &rateLimit.Revision, &flag,
&rateLimit.CreateTime, &rateLimit.ModifyTime, &rateLimit.EnableTime)
if err != nil {
Expand All @@ -325,6 +337,10 @@ func fetchRateLimitCacheRows(rows *sql.Rows) ([]*model.RateLimit, error) {
if flag == 1 {
rateLimit.Valid = false
}
rateLimit.Disable = false
if disable == 1 {
rateLimit.Disable = true
}
rateLimit.ServiceID = serviceID

rateLimits = append(rateLimits, &rateLimit)
Expand Down
61 changes: 61 additions & 0 deletions store/postgresql/ratelimit_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,64 @@ func TestCreateRateLimit(t *testing.T) {
err := obj.rateLimitStore.CreateRateLimit(conf)
fmt.Printf("err: %+v\n", err)
}

func TestUpdateRateLimit(t *testing.T) {
obj := initConf()

// Method: Labels: Priority:0 Rule:{"service":{"value":"polaris.limiter"},"namespace":{"value":"Polaris"},"type":1,"amounts":[{"maxAmount":{"value":1},"validDuration":{"seconds":1}}],"action":{"value":"REJECT"},"disable":{},"regex_combine":{"value":true},"method":{"value":{}},"arguments":[{"key":"aa","value":{"value":{"value":"2"}}}],"name":{"value":"aa"},"max_queue_delay":{"value":1}} Revision:52df77fff7c14ec9a5a8306d381091fc Disable:false Valid:false CreateTime:0001-01-01 00:00:00 +0000 UTC ModifyTime:0001-01-01 00:00:00 +0000 UTC EnableTime:0001-01-01 00:00:00 +0000 UTC}),
conf := &model.RateLimit{
ID: "d7af189c1986413ab928d501db200600",
Name: "aa",
Disable: true,
ServiceID: "",
Method: "",
Labels: "",
Priority: 1,
Revision: "4444",
}
err := obj.rateLimitStore.UpdateRateLimit(conf)
fmt.Printf("err: %+v\n", err)
}

func TestEnableRateLimit(t *testing.T) {
obj := initConf()

// Method: Labels: Priority:0 Rule:{"service":{"value":"polaris.limiter"},"namespace":{"value":"Polaris"},"type":1,"amounts":[{"maxAmount":{"value":1},"validDuration":{"seconds":1}}],"action":{"value":"REJECT"},"disable":{},"regex_combine":{"value":true},"method":{"value":{}},"arguments":[{"key":"aa","value":{"value":{"value":"2"}}}],"name":{"value":"aa"},"max_queue_delay":{"value":1}} Revision:52df77fff7c14ec9a5a8306d381091fc Disable:false Valid:false CreateTime:0001-01-01 00:00:00 +0000 UTC ModifyTime:0001-01-01 00:00:00 +0000 UTC EnableTime:0001-01-01 00:00:00 +0000 UTC}),
conf := &model.RateLimit{
ID: "d7af189c1986413ab928d501db200600",
Name: "aa",
Disable: true,
ServiceID: "",
Method: "",
Labels: "",
Priority: 1,
Revision: "4444",
}
err := obj.rateLimitStore.EnableRateLimit(conf)
fmt.Printf("err: %+v\n", err)
}

func TestDeleteRateLimit(t *testing.T) {
obj := initConf()

// Method: Labels: Priority:0 Rule:{"service":{"value":"polaris.limiter"},"namespace":{"value":"Polaris"},"type":1,"amounts":[{"maxAmount":{"value":1},"validDuration":{"seconds":1}}],"action":{"value":"REJECT"},"disable":{},"regex_combine":{"value":true},"method":{"value":{}},"arguments":[{"key":"aa","value":{"value":{"value":"2"}}}],"name":{"value":"aa"},"max_queue_delay":{"value":1}} Revision:52df77fff7c14ec9a5a8306d381091fc Disable:false Valid:false CreateTime:0001-01-01 00:00:00 +0000 UTC ModifyTime:0001-01-01 00:00:00 +0000 UTC EnableTime:0001-01-01 00:00:00 +0000 UTC}),
conf := &model.RateLimit{
ID: "d7af189c1986413ab928d501db200600",
Name: "aa",
Disable: true,
ServiceID: "",
Method: "",
Labels: "",
Priority: 1,
Revision: "4444",
}
err := obj.rateLimitStore.DeleteRateLimit(conf)
fmt.Printf("err: %+v\n", err)
}

func TestGetRateLimitWithID(t *testing.T) {
obj := initConf()

ret, err := obj.rateLimitStore.GetRateLimitWithID("d7af189c1986413ab928d501db200600")
fmt.Printf("ret: %+v, err: %+v\n", ret, err)
}

0 comments on commit 6b40d42

Please sign in to comment.