Skip to content

Commit

Permalink
添加慢速超时逻辑
Browse files Browse the repository at this point in the history
  • Loading branch information
yangyile committed Oct 3, 2024
1 parent 229fa20 commit aaf9760
Show file tree
Hide file tree
Showing 8 changed files with 130 additions and 2 deletions.
2 changes: 1 addition & 1 deletion authkratossimple/auth_kratos_simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func (a *Config) GetField() string {
func NewMiddleware(cfg *Config, LOGGER log.Logger) middleware.Middleware {
LOG := log.NewHelper(LOGGER)
LOG.Infof(
"new check_auth enable=%v field=%v simple=x include=%v operations=%v",
"new check_auth middleware enable=%v field=%v simple=x include=%v operations=%v",
cfg.IsEnable(),
cfg.field,
cfg.selectPath.SelectSide,
Expand Down
2 changes: 1 addition & 1 deletion authkratostokens/auth_kratos_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (a *Config) GetMapTokens() map[string]string {
func NewMiddleware(cfg *Config, LOGGER log.Logger) middleware.Middleware {
LOG := log.NewHelper(LOGGER)
LOG.Infof(
"new check_auth enable=%v field=%v tokens=%v include=%v operations=%v",
"new check_auth middleware enable=%v field=%v tokens=%v include=%v operations=%v",
cfg.IsEnable(),
cfg.field,
len(cfg.tokens),
Expand Down
5 changes: 5 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ require (
github.com/go-kratos/kratos/v2 v2.8.0
github.com/google/uuid v1.6.0
github.com/yyle88/must v0.0.3
github.com/yyle88/neatjson v0.0.6
go.elastic.co/apm/v2 v2.6.2
)

Expand All @@ -17,7 +18,11 @@ require (
github.com/pkg/errors v0.9.1 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
github.com/rogpeppe/go-internal v1.13.1 // indirect
github.com/yyle88/done v1.0.10 // indirect
github.com/yyle88/erero v1.0.11 // indirect
github.com/yyle88/mutexmap v1.0.4 // indirect
github.com/yyle88/sure v0.0.22 // indirect
github.com/yyle88/syntaxgo v0.0.25 // indirect
github.com/yyle88/zaplog v0.0.10 // indirect
go.elastic.co/fastjson v1.4.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
Expand Down
18 changes: 18 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,24 @@ github.com/rogpeppe/go-internal v1.13.1 h1:KvO1DLK/DRN07sQ1LQKScxyZJuNnedQ5/wKSR
github.com/rogpeppe/go-internal v1.13.1/go.mod h1:uMEvuHeurkdAXX61udpOXGD/AzZDWNMNyH2VO9fmH0o=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/yyle88/done v1.0.10 h1:aM2vBEG9GRdij1tgfb0FHm15BYZ0A2jURJYmn4+bkyc=
github.com/yyle88/done v1.0.10/go.mod h1:bhlEOXeubd6Po0F2/SngxKFWgPsrkws7hPhU78/7twA=
github.com/yyle88/erero v1.0.11 h1:Qg1yAv4EXWa2oGwynlKIvkQXrNj3WUsEZaBeolXdAgU=
github.com/yyle88/erero v1.0.11/go.mod h1:Py9OKSuz3A7r3PmgeUBmgjkVUpNPCeinFlS0LQeC0rQ=
github.com/yyle88/formatgo v1.0.11 h1:bJuHyTAV/L3RD2BfGuk85APwu15qCA8rey+TnnhUgt4=
github.com/yyle88/formatgo v1.0.11/go.mod h1:/ghTCbopOb7VT//Wr10jLHLNIwhznaFXqynT24muBOk=
github.com/yyle88/must v0.0.3 h1:mtQeanevihE0p4Imkx0XkXU6cQZiKxANDXu0PMJElkw=
github.com/yyle88/must v0.0.3/go.mod h1:BGo8wMJL1cXHkVRvGKveRx1V0dkZaJBe9ETxV9Lpfh8=
github.com/yyle88/mutexmap v1.0.4 h1:Y4VfNE28HBYwpw/UaZyTUZAjYcob3qPMubQWxgdYl9s=
github.com/yyle88/mutexmap v1.0.4/go.mod h1:lAEDkpXe9iSU0/8L/SaXAQ6TUa+4A9nnl9OQ56zHEQY=
github.com/yyle88/neatjson v0.0.6 h1:3IZqF5LlgUX1DomALHzSZnqNVgs9NTF7UkosjHqVLLQ=
github.com/yyle88/neatjson v0.0.6/go.mod h1:KEplH+8BAVV6hbnqaW2yDT/ilnjahgvBPlZ7YrI4rKY=
github.com/yyle88/runpath v1.0.7 h1:Je2iferujX94tJujL6/Q6mtMEbkZt9puByQZDmYyi3w=
github.com/yyle88/runpath v1.0.7/go.mod h1:q/MwsaROo6uRRFnB+ZxV9iMX4K5j4P3iEeShrkfUYyo=
github.com/yyle88/sure v0.0.22 h1:Y0L8uocG1PAH4EHojBWhTIWuFFuXp66MaNVl4immwMw=
github.com/yyle88/sure v0.0.22/go.mod h1:rNiocNAPQoOlQ/5Gk4ZrLEikBj8MEkxd4SfWBJAucME=
github.com/yyle88/syntaxgo v0.0.25 h1:jLtmczsBt5WxYKiFR1Vs1KTvocScQQZairOWzRl4Jd8=
github.com/yyle88/syntaxgo v0.0.25/go.mod h1:pvrDegnNAdFGnzKyvJL2vi+3VhuZinzoP0L7Sji/CZk=
github.com/yyle88/zaplog v0.0.10 h1:4jzeWB7h7IQ126hcE5mRMjVo99k8+xpLjQTtbie8JZ8=
github.com/yyle88/zaplog v0.0.10/go.mod h1:dS72cgivcfuXM6ExW6+f/YiPVGOzQ7spQL5jECrEdcU=
go.elastic.co/apm/v2 v2.6.2 h1:VBplAxgbOgTv+Giw/FS91xJpHYw/q8fz/XKPvqC+7/o=
Expand All @@ -47,6 +61,8 @@ go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/mod v0.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/mod v0.21.0/go.mod h1:6SkKJ3Xj0I0BrPOZoBy3bdMptDDU9oJrpohJ3eWZ1fY=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/sync v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
Expand All @@ -55,6 +71,8 @@ golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
golang.org/x/tools v0.25.0 h1:oFU9pkj/iJgs+0DT+VMHrx+oBKs/LJMV+Uvg78sl+fE=
golang.org/x/tools v0.25.0/go.mod h1:/vtpO8WL1N9cQC3FN5zPqb//fRXskFHbLKk4OW1Q7rg=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240930140551-af27646dc61f h1:cUMEy+8oS78BWIH9OWazBkzbr090Od9tWBNtZHkOhf0=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240930140551-af27646dc61f/go.mod h1:UqMtugtsSgubUsoxbuAoiCXvqvErP7Gf0so0mK9tHxU=
google.golang.org/grpc v1.67.1 h1:zWnc1Vrcno+lHZCOofnIMvycFcc0QRGIzm9dhnDX68E=
Expand Down
8 changes: 8 additions & 0 deletions internal/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ func NewUUID() string {
return hex.EncodeToString(uux[:])
}

func MpKB[K comparable](a []K) (mp map[K]bool) {
mp = make(map[K]bool, len(a))
for _, v := range a {
mp[v] = true
}
return mp
}

func Keys[K comparable, V any](a map[K]V) (keys []K) {
keys = make([]K, 0, len(a))
for k := range a {
Expand Down
15 changes: 15 additions & 0 deletions internal/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package utils

import (
"testing"

"github.com/yyle88/neatjson/neatjsons"
)

func TestMpKB(t *testing.T) {
t.Log(neatjsons.S(MpKB([]string{"a", "b", "c"})))
}

func TestSample(t *testing.T) {
t.Log(neatjsons.S(Sample([]string{"a", "b", "c"})))
}
73 changes: 73 additions & 0 deletions slowkratoshandle/slowkratoshandle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package slowkratoshandle

import (
"context"
"time"

"github.com/go-kratos/kratos/v2/log"
"github.com/go-kratos/kratos/v2/middleware"
"github.com/go-kratos/kratos/v2/middleware/selector"
"github.com/orzkratos/authkratos/authkratospath"
"github.com/orzkratos/authkratos/internal/utils"
)

type SlowFastConfig struct {
fastTimeoutGap time.Duration //快速超时的时间
fastOperations []authkratospath.Path
slowOperations []authkratospath.Path
}

func NewSlowFastConfig(
fastTimeoutGap time.Duration,
fastOperations authkratospath.Paths,
slowOperations authkratospath.Paths,
) *SlowFastConfig {
return &SlowFastConfig{
fastTimeoutGap: fastTimeoutGap,
fastOperations: fastOperations,
slowOperations: slowOperations,
}
}

// NewMiddleware 有时接口分为快速返回和耗时返回两种,我们可以单独设置它们的timeout时间,否则假如把超时都设置为10分钟,则某些小接口卡住时也不行
func NewMiddleware(cfg *SlowFastConfig, LOGGER log.Logger) middleware.Middleware {
LOG := log.NewHelper(LOGGER)
LOG.Infof(
"new slow_fast middleware slow=%v fast=%v fast_timeout=%v",
len(cfg.slowOperations),
len(cfg.fastOperations),
cfg.fastTimeoutGap,
)

return selector.Server(middlewareFunc(cfg)).Match(matchFunc(cfg, LOGGER)).Build()
}

func matchFunc(cfg *SlowFastConfig, LOGGER log.Logger) selector.MatchFunc {
LOG := log.NewHelper(LOGGER)
qMap := utils.MpKB(cfg.fastOperations)
sMap := utils.MpKB(cfg.slowOperations)
return func(ctx context.Context, operation string) bool {
path := authkratospath.New(operation)
if qMap[path] {
LOG.Debugf("operation=%s slow_fast_middleware [fast]", operation)
return true
} else if sMap[path] {
LOG.Debugf("operation=%s slow_fast_middleware [slow]", operation)
return false
} else {
LOG.Debugf("operation=%s slow_fast_middleware [soon]", operation)
return true
}
}
}

func middlewareFunc(cfg *SlowFastConfig) middleware.Middleware {
return func(handleFunc middleware.Handler) middleware.Handler {
return func(ctx context.Context, req interface{}) (interface{}, error) {
//设置新超时时间,因此需要外面的超时时间更长些,选择部分接口设置快速超时
ctx, can := context.WithTimeout(ctx, cfg.fastTimeoutGap)
defer can()
return handleFunc(ctx, req)
}
}
}
9 changes: 9 additions & 0 deletions slowkratoshandle/slowkratoshandle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package slowkratoshandle

import (
"testing"
)

func TestMain(m *testing.M) {
m.Run()
}

0 comments on commit aaf9760

Please sign in to comment.