diff --git a/rpcx/client.go b/rpcx/client.go index 5b1540c6..072ebeb3 100644 --- a/rpcx/client.go +++ b/rpcx/client.go @@ -4,18 +4,18 @@ import ( "log" "time" - "zero/core/discov" - "zero/core/rpc" - "zero/rpcx/auth" - "google.golang.org/grpc" + + "zero/core/discov" + "zero/rpcx/internal" + "zero/rpcx/internal/auth" ) type RpcClient struct { - client rpc.Client + client internal.Client } -func MustNewClient(c RpcClientConf, options ...rpc.ClientOption) *RpcClient { +func MustNewClient(c RpcClientConf, options ...internal.ClientOption) *RpcClient { cli, err := NewClient(c, options...) if err != nil { log.Fatal(err) @@ -24,25 +24,25 @@ func MustNewClient(c RpcClientConf, options ...rpc.ClientOption) *RpcClient { return cli } -func NewClient(c RpcClientConf, options ...rpc.ClientOption) (*RpcClient, error) { - var opts []rpc.ClientOption +func NewClient(c RpcClientConf, options ...internal.ClientOption) (*RpcClient, error) { + var opts []internal.ClientOption if c.HasCredential() { - opts = append(opts, rpc.WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{ + opts = append(opts, internal.WithDialOption(grpc.WithPerRPCCredentials(&auth.Credential{ App: c.App, Token: c.Token, }))) } if c.Timeout > 0 { - opts = append(opts, rpc.WithTimeout(time.Duration(c.Timeout)*time.Millisecond)) + opts = append(opts, internal.WithTimeout(time.Duration(c.Timeout)*time.Millisecond)) } opts = append(opts, options...) - var client rpc.Client + var client internal.Client var err error if len(c.Server) > 0 { - client, err = rpc.NewDirectClient(c.Server, opts...) + client, err = internal.NewDirectClient(c.Server, opts...) } else if err = c.Etcd.Validate(); err == nil { - client, err = rpc.NewRoundRobinRpcClient(c.Etcd.Hosts, c.Etcd.Key, opts...) + client, err = internal.NewRoundRobinRpcClient(c.Etcd.Hosts, c.Etcd.Key, opts...) } if err != nil { return nil, err @@ -54,7 +54,7 @@ func NewClient(c RpcClientConf, options ...rpc.ClientOption) (*RpcClient, error) } func NewClientNoAuth(c discov.EtcdConf) (*RpcClient, error) { - client, err := rpc.NewRoundRobinRpcClient(c.Hosts, c.Key) + client, err := internal.NewRoundRobinRpcClient(c.Hosts, c.Key) if err != nil { return nil, err } diff --git a/rpcx/auth/auth.go b/rpcx/internal/auth/auth.go similarity index 100% rename from rpcx/auth/auth.go rename to rpcx/internal/auth/auth.go diff --git a/rpcx/auth/credential.go b/rpcx/internal/auth/credential.go similarity index 100% rename from rpcx/auth/credential.go rename to rpcx/internal/auth/credential.go diff --git a/rpcx/auth/vars.go b/rpcx/internal/auth/vars.go similarity index 100% rename from rpcx/auth/vars.go rename to rpcx/internal/auth/vars.go diff --git a/rpcx/interceptors/authinterceptor.go b/rpcx/internal/authinterceptor.go similarity index 94% rename from rpcx/interceptors/authinterceptor.go rename to rpcx/internal/authinterceptor.go index efac6d98..be79fbff 100644 --- a/rpcx/interceptors/authinterceptor.go +++ b/rpcx/internal/authinterceptor.go @@ -1,9 +1,9 @@ -package interceptors +package internal import ( "context" - "zero/rpcx/auth" + "zero/rpcx/internal/auth" "google.golang.org/grpc" ) diff --git a/core/rpc/chainclientinterceptors.go b/rpcx/internal/chainclientinterceptors.go similarity index 99% rename from core/rpc/chainclientinterceptors.go rename to rpcx/internal/chainclientinterceptors.go index d2f04c0e..bd3f2d92 100644 --- a/core/rpc/chainclientinterceptors.go +++ b/rpcx/internal/chainclientinterceptors.go @@ -1,4 +1,4 @@ -package rpc +package internal import ( "context" diff --git a/core/rpc/chainserverinterceptors.go b/rpcx/internal/chainserverinterceptors.go similarity index 99% rename from core/rpc/chainserverinterceptors.go rename to rpcx/internal/chainserverinterceptors.go index 9c3eec99..73768944 100644 --- a/core/rpc/chainserverinterceptors.go +++ b/rpcx/internal/chainserverinterceptors.go @@ -1,4 +1,4 @@ -package rpc +package internal import ( "context" diff --git a/core/rpc/client.go b/rpcx/internal/client.go similarity index 96% rename from core/rpc/client.go rename to rpcx/internal/client.go index e88386aa..58ed3e29 100644 --- a/core/rpc/client.go +++ b/rpcx/internal/client.go @@ -1,11 +1,11 @@ -package rpc +package internal import ( "context" "fmt" "time" - "zero/core/rpc/clientinterceptors" + "zero/rpcx/internal/clientinterceptors" "google.golang.org/grpc" ) diff --git a/core/rpc/clientinterceptors/breakerinterceptor.go b/rpcx/internal/clientinterceptors/breakerinterceptor.go similarity index 100% rename from core/rpc/clientinterceptors/breakerinterceptor.go rename to rpcx/internal/clientinterceptors/breakerinterceptor.go diff --git a/core/rpc/clientinterceptors/breakerinterceptor_test.go b/rpcx/internal/clientinterceptors/breakerinterceptor_test.go similarity index 100% rename from core/rpc/clientinterceptors/breakerinterceptor_test.go rename to rpcx/internal/clientinterceptors/breakerinterceptor_test.go diff --git a/core/rpc/clientinterceptors/durationinterceptor.go b/rpcx/internal/clientinterceptors/durationinterceptor.go similarity index 100% rename from core/rpc/clientinterceptors/durationinterceptor.go rename to rpcx/internal/clientinterceptors/durationinterceptor.go diff --git a/core/rpc/clientinterceptors/prommetricinterceptor.go b/rpcx/internal/clientinterceptors/prommetricinterceptor.go similarity index 100% rename from core/rpc/clientinterceptors/prommetricinterceptor.go rename to rpcx/internal/clientinterceptors/prommetricinterceptor.go diff --git a/core/rpc/clientinterceptors/timeoutinterceptor.go b/rpcx/internal/clientinterceptors/timeoutinterceptor.go similarity index 100% rename from core/rpc/clientinterceptors/timeoutinterceptor.go rename to rpcx/internal/clientinterceptors/timeoutinterceptor.go diff --git a/core/rpc/clientinterceptors/tracinginterceptor.go b/rpcx/internal/clientinterceptors/tracinginterceptor.go similarity index 100% rename from core/rpc/clientinterceptors/tracinginterceptor.go rename to rpcx/internal/clientinterceptors/tracinginterceptor.go diff --git a/core/rpc/directclient.go b/rpcx/internal/directclient.go similarity index 97% rename from core/rpc/directclient.go rename to rpcx/internal/directclient.go index 3302a077..a7373fa3 100644 --- a/core/rpc/directclient.go +++ b/rpcx/internal/directclient.go @@ -1,4 +1,4 @@ -package rpc +package internal import ( "google.golang.org/grpc" diff --git a/core/rpc/rpclogger.go b/rpcx/internal/rpclogger.go similarity index 98% rename from core/rpc/rpclogger.go rename to rpcx/internal/rpclogger.go index 86e2fcab..f82e2dab 100644 --- a/core/rpc/rpclogger.go +++ b/rpcx/internal/rpclogger.go @@ -1,4 +1,4 @@ -package rpc +package internal import ( "sync" diff --git a/core/rpc/rpcpubserver.go b/rpcx/internal/rpcpubserver.go similarity index 97% rename from core/rpc/rpcpubserver.go rename to rpcx/internal/rpcpubserver.go index e7848bb3..61f827aa 100644 --- a/core/rpc/rpcpubserver.go +++ b/rpcx/internal/rpcpubserver.go @@ -1,4 +1,4 @@ -package rpc +package internal import "zero/core/discov" diff --git a/core/rpc/rpcserver.go b/rpcx/internal/rpcserver.go similarity index 96% rename from core/rpc/rpcserver.go rename to rpcx/internal/rpcserver.go index 6305778d..370beb0c 100644 --- a/core/rpc/rpcserver.go +++ b/rpcx/internal/rpcserver.go @@ -1,11 +1,11 @@ -package rpc +package internal import ( "net" "zero/core/proc" - "zero/core/rpc/serverinterceptors" "zero/core/stat" + "zero/rpcx/internal/serverinterceptors" "google.golang.org/grpc" ) diff --git a/core/rpc/rpcsubclient.go b/rpcx/internal/rpcsubclient.go similarity index 99% rename from core/rpc/rpcsubclient.go rename to rpcx/internal/rpcsubclient.go index 7d0c1436..47347ef2 100644 --- a/core/rpc/rpcsubclient.go +++ b/rpcx/internal/rpcsubclient.go @@ -1,4 +1,4 @@ -package rpc +package internal import ( "time" diff --git a/core/rpc/rrclient.go b/rpcx/internal/rrclient.go similarity index 97% rename from core/rpc/rrclient.go rename to rpcx/internal/rrclient.go index 243c44e5..d0228be1 100644 --- a/core/rpc/rrclient.go +++ b/rpcx/internal/rrclient.go @@ -1,4 +1,4 @@ -package rpc +package internal import ( "math/rand" diff --git a/core/rpc/server.go b/rpcx/internal/server.go similarity index 98% rename from core/rpc/server.go rename to rpcx/internal/server.go index c03b5a30..47b59bc0 100644 --- a/core/rpc/server.go +++ b/rpcx/internal/server.go @@ -1,4 +1,4 @@ -package rpc +package internal import ( "zero/core/stat" diff --git a/core/rpc/serverinterceptors/crashinterceptor.go b/rpcx/internal/serverinterceptors/crashinterceptor.go similarity index 100% rename from core/rpc/serverinterceptors/crashinterceptor.go rename to rpcx/internal/serverinterceptors/crashinterceptor.go diff --git a/core/rpc/serverinterceptors/prommetricinterceptor.go b/rpcx/internal/serverinterceptors/prommetricinterceptor.go similarity index 100% rename from core/rpc/serverinterceptors/prommetricinterceptor.go rename to rpcx/internal/serverinterceptors/prommetricinterceptor.go diff --git a/core/rpc/serverinterceptors/sheddinginterceptor.go b/rpcx/internal/serverinterceptors/sheddinginterceptor.go similarity index 100% rename from core/rpc/serverinterceptors/sheddinginterceptor.go rename to rpcx/internal/serverinterceptors/sheddinginterceptor.go diff --git a/core/rpc/serverinterceptors/statinterceptor.go b/rpcx/internal/serverinterceptors/statinterceptor.go similarity index 100% rename from core/rpc/serverinterceptors/statinterceptor.go rename to rpcx/internal/serverinterceptors/statinterceptor.go diff --git a/core/rpc/serverinterceptors/timeoutinterceptor.go b/rpcx/internal/serverinterceptors/timeoutinterceptor.go similarity index 100% rename from core/rpc/serverinterceptors/timeoutinterceptor.go rename to rpcx/internal/serverinterceptors/timeoutinterceptor.go diff --git a/core/rpc/serverinterceptors/tracinginterceptor.go b/rpcx/internal/serverinterceptors/tracinginterceptor.go similarity index 100% rename from core/rpc/serverinterceptors/tracinginterceptor.go rename to rpcx/internal/serverinterceptors/tracinginterceptor.go diff --git a/rpcx/proxy.go b/rpcx/proxy.go index f720d49c..ef85f204 100644 --- a/rpcx/proxy.go +++ b/rpcx/proxy.go @@ -4,22 +4,22 @@ import ( "context" "sync" - "zero/core/rpc" - "zero/core/syncx" - "zero/rpcx/auth" - "google.golang.org/grpc" + + "zero/core/syncx" + "zero/rpcx/internal" + "zero/rpcx/internal/auth" ) type RpcProxy struct { backend string clients map[string]*RpcClient - options []rpc.ClientOption + options []internal.ClientOption sharedCalls syncx.SharedCalls lock sync.Mutex } -func NewRpcProxy(backend string, opts ...rpc.ClientOption) *RpcProxy { +func NewRpcProxy(backend string, opts ...internal.ClientOption) *RpcProxy { return &RpcProxy{ backend: backend, clients: make(map[string]*RpcClient), diff --git a/rpcx/server.go b/rpcx/server.go index ffa28df4..eb87b1ee 100644 --- a/rpcx/server.go +++ b/rpcx/server.go @@ -9,21 +9,20 @@ import ( "zero/core/load" "zero/core/logx" "zero/core/netx" - "zero/core/rpc" - "zero/core/rpc/serverinterceptors" "zero/core/stat" - "zero/rpcx/auth" - "zero/rpcx/interceptors" + "zero/rpcx/internal" + "zero/rpcx/internal/auth" + "zero/rpcx/internal/serverinterceptors" ) const envPodIp = "POD_IP" type RpcServer struct { - server rpc.Server - register rpc.RegisterFn + server internal.Server + register internal.RegisterFn } -func MustNewServer(c RpcServerConf, register rpc.RegisterFn) *RpcServer { +func MustNewServer(c RpcServerConf, register internal.RegisterFn) *RpcServer { server, err := NewServer(c, register) if err != nil { log.Fatal(err) @@ -32,22 +31,22 @@ func MustNewServer(c RpcServerConf, register rpc.RegisterFn) *RpcServer { return server } -func NewServer(c RpcServerConf, register rpc.RegisterFn) (*RpcServer, error) { +func NewServer(c RpcServerConf, register internal.RegisterFn) (*RpcServer, error) { var err error if err = c.Validate(); err != nil { return nil, err } - var server rpc.Server + var server internal.Server metrics := stat.NewMetrics(c.ListenOn) if c.HasEtcd() { listenOn := figureOutListenOn(c.ListenOn) - server, err = rpc.NewRpcPubServer(c.Etcd.Hosts, c.Etcd.Key, listenOn, rpc.WithMetrics(metrics)) + server, err = internal.NewRpcPubServer(c.Etcd.Hosts, c.Etcd.Key, listenOn, internal.WithMetrics(metrics)) if err != nil { return nil, err } } else { - server = rpc.NewRpcServer(c.ListenOn, rpc.WithMetrics(metrics)) + server = internal.NewRpcServer(c.ListenOn, internal.WithMetrics(metrics)) } server.SetName(c.Name) @@ -99,7 +98,7 @@ func figureOutListenOn(listenOn string) string { } } -func setupInterceptors(server rpc.Server, c RpcServerConf, metrics *stat.Metrics) error { +func setupInterceptors(server 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)) @@ -118,8 +117,8 @@ func setupInterceptors(server rpc.Server, c RpcServerConf, metrics *stat.Metrics return err } - server.AddStreamInterceptors(interceptors.StreamAuthorizeInterceptor(authenticator)) - server.AddUnaryInterceptors(interceptors.UnaryAuthorizeInterceptor(authenticator)) + server.AddStreamInterceptors(internal.StreamAuthorizeInterceptor(authenticator)) + server.AddUnaryInterceptors(internal.UnaryAuthorizeInterceptor(authenticator)) } return nil