Unverified Commit 8b727f60 authored by Xin.Zh's avatar Xin.Zh Committed by GitHub

Ftr: listen on random local port (#29)

* 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: 's avatar望哥 <gelnyang@163.com>
Co-authored-by: 's avataryuyu <yuyu.zx@alipay.com>
parent 705a827b
......@@ -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
......@@ -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
......
......@@ -38,6 +38,7 @@ func init() {
}
}
// GetLocalIP get local ip
func GetLocalIP() (string, error) {
faces, err := net.Interfaces()
if err != nil {
......@@ -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)
}
......@@ -19,6 +19,7 @@ package gxnet
import (
"net"
"strings"
"testing"
)
......@@ -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()
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment