mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-02 16:28:39 +08:00
chore: refactor zrpc setup (#3064)
This commit is contained in:
parent
992a56e90b
commit
04434646eb
@ -109,30 +109,38 @@ func SetServerSlowThreshold(threshold time.Duration) {
|
||||
serverinterceptors.SetSlowThreshold(threshold)
|
||||
}
|
||||
|
||||
func setupInterceptors(server internal.Server, c RpcServerConf, metrics *stat.Metrics) error {
|
||||
func setupAuthInterceptors(svr internal.Server, c RpcServerConf) error {
|
||||
rds, err := redis.NewRedis(c.Redis.RedisConf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
authenticator, err := auth.NewAuthenticator(rds, c.Redis.Key, c.StrictControl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
svr.AddStreamInterceptors(serverinterceptors.StreamAuthorizeInterceptor(authenticator))
|
||||
svr.AddUnaryInterceptors(serverinterceptors.UnaryAuthorizeInterceptor(authenticator))
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func setupInterceptors(svr internal.Server, c RpcServerConf, metrics *stat.Metrics) error {
|
||||
if c.CpuThreshold > 0 {
|
||||
shedder := load.NewAdaptiveShedder(load.WithCpuThreshold(c.CpuThreshold))
|
||||
server.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics))
|
||||
svr.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics))
|
||||
}
|
||||
|
||||
if c.Timeout > 0 {
|
||||
server.AddUnaryInterceptors(serverinterceptors.UnaryTimeoutInterceptor(
|
||||
svr.AddUnaryInterceptors(serverinterceptors.UnaryTimeoutInterceptor(
|
||||
time.Duration(c.Timeout) * time.Millisecond))
|
||||
}
|
||||
|
||||
if c.Auth {
|
||||
rds, err := redis.NewRedis(c.Redis.RedisConf)
|
||||
if err != nil {
|
||||
if err := setupAuthInterceptors(svr, c); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
authenticator, err := auth.NewAuthenticator(rds, c.Redis.Key, c.StrictControl)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
server.AddStreamInterceptors(serverinterceptors.StreamAuthorizeInterceptor(authenticator))
|
||||
server.AddUnaryInterceptors(serverinterceptors.UnaryAuthorizeInterceptor(authenticator))
|
||||
}
|
||||
|
||||
return nil
|
||||
|
Loading…
Reference in New Issue
Block a user