Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix:support use custom register instance port #61

Merged
merged 3 commits into from
Nov 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions balancer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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{},
Expand Down Expand Up @@ -111,7 +110,6 @@ type polarisNamingBalancer struct {
cc balancer.ClientConn
target resolver.Target
host string
port int
rwMutex sync.RWMutex

csEvltr *balancer.ConnectivityStateEvaluator
Expand Down
1 change: 1 addition & 0 deletions client_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 0 additions & 19 deletions doc.go

This file was deleted.

3 changes: 0 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
71 changes: 71 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
@@ -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{}) {

}
10 changes: 6 additions & 4 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion server_option.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
}