Documentation ¶
Index ¶
- Variables
- func AccessLog() app.HandlerFunc
- func CommonMiddleware(next endpoint.Endpoint) endpoint.Endpoint
- func ServerMiddleware(next endpoint.Endpoint) endpoint.Endpoint
- func TokenAuthMiddleware(jwt JWT, skipRoutes ...string) app.HandlerFunc
- func TokenLimitMiddleware() app.HandlerFunc
- type CustomClaims
- type JWT
- type TokenBucket
- type TokenBuckets
Constants ¶
This section is empty.
Variables ¶
View Source
var ( ErrTokenExpired = errors.New("token expired") ErrTokenNotValidYet = errors.New("token is not active yet") ErrTokenMalformed = errors.New("that's not even a token") ErrTokenInvalid = errors.New("couldn't handle this token") )
View Source
var (
CurrentLimiter = MakeTokenBuckets(int64(limiterCapacity), int64(limiterRate)) // 后续用redis缓存每个token对应的令牌桶
)
对每个token限流,不管它请求的API
Functions ¶
func AccessLog ¶
func AccessLog() app.HandlerFunc
func ServerMiddleware ¶
ServerMiddleware server middleware print client address
func TokenAuthMiddleware ¶
func TokenAuthMiddleware(jwt JWT, skipRoutes ...string) app.HandlerFunc
func TokenLimitMiddleware ¶
func TokenLimitMiddleware() app.HandlerFunc
TokenLimitMiddleware 限流中间件,使用令牌桶的方式处理请求。Note: auth中间件需在其前面
Types ¶
type CustomClaims ¶
type CustomClaims struct { Id int64 jwt.StandardClaims }
CustomClaims Structured version of Claims Section, as referenced at https://tools.ietf.org/html/rfc7519#section-4.1 See examples for how to use this with your own claim types
type JWT ¶
type JWT struct {
SigningKey []byte
}
JWT signing Key
func (*JWT) CreateToken ¶
func (j *JWT) CreateToken(claims CustomClaims) (string, error)
create a new token
func (*JWT) ParseToken ¶
func (j *JWT) ParseToken(tokenString string) (*CustomClaims, error)
ParseToken parse the token.
type TokenBucket ¶
type TokenBucket struct { Rate int64 // 固定的token放入速率,r/s Capacity int64 // 桶的容量 Tokens int64 // 桶中当前token数量 LastTokenSec int64 // 桶上次放token的时间 // contains filtered or unexported fields }
func MakeTokenBucket ¶
func MakeTokenBucket(c, r int64) *TokenBucket
func (*TokenBucket) Allow ¶
func (tb *TokenBucket) Allow() bool
type TokenBuckets ¶
type TokenBuckets struct {
// contains filtered or unexported fields
}
func MakeTokenBuckets ¶
func MakeTokenBuckets(capacity, rate int64) *TokenBuckets
func (*TokenBuckets) Allow ¶
func (tbs *TokenBuckets) Allow(token string) bool
Source Files ¶
Click to show internal directories.
Click to hide internal directories.