Skip to content

Commit

Permalink
Ftr: listen on random local port (#29)
Browse files Browse the repository at this point in the history
* Add: go safely

* Mod: go fmt

* Add: remark and ignoreRecover param

* Add: atomic load var

* directly pass handle func

* Fix: load var in atomic

* Rem: load atomic var by atomic loading

* Add: period param

* Mod: add readme

* Fix: inc value by atomic

* Add: ListenOnTcpRandomPort/ListenOnUdpRandomPort

* Fix: conflict

* Update: readme

Co-authored-by: 望哥 <gelnyang@163.com>
Co-authored-by: yuyu <yuyu.zx@alipay.com>
  • Loading branch information
3 people authored Apr 18, 2020
1 parent 705a827 commit 8b727f6
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 1 deletion.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ A go sdk for [Apache Dubbo-go](github.com/apache/dubbo-go).
> bytes.Buffer pool
* SlicePool
> slice pool
> slice pool
## container

* queue
Expand All @@ -26,6 +27,21 @@ A go sdk for [Apache Dubbo-go](github.com/apache/dubbo-go).
## math

* Decimal

## net

* GetLocalIP() (string, error)
* IsSameAddr(addr1, addr2 net.Addr) bool
* ListenOnTCPRandomPort(ip string) (*net.TCPListener, error)
* ListenOnUDPRandomPort(ip string) (*net.UDPConn, error)

## runtime

* GoSafely
> Using `go` in a safe way.
* GoUnterminated
> Run a goroutine in a safe way whose task is long live as the whole process life time.
## runtime

Expand Down
30 changes: 30 additions & 0 deletions net/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func init() {
}
}

// GetLocalIP get local ip
func GetLocalIP() (string, error) {
faces, err := net.Interfaces()
if err != nil {
Expand Down Expand Up @@ -145,3 +146,32 @@ func IsSameAddr(addr1, addr2 net.Addr) bool {
addr2s = strings.TrimPrefix(addr2s, ipv4prefix)
return addr1s == addr2s
}

// ListenOnTCPRandomPort a tcp server listening on a random port by tcp protocol
func ListenOnTCPRandomPort(ip string) (*net.TCPListener, error) {
localAddr := net.TCPAddr{
IP: net.IPv4zero,
Port: 0,
}
if len(ip) > 0 {
localAddr.IP = net.ParseIP(ip)
}

// on some containers, u can not bind an random port by the following clause.
// listener, err := net.Listen("tcp", ":0")

return net.ListenTCP("tcp4", &localAddr)
}

// ListenOnUDPRandomPort a udp endpoint listening on a random port
func ListenOnUDPRandomPort(ip string) (*net.UDPConn, error) {
localAddr := net.UDPAddr{
IP: net.IPv4zero,
Port: 0,
}
if len(ip) > 0 {
localAddr.IP = net.ParseIP(ip)
}

return net.ListenUDP("udp4", &localAddr)
}
33 changes: 33 additions & 0 deletions net/net_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package gxnet

import (
"net"
"strings"
"testing"
)

Expand Down Expand Up @@ -54,3 +55,35 @@ func TestIsSameAddr(t *testing.T) {
addr2.Zone = ""
assert.True(t, IsSameAddr(&addr1, &addr1))
}

func TestListenOnTCPRandomPort(t *testing.T) {
l, err := ListenOnTCPRandomPort("")
assert.Nil(t, err)
t.Logf("a tcp server listen on a random addr:%s", l.Addr())
l.Close()

localIP, err := GetLocalIP()
if err == nil {
l, err = ListenOnTCPRandomPort(localIP)
assert.Nil(t, err)
assert.True(t, strings.Contains(l.Addr().String(), localIP))
t.Logf("a tcp server listen on a random addr:%s", l.Addr())
l.Close()
}
}

func TestListenOnUDPRandomPort(t *testing.T) {
l, err := ListenOnUDPRandomPort("")
assert.Nil(t, err)
t.Logf("a udp peer listen on a random addr:%s", l.LocalAddr())
l.Close()

localIP, err := GetLocalIP()
if err == nil {
l, err = ListenOnUDPRandomPort(localIP)
assert.Nil(t, err)
assert.True(t, strings.Contains(l.LocalAddr().String(), localIP))
t.Logf("a udp server listen on a random addr:%s", l.LocalAddr())
l.Close()
}
}

0 comments on commit 8b727f6

Please sign in to comment.