From ca65714f2d53d2a6fda1e9f0969038508cf4beeb Mon Sep 17 00:00:00 2001 From: Liujian <824010343@qq.com> Date: Thu, 31 Aug 2023 10:36:43 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=AE=A1=E6=95=B0=E6=8F=92?= =?UTF-8?q?=E4=BB=B6=E4=BD=BF=E7=94=A8Redis=E6=97=B6=E5=9B=9E=E6=BB=9A?= =?UTF-8?q?=E6=93=8D=E4=BD=9C=E4=BC=9A=E5=B0=86key=E5=88=A0=E6=8E=89?= =?UTF-8?q?=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- drivers/plugins/counter/matcher/match.go | 2 +- drivers/plugins/counter/separator/count.go | 4 ++-- drivers/resources/redis/redis.go | 12 +++++++++--- drivers/router/http-router/router.go | 11 ++++++++--- resources/cache-local.go | 5 ++++- 5 files changed, 24 insertions(+), 10 deletions(-) diff --git a/drivers/plugins/counter/matcher/match.go b/drivers/plugins/counter/matcher/match.go index 39847c0b..a87beb22 100644 --- a/drivers/plugins/counter/matcher/match.go +++ b/drivers/plugins/counter/matcher/match.go @@ -10,6 +10,6 @@ type IMatcher interface { type MatchParam struct { Key string `json:"key"` - Kind string `json:"kind"` // int|string|bool + Kind string `json:"kind" enum:"int,string,bool" default:"string"` // int|string|bool Value []string `json:"value"` } diff --git a/drivers/plugins/counter/separator/count.go b/drivers/plugins/counter/separator/count.go index 48b2367e..36a1a654 100644 --- a/drivers/plugins/counter/separator/count.go +++ b/drivers/plugins/counter/separator/count.go @@ -18,7 +18,7 @@ var ( ) type CountRule struct { - RequestBodyType string `json:"request_body_type" label:"请求体类型" enum:"form-data,json"` + RequestBodyType string `json:"request_body_type" label:"请求体类型" enum:"form-data,json,multipart-formdata"` Key string `json:"key" label:"参数名称(支持json path)"` Separator string `json:"separator" label:"分隔符" switch:"separator_type===splite"` SeparatorType string `json:"separator_type" label:"分割类型" enum:"splite,array,length"` @@ -32,7 +32,7 @@ type ICounter interface { } func GetCounter(rule *CountRule) (ICounter, error) { - if rule == nil && rule.Key == "" { + if rule == nil || rule.Key == "" { return NewEmptyCounter(), nil } switch strings.ToLower(rule.RequestBodyType) { diff --git a/drivers/resources/redis/redis.go b/drivers/resources/redis/redis.go index 00075684..4a3c58c3 100644 --- a/drivers/resources/redis/redis.go +++ b/drivers/resources/redis/redis.go @@ -3,9 +3,10 @@ package redis import ( "context" "fmt" + "time" + "github.com/eolinker/apinto/resources" "github.com/go-redis/redis/v8" - "time" ) type statusResult struct { @@ -63,7 +64,9 @@ func (r *Cmdable) SetNX(ctx context.Context, key string, value []byte, expiratio func (r *Cmdable) DecrBy(ctx context.Context, key string, decrement int64, expiration time.Duration) resources.IntResult { pipeline := r.cmdable.Pipeline() result := pipeline.DecrBy(ctx, key, decrement) - pipeline.Expire(ctx, key, expiration) + if expiration > 0 { + pipeline.Expire(ctx, key, expiration) + } _, err := pipeline.Exec(ctx) if err != nil { return nil @@ -75,7 +78,10 @@ func (r *Cmdable) DecrBy(ctx context.Context, key string, decrement int64, expir func (r *Cmdable) IncrBy(ctx context.Context, key string, decrement int64, expiration time.Duration) resources.IntResult { pipeline := r.cmdable.Pipeline() result := pipeline.IncrBy(ctx, key, decrement) - pipeline.Expire(ctx, key, expiration) + if expiration > 0 { + pipeline.Expire(ctx, key, expiration) + } + _, err := pipeline.Exec(ctx) if err != nil { return nil diff --git a/drivers/router/http-router/router.go b/drivers/router/http-router/router.go index 1a72cfff..a7532aac 100644 --- a/drivers/router/http-router/router.go +++ b/drivers/router/http-router/router.go @@ -2,13 +2,14 @@ package http_router import ( "net/http" + "net/url" "strings" "time" - "github.com/eolinker/apinto/drivers/router/http-router/websocket" - "github.com/eolinker/apinto/service" + "github.com/eolinker/apinto/drivers/router/http-router/websocket" + "github.com/eolinker/eosc/eocontext" "github.com/eolinker/apinto/drivers" @@ -87,7 +88,11 @@ func (h *HttpRouter) reset(cfg *Config, workers map[eosc.RequireId]eosc.IWorker) // 当service未指定,使用默认返回 handler.completeHandler = http_complete.NewNoServiceCompleteHandler(cfg.Status, cfg.Header, cfg.Body) } else { - serviceWorker, has := workers[cfg.Service] + s, err := url.PathUnescape(string(cfg.Service)) + if err != nil { + s = string(cfg.Service) + } + serviceWorker, has := workers[eosc.RequireId(s)] if !has || !serviceWorker.CheckSkill(service.ServiceSkill) { return eosc.ErrorNotGetSillForRequire } diff --git a/resources/cache-local.go b/resources/cache-local.go index 7146c5a9..b5e55b6a 100644 --- a/resources/cache-local.go +++ b/resources/cache-local.go @@ -2,10 +2,11 @@ package resources import ( "context" - "github.com/coocood/freecache" "strconv" "sync" "time" + + "github.com/coocood/freecache" ) var ( @@ -79,6 +80,7 @@ func (n *cacheLocal) DecrBy(ctx context.Context, key string, decrement int64, ex } func (n *cacheLocal) IncrBy(ctx context.Context, key string, incr int64, expiration time.Duration) IntResult { + n.keyLock.Lock() lock, has := n.keyLocks[key] if !has { @@ -101,6 +103,7 @@ func (n *cacheLocal) IncrBy(ctx context.Context, key string, incr int64, expirat n.keyLock.Unlock() } }() + v, err := n.client.Get([]byte(key)) if err != nil || len(v) != 8 { v = ToBytes(incr)