From f9eeb8b8fc21ee25100971db73aa562b58d56a8e Mon Sep 17 00:00:00 2001 From: Joe Zou <yixian.zou@gmail.com> Date: Sat, 18 Apr 2020 23:28:08 +0800 Subject: [PATCH] modify random port implement --- config/service_config.go | 47 ++++++++-------------------------------- go.mod | 2 +- go.sum | 2 ++ 3 files changed, 12 insertions(+), 39 deletions(-) diff --git a/config/service_config.go b/config/service_config.go index f1ea65889..a05c7a038 100644 --- a/config/service_config.go +++ b/config/service_config.go @@ -20,7 +20,7 @@ package config import ( "context" "fmt" - "net" + gxnet "github.com/dubbogo/gost/net" "net/url" "strconv" "strings" @@ -106,43 +106,14 @@ func NewServiceConfig(id string, context context.Context) *ServiceConfig { } } -// Get Random Ports with no size -func getRandomPort() int { - return getRandomPorts(1)[0] -} - -// Get Random Ports with size -func getRandomPorts(size int) []int { - ports := make([]int, 0, size) - if size <= 0 { - return ports - } - - var ( - flag bool - addr *net.TCPAddr - ) - i := 0 - for i < size { - flag = false - addr = nil - go func() { - listener, err := net.Listen("tcp", ":0") - if err != nil { - return - } - - flag = true - addr = listener.Addr().(*net.TCPAddr) - }() - time.Sleep(50 * time.Millisecond) - if !flag { - continue - } - ports = append(ports, addr.Port) - i++ +// Get Random Port +func getRandomPort(ip string) string { + tcp, err := gxnet.ListenOnTCPRandomPort(ip) + if err != nil { + panic(perrors.New(fmt.Sprintf("Get tcp port error,err is {%v}", err))) } - return ports + defer tcp.Close() + return strings.Split(tcp.Addr().String(), ":")[1] } // Export ... @@ -179,7 +150,7 @@ func (c *ServiceConfig) Export() error { } port := proto.Port if len(proto.Port) == 0 { - port = strconv.Itoa(getRandomPort()) + port = getRandomPort(proto.Ip) } ivkURL := common.NewURLWithOptions( common.WithPath(c.id), diff --git a/go.mod b/go.mod index b77fd3eda..649f9b781 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/creasty/defaults v1.3.0 github.com/dubbogo/getty v1.3.4 github.com/dubbogo/go-zookeeper v1.0.0 - github.com/dubbogo/gost v1.8.0 + github.com/dubbogo/gost v1.9.0 github.com/emicklei/go-restful/v3 v3.0.0 github.com/fastly/go-utils v0.0.0-20180712184237-d95a45783239 // indirect github.com/go-errors/errors v1.0.1 // indirect diff --git a/go.sum b/go.sum index e0e1c7a9f..95deb6611 100644 --- a/go.sum +++ b/go.sum @@ -122,6 +122,8 @@ github.com/dubbogo/gost v1.7.0 h1:lWNBIE2hk1Aj2be2uXkyRTpZG0RQZj0/xbXnkIq6EHE= github.com/dubbogo/gost v1.7.0/go.mod h1:pPTjVyoJan3aPxBPNUX0ADkXjPibLo+/Ib0/fADXSG8= github.com/dubbogo/gost v1.8.0 h1:9ACbQe5OwMjqtinQcNJC5xp16kky27OsfSGw5L9A6vw= github.com/dubbogo/gost v1.8.0/go.mod h1:pPTjVyoJan3aPxBPNUX0ADkXjPibLo+/Ib0/fADXSG8= +github.com/dubbogo/gost v1.9.0 h1:UT+dWwvLyJiDotxJERO75jB3Yxgsdy10KztR5ycxRAk= +github.com/dubbogo/gost v1.9.0/go.mod h1:pPTjVyoJan3aPxBPNUX0ADkXjPibLo+/Ib0/fADXSG8= github.com/duosecurity/duo_api_golang v0.0.0-20190308151101-6c680f768e74 h1:2MIhn2R6oXQbgW5yHfS+d6YqyMfXiu2L55rFZC4UD/M= github.com/duosecurity/duo_api_golang v0.0.0-20190308151101-6c680f768e74/go.mod h1:UqXY1lYT/ERa4OEAywUqdok1T4RCRdArkhic1Opuavo= github.com/elazarl/go-bindata-assetfs v0.0.0-20160803192304-e1a2a7ec64b0 h1:ZoRgc53qJCfSLimXqJDrmBhnt5GChDsExMCK7t48o0Y= -- GitLab