Skip to content

Commit

Permalink
Merge pull request #808 from cvictory/refactor_local_ip
Browse files Browse the repository at this point in the history
Imp: get local ip once and reused it
  • Loading branch information
watermelo authored Oct 28, 2020
2 parents 268b392 + c1c46b3 commit c8689e6
Show file tree
Hide file tree
Showing 15 changed files with 88 additions and 39 deletions.
5 changes: 2 additions & 3 deletions cluster/cluster_impl/base_cluster_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
)

import (
gxnet "github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
"go.uber.org/atomic"
)
Expand Down Expand Up @@ -72,7 +71,7 @@ func (invoker *baseClusterInvoker) IsAvailable() bool {
//check invokers availables
func (invoker *baseClusterInvoker) checkInvokers(invokers []protocol.Invoker, invocation protocol.Invocation) error {
if len(invokers) == 0 {
ip, _ := gxnet.GetLocalIP()
ip := common.GetLocalIp()
return perrors.Errorf("Failed to invoke the method %v. No provider available for the service %v from "+
"registry %v on the consumer %v using the dubbo version %v .Please check if the providers have been started and registered.",
invocation.MethodName(), invoker.directory.GetUrl().SubURL.Key(), invoker.directory.GetUrl().String(), ip, constant.Version)
Expand All @@ -84,7 +83,7 @@ func (invoker *baseClusterInvoker) checkInvokers(invokers []protocol.Invoker, in
//check cluster invoker is destroyed or not
func (invoker *baseClusterInvoker) checkWhetherDestroyed() error {
if invoker.destroyed.Load() {
ip, _ := gxnet.GetLocalIP()
ip := common.GetLocalIp()
return perrors.Errorf("Rpc cluster invoker for %v on consumer %v use dubbo version %v is now destroyed! can not invoke any more. ",
invoker.directory.GetUrl().Service(), ip, constant.Version)
}
Expand Down
4 changes: 2 additions & 2 deletions cluster/cluster_impl/failover_cluster_invoker.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ import (
)

import (
gxnet "github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
)

import (
"github.com/apache/dubbo-go/cluster"
"github.com/apache/dubbo-go/common"
"github.com/apache/dubbo-go/common/constant"
"github.com/apache/dubbo-go/common/logger"
"github.com/apache/dubbo-go/protocol"
Expand Down Expand Up @@ -89,7 +89,7 @@ func (invoker *failoverClusterInvoker) Invoke(ctx context.Context, invocation pr
return result
}

ip, _ := gxnet.GetLocalIP()
ip := common.GetLocalIp()
invokerSvc := invoker.GetUrl().Service()
invokerUrl := invoker.directory.GetUrl()
return &protocol.RPCResult{
Expand Down
5 changes: 2 additions & 3 deletions cluster/directory/base_directory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
)

import (
gxnet "github.com/dubbogo/gost/net"
"github.com/stretchr/testify/assert"
)

Expand Down Expand Up @@ -55,7 +54,7 @@ func TestBuildRouterChain(t *testing.T) {

assert.NotNil(t, directory)

localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
rule := base64.URLEncoding.EncodeToString([]byte("true => " + " host = " + localIP))
routeURL := getRouteURL(rule, anyURL)
routeURL.AddParam(constant.INTERFACE_KEY, "mock-app")
Expand All @@ -79,7 +78,7 @@ func TestIsProperRouter(t *testing.T) {
regURL := url
regURL.AddParam(constant.APPLICATION_KEY, "mock-app")
d := NewBaseDirectory(&regURL)
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
rule := base64.URLEncoding.EncodeToString([]byte("true => " + " host = " + localIP))
routeURL := getRouteURL(rule, anyURL)
routeURL.AddParam(constant.APPLICATION_KEY, "mock-app")
Expand Down
21 changes: 10 additions & 11 deletions cluster/router/condition/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
)

import (
"github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -164,7 +163,7 @@ func TestRoute_matchWhen(t *testing.T) {
}

func TestRoute_matchFilter(t *testing.T) {
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
t.Logf("The local ip is %s", localIP)
url1, _ := common.NewURL("dubbo://10.20.3.3:20880/com.foo.BarService?default.serialization=fastjson")
url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP))
Expand Down Expand Up @@ -223,7 +222,7 @@ func TestRoute_methodRoute(t *testing.T) {

func TestRoute_ReturnFalse(t *testing.T) {
url, _ := common.NewURL("")
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
invokers := []protocol.Invoker{NewMockInvoker(url, 1), NewMockInvoker(url, 2), NewMockInvoker(url, 3)}
inv := &invocation.RPCInvocation{}
rule := base64.URLEncoding.EncodeToString([]byte("host = " + localIP + " => false"))
Expand All @@ -234,7 +233,7 @@ func TestRoute_ReturnFalse(t *testing.T) {
}

func TestRoute_ReturnEmpty(t *testing.T) {
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
url, _ := common.NewURL("")
invokers := []protocol.Invoker{NewMockInvoker(url, 1), NewMockInvoker(url, 2), NewMockInvoker(url, 3)}
inv := &invocation.RPCInvocation{}
Expand All @@ -246,7 +245,7 @@ func TestRoute_ReturnEmpty(t *testing.T) {
}

func TestRoute_ReturnAll(t *testing.T) {
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
urlString := "dubbo://" + localIP + "/com.foo.BarService"
dubboURL, _ := common.NewURL(urlString)
mockInvoker1 := NewMockInvoker(dubboURL, 1)
Expand All @@ -262,7 +261,7 @@ func TestRoute_ReturnAll(t *testing.T) {
}

func TestRoute_HostFilter(t *testing.T) {
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
url1, _ := common.NewURL(factory333URL)
url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP))
url3, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP))
Expand All @@ -281,7 +280,7 @@ func TestRoute_HostFilter(t *testing.T) {
}

func TestRoute_Empty_HostFilter(t *testing.T) {
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
url1, _ := common.NewURL(factory333URL)
url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP))
url3, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP))
Expand All @@ -300,7 +299,7 @@ func TestRoute_Empty_HostFilter(t *testing.T) {
}

func TestRoute_False_HostFilter(t *testing.T) {
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
url1, _ := common.NewURL(factory333URL)
url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP))
url3, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP))
Expand All @@ -319,7 +318,7 @@ func TestRoute_False_HostFilter(t *testing.T) {
}

func TestRoute_Placeholder(t *testing.T) {
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
url1, _ := common.NewURL(factory333URL)
url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP))
url3, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP))
Expand All @@ -338,7 +337,7 @@ func TestRoute_Placeholder(t *testing.T) {
}

