change code for shield pkg

This commit is contained in:
zhuyasen
2022-11-02 23:50:36 +08:00
parent 4d35f998b0
commit b6e01e19e3
9 changed files with 48 additions and 41 deletions

View File

@@ -5,10 +5,9 @@ import (
"github.com/zhufuyi/sponge/pkg/container/group" "github.com/zhufuyi/sponge/pkg/container/group"
"github.com/zhufuyi/sponge/pkg/gin/response" "github.com/zhufuyi/sponge/pkg/gin/response"
"github.com/zhufuyi/sponge/pkg/shield/circuitbreaker"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-kratos/aegis/circuitbreaker"
"github.com/go-kratos/aegis/circuitbreaker/sre"
) )
// ErrNotAllowed error not allowed. // ErrNotAllowed error not allowed.
@@ -24,7 +23,7 @@ type circuitBreakerOptions struct {
func defaultCircuitBreakerOptions() *circuitBreakerOptions { func defaultCircuitBreakerOptions() *circuitBreakerOptions {
return &circuitBreakerOptions{ return &circuitBreakerOptions{
group: group.NewGroup(func() interface{} { group: group.NewGroup(func() interface{} {
return sre.NewBreaker() return circuitbreaker.NewBreaker()
}), }),
} }
} }

View File

@@ -12,10 +12,10 @@ import (
"github.com/zhufuyi/sponge/pkg/container/group" "github.com/zhufuyi/sponge/pkg/container/group"
"github.com/zhufuyi/sponge/pkg/gin/response" "github.com/zhufuyi/sponge/pkg/gin/response"
"github.com/zhufuyi/sponge/pkg/gohttp" "github.com/zhufuyi/sponge/pkg/gohttp"
"github.com/zhufuyi/sponge/pkg/shield/circuitbreaker"
"github.com/zhufuyi/sponge/pkg/utils" "github.com/zhufuyi/sponge/pkg/utils"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
"github.com/go-kratos/aegis/circuitbreaker/sre"
) )
func runCircuitBreakerHTTPServer() string { func runCircuitBreakerHTTPServer() string {
@@ -24,7 +24,7 @@ func runCircuitBreakerHTTPServer() string {
gin.SetMode(gin.ReleaseMode) gin.SetMode(gin.ReleaseMode)
r := gin.New() r := gin.New()
r.Use(CircuitBreaker(WithGroup(group.NewGroup(func() interface{} { r.Use(CircuitBreaker(WithGroup(group.NewGroup(func() interface{} {
return sre.NewBreaker() return circuitbreaker.NewBreaker()
})))) }))))
r.GET("/hello", func(c *gin.Context) { r.GET("/hello", func(c *gin.Context) {

View File

@@ -4,11 +4,9 @@ import (
"net/http" "net/http"
"time" "time"
"github.com/zhufuyi/sponge/pkg/gin/response"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
rl "github.com/go-kratos/aegis/ratelimit" "github.com/zhufuyi/sponge/pkg/gin/response"
"github.com/go-kratos/aegis/ratelimit/bbr" rl "github.com/zhufuyi/sponge/pkg/shield/ratelimit"
) )
// ErrLimitExceed is returned when the rate limiter is // ErrLimitExceed is returned when the rate limiter is
@@ -71,11 +69,11 @@ func WithCPUQuota(quota float64) RateLimitOption {
func RateLimit(opts ...RateLimitOption) gin.HandlerFunc { func RateLimit(opts ...RateLimitOption) gin.HandlerFunc {
o := defaultRatelimitOptions() o := defaultRatelimitOptions()
o.apply(opts...) o.apply(opts...)
limiter := bbr.NewLimiter( limiter := rl.NewLimiter(
bbr.WithWindow(o.window), rl.WithWindow(o.window),
bbr.WithBucket(o.bucket), rl.WithBucket(o.bucket),
bbr.WithCPUThreshold(o.cpuThreshold), rl.WithCPUThreshold(o.cpuThreshold),
bbr.WithCPUQuota(o.cpuQuota), rl.WithCPUQuota(o.cpuQuota),
) )
return func(c *gin.Context) { return func(c *gin.Context) {

View File

@@ -5,9 +5,8 @@ import (
"github.com/zhufuyi/sponge/pkg/container/group" "github.com/zhufuyi/sponge/pkg/container/group"
"github.com/zhufuyi/sponge/pkg/errcode" "github.com/zhufuyi/sponge/pkg/errcode"
"github.com/zhufuyi/sponge/pkg/shield/circuitbreaker"
"github.com/go-kratos/aegis/circuitbreaker"
"github.com/go-kratos/aegis/circuitbreaker/sre"
"google.golang.org/grpc" "google.golang.org/grpc"
"google.golang.org/grpc/codes" "google.golang.org/grpc/codes"
"google.golang.org/grpc/status" "google.golang.org/grpc/status"
@@ -26,7 +25,7 @@ type circuitBreakerOptions struct {
func defaultCircuitBreakerOptions() *circuitBreakerOptions { func defaultCircuitBreakerOptions() *circuitBreakerOptions {
return &circuitBreakerOptions{ return &circuitBreakerOptions{
group: group.NewGroup(func() interface{} { group: group.NewGroup(func() interface{} {
return sre.NewBreaker() return circuitbreaker.NewBreaker()
}), }),
} }
} }

View File

@@ -6,8 +6,8 @@ import (
"github.com/zhufuyi/sponge/pkg/container/group" "github.com/zhufuyi/sponge/pkg/container/group"
"github.com/zhufuyi/sponge/pkg/errcode" "github.com/zhufuyi/sponge/pkg/errcode"
"github.com/zhufuyi/sponge/pkg/shield/circuitbreaker"
"github.com/go-kratos/aegis/circuitbreaker/sre"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
@@ -15,7 +15,7 @@ import (
func TestUnaryClientCircuitBreaker(t *testing.T) { func TestUnaryClientCircuitBreaker(t *testing.T) {
interceptor := UnaryClientCircuitBreaker(WithGroup( interceptor := UnaryClientCircuitBreaker(WithGroup(
group.NewGroup(func() interface{} { group.NewGroup(func() interface{} {
return sre.NewBreaker() return circuitbreaker.NewBreaker()
}), }),
)) ))
assert.NotNil(t, interceptor) assert.NotNil(t, interceptor)

View File

@@ -5,9 +5,8 @@ import (
"time" "time"
"github.com/zhufuyi/sponge/pkg/errcode" "github.com/zhufuyi/sponge/pkg/errcode"
rl "github.com/zhufuyi/sponge/pkg/shield/ratelimit"
rl "github.com/go-kratos/aegis/ratelimit"
"github.com/go-kratos/aegis/ratelimit/bbr"
"google.golang.org/grpc" "google.golang.org/grpc"
) )
@@ -73,11 +72,11 @@ func WithCPUQuota(quota float64) RatelimitOption {
func UnaryServerRateLimit(opts ...RatelimitOption) grpc.UnaryServerInterceptor { func UnaryServerRateLimit(opts ...RatelimitOption) grpc.UnaryServerInterceptor {
o := defaultRatelimitOptions() o := defaultRatelimitOptions()
o.apply(opts...) o.apply(opts...)
limiter := bbr.NewLimiter( limiter := rl.NewLimiter(
bbr.WithWindow(o.window), rl.WithWindow(o.window),
bbr.WithBucket(o.bucket), rl.WithBucket(o.bucket),
bbr.WithCPUThreshold(o.cpuThreshold), rl.WithCPUThreshold(o.cpuThreshold),
bbr.WithCPUQuota(o.cpuQuota), rl.WithCPUQuota(o.cpuQuota),
) )
return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) { return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (resp interface{}, err error) {
@@ -96,11 +95,11 @@ func UnaryServerRateLimit(opts ...RatelimitOption) grpc.UnaryServerInterceptor {
func StreamServerRateLimit(opts ...RatelimitOption) grpc.StreamServerInterceptor { func StreamServerRateLimit(opts ...RatelimitOption) grpc.StreamServerInterceptor {
o := defaultRatelimitOptions() o := defaultRatelimitOptions()
o.apply(opts...) o.apply(opts...)
limiter := bbr.NewLimiter( limiter := rl.NewLimiter(
bbr.WithWindow(o.window), rl.WithWindow(o.window),
bbr.WithBucket(o.bucket), rl.WithBucket(o.bucket),
bbr.WithCPUThreshold(o.cpuThreshold), rl.WithCPUThreshold(o.cpuThreshold),
bbr.WithCPUQuota(o.cpuQuota), rl.WithCPUQuota(o.cpuQuota),
) )
return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error { return func(srv interface{}, ss grpc.ServerStream, info *grpc.StreamServerInfo, handler grpc.StreamHandler) error {

View File

@@ -103,6 +103,15 @@ func srvRegisterMetrics() {
}) })
} }
// Register 注册http路由和grpc方法
func Register(mux *http.ServeMux, grpcServer *grpc.Server) {
// 注册http路由
mux.Handle("/metrics", promhttp.HandlerFor(srvReg, promhttp.HandlerOpts{}))
// 注册所有gRPC方法到metrics
grpcServerMetrics.InitializeMetrics(grpcServer)
}
// GoHTTPService 初始化服务端的prometheus的exporter服务使用 http://ip:port/metrics 获取数据 // GoHTTPService 初始化服务端的prometheus的exporter服务使用 http://ip:port/metrics 获取数据
func GoHTTPService(addr string, grpcServer *grpc.Server) *http.Server { func GoHTTPService(addr string, grpcServer *grpc.Server) *http.Server {
httpServer := &http.Server{ httpServer := &http.Server{

View File

@@ -18,11 +18,14 @@ const (
) )
var ( var (
r = rand.New(rand.NewSource(time.Now().UnixNano()))
refSlices = []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789") refSlices = []byte("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789")
kinds = [][]byte{refSlices[0:10], refSlices[10:36], refSlices[0:36], refSlices[36:62], refSlices[36:], refSlices[10:62], refSlices[0:62]} kinds = [][]byte{refSlices[0:10], refSlices[10:36], refSlices[0:36], refSlices[36:62], refSlices[36:], refSlices[10:62], refSlices[0:62]}
) )
func init() {
rand.Seed(time.Now().UnixNano())
}
// String 生成多种类型的任意长度的随机字符串如果参数size为空默认长度为6 // String 生成多种类型的任意长度的随机字符串如果参数size为空默认长度为6
// exampleString(R_ALL), String(R_ALL, 16), String(R_NUM|R_LOWER, 16) // exampleString(R_ALL), String(R_ALL, 16), String(R_NUM|R_LOWER, 16)
func String(kind int, size ...int) string { func String(kind int, size ...int) string {
@@ -46,7 +49,7 @@ func Bytes(kind int, bytesLen ...int) []byte {
result := make([]byte, length) result := make([]byte, length)
for i := 0; i < length; i++ { for i := 0; i < length; i++ {
result[i] = kinds[kind-1][r.Intn(len(kinds[kind-1]))] result[i] = kinds[kind-1][rand.Intn(len(kinds[kind-1]))]
} }
return result return result
@@ -56,14 +59,14 @@ func Bytes(kind int, bytesLen ...int) []byte {
func Int(rangeSize ...int) int { func Int(rangeSize ...int) int {
switch len(rangeSize) { switch len(rangeSize) {
case 0: case 0:
return r.Intn(101) // 默认0~100 return rand.Intn(101) // 默认0~100
case 1: case 1:
return r.Intn(rangeSize[0] + 1) return rand.Intn(rangeSize[0] + 1)
default: default:
if rangeSize[0] > rangeSize[1] { if rangeSize[0] > rangeSize[1] {
rangeSize[0], rangeSize[1] = rangeSize[1], rangeSize[0] rangeSize[0], rangeSize[1] = rangeSize[1], rangeSize[0]
} }
return r.Intn(rangeSize[1]-rangeSize[0]+1) + rangeSize[0] return rand.Intn(rangeSize[1]-rangeSize[0]+1) + rangeSize[0]
} }
} }
@@ -76,18 +79,18 @@ func Float64(dpLength int, rangeSize ...int) float64 {
for i := 0; i < dpLength; i++ { for i := 0; i < dpLength; i++ {
dpmax *= 10 dpmax *= 10
} }
dp = float64(r.Intn(dpmax)) / float64(dpmax) dp = float64(rand.Intn(dpmax)) / float64(dpmax)
} }
switch len(rangeSize) { switch len(rangeSize) {
case 0: case 0:
return float64(r.Intn(101)) + dp // 默认0~100 return float64(rand.Intn(101)) + dp // 默认0~100
case 1: case 1:
return float64(r.Intn(rangeSize[0]+1)) + dp return float64(rand.Intn(rangeSize[0]+1)) + dp
default: default:
if rangeSize[0] > rangeSize[1] { if rangeSize[0] > rangeSize[1] {
rangeSize[0], rangeSize[1] = rangeSize[1], rangeSize[0] rangeSize[0], rangeSize[1] = rangeSize[1], rangeSize[0]
} }
return float64(r.Intn(rangeSize[1]-rangeSize[0]+1)+rangeSize[0]) + dp return float64(rand.Intn(rangeSize[1]-rangeSize[0]+1)+rangeSize[0]) + dp
} }
} }

View File

@@ -1,6 +1,6 @@
## sql2code ## sql2code
根据sql生成不同用途代码支持生成json、gorm model、dao、handler代码sql可以从参数、文件、db三种方式获取优先从高到低。 根据sql生成不同用途代码支持生成json、gorm model、update参数、request 参数代码sql可以从参数、文件、db三种方式获取优先从高到低。
<br> <br>