Skip to content

Commit

Permalink
修改计数插件逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
Dot-Liu committed Nov 8, 2023
1 parent 896aaa2 commit a287c03
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
10 changes: 5 additions & 5 deletions drivers/plugins/counter/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,19 +51,19 @@ func (b *executor) DoHttpFilter(ctx http_service.IHttpContext, next eocontext.IC
b.client = scope_manager.Auto[counter.IClient](b.clientID, "counter")
b.counterPusher = scope_manager.Auto[counter.ICountPusher](b.countPusherID, "counter-pusher")
})
body := ctx.Response().GetBody()
if len(body) < 1 {

variables, ok := b.keyGenerate.Variables(ctx)
if !ok {
return next.DoChain(ctx)
}

key := b.keyGenerate.Key(ctx)
ct, has := b.counters.Get(key)
if !has {
switch b.countMode {
case localMode:
ct = NewLocalCounter(key, b.keyGenerate.Variables(ctx), b.client, b.counterPusher)
ct = NewLocalCounter(key, variables, b.client, b.counterPusher)
case redisMode:
ct = NewRedisCounter(key, b.keyGenerate.Variables(ctx), b.cache, b.client, b.counterPusher)
ct = NewRedisCounter(key, variables, b.cache, b.client, b.counterPusher)
}
b.counters.Set(key, ct)
}
Expand Down
12 changes: 8 additions & 4 deletions drivers/plugins/counter/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var _ IKeyGenerator = (*keyGenerate)(nil)

type IKeyGenerator interface {
Key(ctx http_service.IHttpContext) string
Variables(ctx http_service.IHttpContext) eosc.Untyped[string, string]
Variables(ctx http_service.IHttpContext) (eosc.Untyped[string, string], bool)
}

func newKeyGenerate(key string) *keyGenerate {
Expand Down Expand Up @@ -43,13 +43,17 @@ type keyGenerate struct {
variables []string
}

func (k *keyGenerate) Variables(ctx http_service.IHttpContext) eosc.Untyped[string, string] {
func (k *keyGenerate) Variables(ctx http_service.IHttpContext) (eosc.Untyped[string, string], bool) {
variables := eosc.BuildUntyped[string, string]()
entry := ctx.GetEntry()
for _, v := range k.variables {
variables.Set(fmt.Sprintf("$%s", v), eosc.ReadStringFromEntry(entry, v))
value := eosc.ReadStringFromEntry(entry, v)
if value == "" {
return nil, false
}
variables.Set(fmt.Sprintf("$%s", v), value)
}
return variables
return variables, true
}

func (k *keyGenerate) Key(ctx http_service.IHttpContext) string {
Expand Down

0 comments on commit a287c03

Please sign in to comment.