func TestRoute_NoForce(t *testing.T) {
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
url1, _ := common.NewURL(factory333URL)
url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP))
url3, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP))
Expand All @@ -355,7 +354,7 @@ func TestRoute_NoForce(t *testing.T) {
}

func TestRoute_Force(t *testing.T) {
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
url1, _ := common.NewURL(factory333URL)
url2, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP))
url3, _ := common.NewURL(fmt.Sprintf(factoryDubboFormat, localIP))
Expand Down
3 changes: 1 addition & 2 deletions cluster/router/condition/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
import (
"github.com/RoaringBitmap/roaring"
"github.com/dubbogo/gost/container/set"
"github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
)

Expand Down Expand Up @@ -184,7 +183,7 @@ func (c *ConditionRouter) Route(invokers *roaring.Bitmap, cache router.Cache, ur
return result
} else if c.Force {
rule, _ := url.GetParamAndDecoded(constant.RULE_KEY)
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
logger.Warnf("The route result is empty and force execute. consumer: %s, service: %s, router: %s", localIP, url.Service(), rule)
return result
}
Expand Down
30 changes: 30 additions & 0 deletions common/host_util.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 common

import gxnet "github.com/dubbogo/gost/net"

var localIp string

func GetLocalIp() string {
if len(localIp) != 0 {
return localIp
}
localIp, _ = gxnet.GetLocalIP()
return localIp
}
30 changes: 30 additions & 0 deletions common/host_util_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* 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 common

import (
"testing"
)

import (
"github.com/stretchr/testify/assert"
)

func TestGetLocalIp(t *testing.T) {
assert.NotNil(t, GetLocalIp())
}
3 changes: 1 addition & 2 deletions config/config_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ import (
)

import (
gxnet "github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
)

