From 04434646eb38a713fc5d429290eb9b546f10576f Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sat, 25 Mar 2023 20:40:21 +0800 Subject: [PATCH] chore: refactor zrpc setup (#3064) --- zrpc/server.go | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/zrpc/server.go b/zrpc/server.go index 5327b83e..158d8109 100644 --- a/zrpc/server.go +++ b/zrpc/server.go @@ -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 { - return err - } - - authenticator, err := auth.NewAuthenticator(rds, c.Redis.Key, c.StrictControl) - if err != nil { + if err := setupAuthInterceptors(svr, c); err != nil { return err } - - server.AddStreamInterceptors(serverinterceptors.StreamAuthorizeInterceptor(authenticator)) - server.AddUnaryInterceptors(serverinterceptors.UnaryAuthorizeInterceptor(authenticator)) } return nil