Skip to content

Commit

Permalink
refactor:重构grpc-go-polaris的相关版本设计
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun committed Jun 20, 2024
1 parent 1a6227d commit 3a615f1
Show file tree
Hide file tree
Showing 12 changed files with 1,029 additions and 862 deletions.
2 changes: 0 additions & 2 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ import (
"fmt"
"strings"

"google.golang.org/grpc/metadata"

"google.golang.org/grpc"
"google.golang.org/grpc/metadata"
)
Expand Down
3 changes: 0 additions & 3 deletions client_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ import (
"github.com/polarismesh/polaris-go/api"
"github.com/polarismesh/polaris-go/pkg/config"
"google.golang.org/grpc"

"github.com/polarismesh/polaris-go/pkg/config"
"google.golang.org/grpc"
)

// DialOption dialOptions for gRPC-Go-Polaris
Expand Down
4 changes: 0 additions & 4 deletions examples/circuitbreak/consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ import (
"strings"
"time"

polaris "github.com/polarismesh/grpc-go-polaris"
"github.com/polarismesh/polaris-go/pkg/model"
"google.golang.org/grpc/balancer"

"github.com/polarismesh/polaris-go/api"
"github.com/polarismesh/polaris-go/pkg/model"
"google.golang.org/grpc"
Expand Down
2 changes: 1 addition & 1 deletion examples/circuitbreak/consumer/polaris.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global:
serverConnector:
addresses:
- 183.47.111.80:8091
- 127.0.0.1:8091
2 changes: 1 addition & 1 deletion examples/circuitbreak/provider/polaris.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global:
serverConnector:
addresses:
- 183.47.111.80:8091
- 127.0.0.1:8091
2 changes: 0 additions & 2 deletions examples/quickstart/consumer/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ import (
"log"
"net/http"

"google.golang.org/grpc/metadata"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
Expand Down
2 changes: 1 addition & 1 deletion examples/ratelimit/provider/polaris.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global:
serverConnector:
addresses:
- 183.47.111.80:8091
- 127.0.0.1:8091
2 changes: 1 addition & 1 deletion examples/routing/provider/polaris.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
global:
serverConnector:
addresses:
- 183.47.111.80:8091
- 127.0.0.1:8091
27 changes: 14 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,21 @@ module github.com/polarismesh/grpc-go-polaris
go 1.15

require (
github.com/dlclark/regexp2 v1.10.0 // indirect
github.com/golang/protobuf v1.5.3
github.com/google/uuid v1.3.0
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/dlclark/regexp2 v1.11.0 // indirect
github.com/golang/protobuf v1.5.4
github.com/google/uuid v1.6.0
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/polarismesh/polaris-go v1.5.2
github.com/polarismesh/specification v1.3.2
github.com/prometheus/client_golang v1.16.0 // indirect
github.com/prometheus/common v0.44.0 // indirect
github.com/prometheus/procfs v0.11.1 // indirect
github.com/polarismesh/polaris-go v1.5.6
github.com/polarismesh/specification v1.5.1
github.com/prometheus/client_golang v1.19.1 // indirect
github.com/prometheus/common v0.54.0 // indirect
github.com/prometheus/procfs v0.15.1 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.25.0 // indirect
golang.org/x/net v0.14.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20230803162519-f966b187b2e5 // indirect
google.golang.org/grpc v1.57.0
google.golang.org/protobuf v1.31.0
go.uber.org/zap v1.27.0 // indirect
golang.org/x/net v0.26.0
google.golang.org/genproto/googleapis/rpc v0.0.0-20240617180043-68d350f18fd4 // indirect
google.golang.org/grpc v1.64.0
google.golang.org/protobuf v1.34.2
gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c
)
1,691 changes: 1,002 additions & 689 deletions go.sum

Large diffs are not rendered by default.

145 changes: 0 additions & 145 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,148 +272,3 @@ func deregisterServices(registerContext *RegisterContext) {
deregisterRequest.Host, deregisterRequest.Port, deregisterRequest.Service, deregisterRequest.Namespace)
}
}

// RegisterContext context parameters by register
type RegisterContext struct {
providerAPI api.ProviderAPI
registerRequests []*api.InstanceRegisterRequest
}

// Serve start polaris server
func Serve(gSrv *grpc.Server, lis net.Listener, opts ...ServerOption) error {
pSrv, err := Register(gSrv, lis, opts...)
if err != nil {
log.Fatalf("polaris register err: %v", err)
}

go func() {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
s := <-c
log.Printf("[Polaris][Naming] receive quit signal: %v", s)
signal.Stop(c)
pSrv.Stop()
}()

return gSrv.Serve(lis)
}

// Stop deregister and stop
func (s *Server) Stop() {
s.Deregister()

if !*s.serverOptions.gracefulStopEnable {
s.gServer.Stop()
return
}

ctx, cancel := context.WithDeadline(context.Background(),
time.Now().Add(s.serverOptions.gracefulStopMaxWaitDuration))
go func() {
s.gServer.GracefulStop()
cancel()
}()

<-ctx.Done()
}

