chore: refactor zrpc setup (#3064)

master
Kevin Wan 2 years ago committed by GitHub
parent 992a56e90b
commit 04434646eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -109,30 +109,38 @@ func SetServerSlowThreshold(threshold time.Duration) {
serverinterceptors.SetSlowThreshold(threshold) 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 { if c.CpuThreshold > 0 {
shedder := load.NewAdaptiveShedder(load.WithCpuThreshold(c.CpuThreshold)) shedder := load.NewAdaptiveShedder(load.WithCpuThreshold(c.CpuThreshold))
server.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics)) svr.AddUnaryInterceptors(serverinterceptors.UnarySheddingInterceptor(shedder, metrics))
} }
if c.Timeout > 0 { if c.Timeout > 0 {
server.AddUnaryInterceptors(serverinterceptors.UnaryTimeoutInterceptor( svr.AddUnaryInterceptors(serverinterceptors.UnaryTimeoutInterceptor(
time.Duration(c.Timeout) * time.Millisecond)) time.Duration(c.Timeout) * time.Millisecond))
} }
if c.Auth { if c.Auth {
rds, err := redis.NewRedis(c.Redis.RedisConf) if err := setupAuthInterceptors(svr, c); err != nil {
if err != nil {
return err
}
authenticator, err := auth.NewAuthenticator(rds, c.Redis.Key, c.StrictControl)
if err != nil {
return err return err
} }
server.AddStreamInterceptors(serverinterceptors.StreamAuthorizeInterceptor(authenticator))
server.AddUnaryInterceptors(serverinterceptors.UnaryAuthorizeInterceptor(authenticator))
} }
return nil return nil

Loading…
Cancel
Save