Skip to content

Commit

Permalink
修复计数插件使用Redis时回滚操作会将key删掉的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
Dot-Liu committed Aug 31, 2023
1 parent bb5924c commit ca65714
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 10 deletions.
2 changes: 1 addition & 1 deletion drivers/plugins/counter/matcher/match.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
}
4 changes: 2 additions & 2 deletions drivers/plugins/counter/separator/count.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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) {
Expand Down
12 changes: 9 additions & 3 deletions drivers/resources/redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 8 additions & 3 deletions drivers/router/http-router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
5 changes: 4 additions & 1 deletion resources/cache-local.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package resources

import (
"context"
"github.com/coocood/freecache"
"strconv"
"sync"
"time"

"github.com/coocood/freecache"
)

var (
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
Expand Down

0 comments on commit ca65714

Please sign in to comment.