// Register server as polaris instances
func Register(gSrv *grpc.Server, lis net.Listener, opts ...ServerOption) (*Server, error) {
srv := &Server{gServer: gSrv}
for _, opt := range opts {
opt.apply(&srv.serverOptions)
}
srv.serverOptions.setDefault()
svcInfos := buildServiceNames(srv.gServer, srv)

registerContext := &RegisterContext{}
if len(svcInfos) == 0 {
srv.registerContext = registerContext
return srv, nil
}
polarisCtx := srv.serverOptions.SDKContext
if polarisCtx == nil {
var err error
polarisCtx, err = PolarisContext()
if nil != err {
return nil, err
}
}
if len(srv.serverOptions.host) == 0 {
host, err := getLocalHost(polarisCtx.GetConfig().GetGlobal().GetServerConnector().GetAddresses()[0])
if nil != err {
return nil, fmt.Errorf("error occur while fetching localhost: %w", err)
}
srv.serverOptions.host = host
}
if srv.serverOptions.port == 0 {
port, err := parsePort(lis.Addr().String())
if nil != err {
return nil, fmt.Errorf("error occur while parsing port from listener: %w", err)
}
srv.serverOptions.port = port
}

if *srv.serverOptions.delayRegisterEnable {
delayStrategy := srv.serverOptions.delayRegisterStrategy
for {
if delayStrategy.Allow() {
break
}
time.Sleep(100 * time.Millisecond)
}
}

registerContext.registerRequests = make([]*api.InstanceRegisterRequest, 0, len(svcInfos))
registerContext.providerAPI = api.NewProviderAPIByContext(polarisCtx)

for _, name := range svcInfos {
registerRequest := buildRegisterInstanceRequest(srv, name)
registerContext.registerRequests = append(registerContext.registerRequests, registerRequest)
resp, err := registerContext.providerAPI.RegisterInstance(registerRequest)
if nil != err {
deregisterServices(registerContext)
return nil, fmt.Errorf("fail to register service %s: %w", name, err)
}
grpclog.Infof("[Polaris][Naming] success to register %s:%d to service %s(%s), id %s",
registerRequest.Host, registerRequest.Port, name, registerRequest.Namespace, resp.InstanceID)
}

srv.registerContext = registerContext
return srv, nil
}

func buildServiceNames(gSrv *grpc.Server, svr *Server) []string {
svcInfo := gSrv.GetServiceInfo()
ret := make([]string, 0, len(svcInfo))
for k := range svcInfo {
ret = append(ret, k)
}

if len(svr.serverOptions.svcName) != 0 {
ret = []string{
svr.serverOptions.svcName,
}
}

return ret
}

func buildRegisterInstanceRequest(srv *Server, serviceName string) *api.InstanceRegisterRequest {
registerRequest := &api.InstanceRegisterRequest{}
registerRequest.Namespace = srv.serverOptions.namespace
registerRequest.Service = serviceName
registerRequest.Host = srv.serverOptions.host
registerRequest.Port = srv.serverOptions.port
registerRequest.Protocol = proto.String("grpc")
registerRequest.Metadata = srv.serverOptions.metadata
registerRequest.Version = proto.String(srv.serverOptions.version)
registerRequest.ServiceToken = srv.serverOptions.token
registerRequest.SetTTL(srv.serverOptions.ttl)
return registerRequest
}

// Deregister deregister services from polaris
func (s *Server) Deregister() {
deregisterServices(s.registerContext)
}
9 changes: 9 additions & 0 deletions server_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import (
"time"

"github.com/polarismesh/polaris-go/api"
"github.com/polarismesh/polaris-go/pkg/config"
"google.golang.org/grpc"
)

type serverOptions struct {
gRPCServerOptions []grpc.ServerOption
SDKContext api.SDKContext
config config.Configuration
namespace string
svcName string
ttl int
Expand Down Expand Up @@ -270,6 +272,13 @@ func WithServerPolarisConfig(polarisCfg config.Configuration) ServerOption {
})
}

// WithPolarisContext set polaris SDKContext

Check failure on line 275 in server_option.go

View workflow job for this annotation

GitHub Actions / build (1.17)

comment on exported function WithServerPolarisContext should be of the form "WithServerPolarisContext ..."

Check failure on line 275 in server_option.go

View workflow job for this annotation

GitHub Actions / build (1.15)

comment on exported function WithServerPolarisContext should be of the form "WithServerPolarisContext ..."

Check failure on line 275 in server_option.go

View workflow job for this annotation

GitHub Actions / build (1.16)

comment on exported function WithServerPolarisContext should be of the form "WithServerPolarisContext ..."

Check failure on line 275 in server_option.go

View workflow job for this annotation

GitHub Actions / build (1.18)

comment on exported function WithServerPolarisContext should be of the form "WithServerPolarisContext ..."
func WithServerPolarisContext(sdkContext api.SDKContext) DialOption {
return newFuncDialOption(func(options *dialOptions) {
options.SDKContext = sdkContext
})
}

// WithPolarisLimit 开启北极星服务端限流能力

Check failure on line 282 in server_option.go

View workflow job for this annotation

GitHub Actions / build (1.17)

comment on exported function WithPolarisRateLimit should be of the form "WithPolarisRateLimit ..."

Check failure on line 282 in server_option.go

View workflow job for this annotation

GitHub Actions / build (1.15)

comment on exported function WithPolarisRateLimit should be of the form "WithPolarisRateLimit ..."

Check failure on line 282 in server_option.go

View workflow job for this annotation

GitHub Actions / build (1.16)

comment on exported function WithPolarisRateLimit should be of the form "WithPolarisRateLimit ..."

Check failure on line 282 in server_option.go

View workflow job for this annotation

GitHub Actions / build (1.18)

comment on exported function WithPolarisRateLimit should be of the form "WithPolarisRateLimit ..."
func WithPolarisRateLimit() ServerOption {
return newFuncServerOption(func(options *serverOptions) {
Expand Down

0 comments on commit 3a615f1

Please sign in to comment.