From b3e25c0d95f192f259aeeb5b02ad13180c415559 Mon Sep 17 00:00:00 2001 From: liaochuntao Date: Wed, 15 Nov 2023 11:11:44 +0800 Subject: [PATCH] fix:support use custom register instance port (#61) --- balancer.go | 4 +-- client_option.go | 1 + doc.go | 19 ------------- go.sum | 3 -- logger.go | 71 ++++++++++++++++++++++++++++++++++++++++++++++++ server.go | 10 ++++--- server_option.go | 3 +- 7 files changed, 81 insertions(+), 30 deletions(-) delete mode 100644 doc.go create mode 100644 logger.go diff --git a/balancer.go b/balancer.go index a3859ba..cb7cd5e 100644 --- a/balancer.go +++ b/balancer.go @@ -74,7 +74,7 @@ type ( func (bb *balancerBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOptions) balancer.Balancer { grpclog.Infof("[Polaris][Balancer] start to build polaris balancer") target := opts.Target - host, port, err := parseHost(target.URL.Host) + host, _, err := parseHost(target.URL.Host) if err != nil { grpclog.Errorln("[Polaris][Balancer] failed to create balancer: " + err.Error()) return nil @@ -83,7 +83,6 @@ func (bb *balancerBuilder) Build(cc balancer.ClientConn, opts balancer.BuildOpti cc: cc, target: opts.Target, host: host, - port: port, subConns: make(map[string]balancer.SubConn), scStates: make(map[balancer.SubConn]connectivity.State), csEvltr: &balancer.ConnectivityStateEvaluator{}, @@ -111,7 +110,6 @@ type polarisNamingBalancer struct { cc balancer.ClientConn target resolver.Target host string - port int rwMutex sync.RWMutex csEvltr *balancer.ConnectivityStateEvaluator diff --git a/client_option.go b/client_option.go index 4907a3d..7202bb6 100644 --- a/client_option.go +++ b/client_option.go @@ -114,6 +114,7 @@ func WithPolarisConfig(polarisCfg config.Configuration) DialOption { }) } +// WithPolarisContext set polaris SDKContext func WithPolarisContext(sdkContext api.SDKContext) DialOption { return newFuncDialOption(func(options *dialOptions) { options.SDKContext = sdkContext diff --git a/doc.go b/doc.go deleted file mode 100644 index 73e361c..0000000 --- a/doc.go +++ /dev/null @@ -1,19 +0,0 @@ -/** - * Tencent is pleased to support the open source community by making Polaris available. - * - * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. - * - * Licensed under the BSD 3-Clause License (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * https://opensource.org/licenses/BSD-3-Clause - * - * Unless required by applicable law or agreed to in writing, software distributed - * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR - * CONDITIONS OF ANY KIND, either express or implied. See the License for the - * specific language governing permissions and limitations under the License. - */ - -// Package grpcpolaris grpc-go integrates with polaris-go -package grpcpolaris diff --git a/go.sum b/go.sum index 49f808d..8a0a80b 100644 --- a/go.sum +++ b/go.sum @@ -1033,11 +1033,8 @@ github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZ github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= -github.com/polarismesh/polaris-go v1.5.1 h1:EiAOYITCoa8YCigZhWefClEbqVj8MF7mTrLD3dlKIyM= -github.com/polarismesh/polaris-go v1.5.1/go.mod h1:45nx/yegSRFJ5mGITumX5FMLaY7ltrT68HVkTs9pPlk= github.com/polarismesh/polaris-go v1.5.2 h1:r9gSQrkq49nBTbKESrW/cMPlUe64/2Z91h/8Nki8IBs= github.com/polarismesh/polaris-go v1.5.2/go.mod h1:tF8ed2GS6tYh3cJYcHFq8FKWMZTveGfKwhX1/YriZVY= -github.com/polarismesh/specification v1.3.2-alpha.2/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= github.com/polarismesh/specification v1.3.2 h1:NG8guSTi7brxEMTG39VVmRSZeS7XvacKnrpoOAVvOtU= github.com/polarismesh/specification v1.3.2/go.mod h1:rDvMMtl5qebPmqiBLNa5Ps0XtwkP31ZLirbH4kXA0YU= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= diff --git a/logger.go b/logger.go new file mode 100644 index 0000000..234ebb4 --- /dev/null +++ b/logger.go @@ -0,0 +1,71 @@ +/** + * Tencent is pleased to support the open source community by making Polaris available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package grpcpolaris + +import ( + "io" + "sync/atomic" +) + +type LogLevel int + +const ( + _ LogLevel = iota + LogDebug + LogInfo + LogWarn + LogError +) + +type Logger interface { + SetWriter(io.WriteCloser) + SetLevel() + Debug(format string, args interface{}) + Info(format string, args interface{}) + Warn(format string, args interface{}) + Error(format string, args interface{}) +} + +type defaultLogger struct { + writerRef atomic.Value + levelRef atomic.Value +} + +func (l *defaultLogger) SetWriter(writer io.WriteCloser) { + l.writerRef.Store(writer) +} + +func (l *defaultLogger) SetLevel(level LogLevel) { + l.levelRef.Store(level) +} + +func (l *defaultLogger) Debug(format string, args interface{}) { + +} + +func (l *defaultLogger) Info(format string, args interface{}) { + +} + +func (l *defaultLogger) Warn(format string, args interface{}) { + +} + +func (l *defaultLogger) Error(format string, args interface{}) { + +} diff --git a/server.go b/server.go index ccccd60..8f014e4 100644 --- a/server.go +++ b/server.go @@ -159,11 +159,13 @@ func Register(gSrv *grpc.Server, lis net.Listener, opts ...ServerOption) (*Serve } srv.serverOptions.host = host } - port, err := parsePort(lis.Addr().String()) - if nil != err { - return nil, fmt.Errorf("error occur while parsing port from listener: %w", err) + 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 } - srv.serverOptions.port = port if *srv.serverOptions.delayRegisterEnable { delayStrategy := srv.serverOptions.delayRegisterStrategy diff --git a/server_option.go b/server_option.go index 4c9ac27..900c0e7 100644 --- a/server_option.go +++ b/server_option.go @@ -252,8 +252,9 @@ func WithTTL(ttl int) ServerOption { } // WithPort set the port to register instance -// Deprecated: will remove in 1.4 +// 该方法非必需调用, 建议只在注册端口和程序实际监听端口需要不一致时才调用 func WithPort(port int) ServerOption { return newFuncServerOption(func(options *serverOptions) { + options.port = port }) }