ginmiddleware

package module
v0.0.0-...-30b2d24 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 16, 2024 License: MIT Imports: 11 Imported by: 0

README

middleware 中间件

limit_conn_test.go 限连接模块

TestLimitConn

router := gin.New()
lr := NewLimitConn(reqHostIp)

router.Use(lr.Incoming(nil, 10, 4))
router.GET("/testurl", func(c *gin.Context) {
	time.Sleep(time.Second)
	fmt.Println("enter")
	c.String(http.StatusOK, "ok!!")
}, lr.Leaving(nil))

safego.Go(func() {
	router.Run(":8081")
})

time.Sleep(time.Second * 3)

for j := 0; j < 5; j++ {
	time.Sleep(time.Second * 1)
	for i := 0; i < 20; i++ {
		safego.Go(func() {
			resp, err := http.Get("http://127.0.0.1:8081/testurl")
			if err != nil {
				fmt.Println(err)
			} else {
				if 200 != resp.StatusCode {
					fmt.Println("点击太快了", resp.StatusCode)
				}
			}

		})
	}
}

//w1 := utils.PerformTestRequest("GET", "/testurl", router)
//if 200 == w1.Code {
//	fmt.Println("okk")
//}
time.Sleep(time.Minute * 20)

limit_req_test.go 限流模块

TestLimitReq

router := gin.New()
lr := NewLimitReq(reqHostIp)

router.Use(lr.Incoming(nil, 10, 4))
router.GET("/testurl", func(c *gin.Context) {
	time.Sleep(time.Second)
	fmt.Println("enter")
	c.String(http.StatusOK, "ok!!")
})

safego.Go(func() {
	router.Run(":8080")
})

time.Sleep(time.Second * 3)

for j := 0; j < 5; j++ {
	time.Sleep(time.Second * 1)
	for i := 0; i < 20; i++ {
		safego.Go(func() {
			resp, err := http.Get("http://127.0.0.1:8080/testurl")
			if err != nil {
				fmt.Println(err)
			} else {
				if 200 != resp.StatusCode {
					fmt.Println("点击太快了", resp.StatusCode)
				}
			}

		})
	}
}

//w1 := utils.PerformTestRequest("GET", "/testurl", router)
//if 200 == w1.Code {
//	fmt.Println("okk")
//}
time.Sleep(time.Minute * 20)

service_handler_test.go service封装器

TestServiceHandler

router := gin.New()
router.POST("/foo", ServiceHandler(serviceFoo, fooReq{}, nil))

router.Run(":8080")

Documentation

Index

Constants

View Source
const (
	Success   = 0  // 成功
	Unknown   = -1 // 未知错误
	WrongArgs = -2 // 参数错误
)

Variables

This section is empty.

Functions

func ServiceHandler

func ServiceHandler(serviceFunc ServiceFunc, reqVal interface{}, respFactory ServiceRespFactory) func(*gin.Context)

Types

type DefaultServiceRespFactory

type DefaultServiceRespFactory struct {
}

func (*DefaultServiceRespFactory) IsJsonResponse

func (t *DefaultServiceRespFactory) IsJsonResponse(data interface{}) bool

func (*DefaultServiceRespFactory) NewDataResponse

func (t *DefaultServiceRespFactory) NewDataResponse(data interface{}, tag string) ServiceResponse

func (*DefaultServiceRespFactory) NewErrRespWithCode

func (t *DefaultServiceRespFactory) NewErrRespWithCode(code int, err error, data interface{}, tag string) ServiceResponse

type DefaultServiceResponse

type DefaultServiceResponse struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Tag  string      `json:"tag,omitempty"`
	Data interface{} `json:"data"`
}

func (*DefaultServiceResponse) GetCode

func (t *DefaultServiceResponse) GetCode() int

func (*DefaultServiceResponse) GetMsg

func (t *DefaultServiceResponse) GetMsg() string

type Errorx3

type Errorx3 interface {
	Error() string
	Code() int
	LogLevel() zapcore.Level
}

type LimitConn

type LimitConn struct {
	// contains filtered or unexported fields
}

func NewLimitConn

func NewLimitConn(keyFunc LimitKeyFunc) *LimitConn

func (*LimitConn) Incoming

func (l *LimitConn) Incoming(keyFunc LimitKeyFunc, max, burst int) gin.HandlerFunc

func (*LimitConn) Leaving

func (l *LimitConn) Leaving(keyFunc LimitKeyFunc) gin.HandlerFunc

type LimitKeyFunc

type LimitKeyFunc func(*gin.Context) (string, error)

type LimitReq

type LimitReq struct {
	// contains filtered or unexported fields
}

func NewLimitReq

func NewLimitReq(keyFunc LimitKeyFunc) *LimitReq

func (*LimitReq) Incoming

func (l *LimitReq) Incoming(keyFunc LimitKeyFunc, rate float64, burst int) gin.HandlerFunc

type ServiceFunc

type ServiceFunc func(context.Context, interface{}) (interface{}, error)

type ServiceRespFactory

type ServiceRespFactory interface {
	IsJsonResponse(data interface{}) bool
	NewErrRespWithCode(code int, err error, data interface{}, tag string) ServiceResponse
	NewDataResponse(data interface{}, tag string) ServiceResponse
}

type ServiceResponse

type ServiceResponse interface {
	GetCode() int
	GetMsg() string
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL