Skip to content

Commit

Permalink
fixbug-修复创建限流默认时间问题
Browse files Browse the repository at this point in the history
  • Loading branch information
bingxindan committed Jul 18, 2023
1 parent 7ae1a34 commit ad9d132
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 2 deletions.
2 changes: 1 addition & 1 deletion store/postgresql/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ const (
// DefaultConnMaxLifetime default maximum connection lifetime
DefaultConnMaxLifetime = 60 * 30 // 默认是30分钟
// emptyEnableTime 规则禁用时启用时间的默认值
emptyEnableTime = "STR_TO_DATE('1980-01-01 00:00:01', '%Y-%m-%d %H:%i:%s')"
emptyEnableTime = "1980-01-01 00:00:01"
)

// PostgresqlStore 实现了Store接口
Expand Down
6 changes: 5 additions & 1 deletion store/postgresql/ratelimit_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ func (rls *rateLimitStore) createRateLimit(limit *model.RateLimit) error {
}()

etimeStr := limitToEtimeStr(limit)
disable := 0
if limit.Disable {
disable = 1
}
// 新建限流规则
str := "insert into ratelimit_config(id, name, disable, service_id, " +
"method, labels, priority, rule, revision, ctime, mtime, etime) " +
Expand All @@ -79,7 +83,7 @@ func (rls *rateLimitStore) createRateLimit(limit *model.RateLimit) error {
if err != nil {
return err
}
if _, err = stmt.Exec(limit.ID, limit.Name, limit.Disable, limit.ServiceID, limit.Method,
if _, err = stmt.Exec(limit.ID, limit.Name, disable, limit.ServiceID, limit.Method,
limit.Labels, limit.Priority, limit.Rule, limit.Revision, GetCurrentTimeFormat(),
GetCurrentTimeFormat(), etimeStr); err != nil {
log.Errorf("[Store][database] create rate limit(%+v), sql %s err: %s", limit, str, err.Error())
Expand Down
42 changes: 42 additions & 0 deletions store/postgresql/ratelimit_config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package postgresql

import (
"fmt"
"github.com/polarismesh/polaris/common/model"
"testing"
)

func TestCreateRateLimit(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.CreateRateLimit(conf)
fmt.Printf("err: %+v\n", err)
}

0 comments on commit ad9d132

Please sign in to comment.