Expand Down Expand Up @@ -250,7 +249,7 @@ func createInstance(url common.URL) (registry.ServiceInstance, error) {

host := url.Ip
if len(host) == 0 {
host, err = gxnet.GetLocalIP()
host = common.GetLocalIp()
if err != nil {
return nil, perrors.WithMessage(err, "could not get the local Ip")
}
Expand Down
6 changes: 3 additions & 3 deletions config/service_config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@
package config

import (
"github.com/apache/dubbo-go/common"
"testing"
)

import (
gxnet "github.com/dubbogo/gost/net"
"github.com/stretchr/testify/assert"
"go.uber.org/atomic"
)
Expand Down Expand Up @@ -184,7 +184,7 @@ func TestExport(t *testing.T) {
func TestGetRandomPort(t *testing.T) {
protocolConfigs := make([]*ProtocolConfig, 0, 3)

ip, err := gxnet.GetLocalIP()
ip := common.GetLocalIp()
protocolConfigs = append(protocolConfigs, &ProtocolConfig{
Ip: ip,
})
Expand All @@ -194,7 +194,7 @@ func TestGetRandomPort(t *testing.T) {
protocolConfigs = append(protocolConfigs, &ProtocolConfig{
Ip: ip,
})
assert.NoError(t, err)
//assert.NoError(t, err)
ports := getRandomPort(protocolConfigs)

assert.Equal(t, ports.Len(), len(protocolConfigs))
Expand Down
5 changes: 2 additions & 3 deletions config_center/configurator/override.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

import (
gxset "github.com/dubbogo/gost/container/set"
gxnet "github.com/dubbogo/gost/net"
)

import (
Expand Down Expand Up @@ -61,7 +60,7 @@ func (c *overrideConfigurator) Configure(url *common.URL) {
currentSide := url.GetParam(constant.SIDE_KEY, "")
configuratorSide := c.configuratorUrl.GetParam(constant.SIDE_KEY, "")
if currentSide == configuratorSide && common.DubboRole[common.CONSUMER] == currentSide && c.configuratorUrl.Port == "0" {
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
c.configureIfMatch(localIP, url)
} else if currentSide == configuratorSide && common.DubboRole[common.PROVIDER] == currentSide && c.configuratorUrl.Port == url.Port {
c.configureIfMatch(url.Ip, url)
Expand Down Expand Up @@ -127,7 +126,7 @@ func (c *overrideConfigurator) configureDeprecated(url *common.URL) {
// 1.If it is a consumer ip address, the intention is to control a specific consumer instance, it must takes effect at the consumer side, any provider received this override url should ignore;
// 2.If the ip is 0.0.0.0, this override url can be used on consumer, and also can be used on provider
if url.GetParam(constant.SIDE_KEY, "") == common.DubboRole[common.CONSUMER] {
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()
c.configureIfMatch(localIP, url)
} else {
c.configureIfMatch(constant.ANYHOST_VALUE, url)
Expand Down
3 changes: 1 addition & 2 deletions registry/base_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
)

import (
gxnet "github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
)

Expand All @@ -53,7 +52,7 @@ var (

func init() {
processID = fmt.Sprintf("%d", os.Getpid())
localIP, _ = gxnet.GetLocalIP()
localIP = common.GetLocalIp()
}

type createPathFunc func(dubboPath string) error
Expand Down
3 changes: 1 addition & 2 deletions registry/consul/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
)

import (
gxnet "github.com/dubbogo/gost/net"
consul "github.com/hashicorp/consul/api"
perrors "github.com/pkg/errors"
)
Expand All @@ -47,7 +46,7 @@ func buildService(url common.URL) (*consul.AgentServiceRegistration, error) {

// address
if url.Ip == "" {
url.Ip, _ = gxnet.GetLocalIP()
url.Ip = common.GetLocalIp()
}

// port
Expand Down
3 changes: 1 addition & 2 deletions registry/directory/directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import (
)

import (
gxnet "github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
"go.uber.org/atomic"
)
Expand Down Expand Up @@ -365,7 +364,7 @@ func (dir *RegistryDirectory) overrideUrl(targetUrl *common.URL) {

func (dir *RegistryDirectory) getConsumerUrl(c *common.URL) *common.URL {
processID := fmt.Sprintf("%d", os.Getpid())
localIP, _ := gxnet.GetLocalIP()
localIP := common.GetLocalIp()

params := url.Values{}
c.RangeParams(func(key, value string) bool {
Expand Down
3 changes: 1 addition & 2 deletions registry/kubernetes/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ import (

import (
"github.com/apache/dubbo-getty"
"github.com/dubbogo/gost/net"
perrors "github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
)
Expand All @@ -54,7 +53,7 @@ const (

func init() {
processID = fmt.Sprintf("%d", os.Getpid())
localIP, _ = gxnet.GetLocalIP()
localIP = common.GetLocalIp()
extension.SetRegistry(Name, newKubernetesRegistry)
}

Expand Down
Loading

0 comments on commit c8689e6

Please sign in to comment.