From 4d714a8b381f974c7ed16a41679b2ad253d2c46d Mon Sep 17 00:00:00 2001 From: Aleksandr Razumov Date: Tue, 5 Apr 2022 14:24:04 +0300 Subject: [PATCH] feat: use net/netip --- block_fuzz_test.go | 5 ++--- go.mod | 3 --- go.sum | 15 --------------- proto/ipv4.go | 11 +++++------ proto/ipv4_test.go | 18 +++++++++--------- proto/ipv6.go | 10 +++++----- proto/ipv6_test.go | 20 ++++++++++---------- query_test.go | 8 ++++---- 8 files changed, 35 insertions(+), 55 deletions(-) diff --git a/block_fuzz_test.go b/block_fuzz_test.go index 294bc48a..591865c1 100644 --- a/block_fuzz_test.go +++ b/block_fuzz_test.go @@ -4,11 +4,10 @@ package ch import ( "bytes" + "net/netip" "strings" "testing" - "inet.af/netaddr" - "github.com/go-faster/ch/internal/gold" "github.com/go-faster/ch/proto" ) @@ -58,7 +57,7 @@ func encodeTestIPv6Block() []byte { } { var values []proto.IPv6 for _, s := range v { - ip := netaddr.MustParseIP(s) + ip := netip.MustParseAddr(s) values = append(values, proto.ToIPv6(ip)) } arr.AppendIPv6(values) diff --git a/go.mod b/go.mod index 150fe1cb..018756c4 100644 --- a/go.mod +++ b/go.mod @@ -22,7 +22,6 @@ require ( go.uber.org/multierr v1.8.0 go.uber.org/zap v1.21.0 golang.org/x/sync v0.0.0-20210220032951-036812b2e83c - inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6 ) require ( @@ -32,8 +31,6 @@ require ( github.com/go-logr/stdr v1.2.2 // indirect github.com/pascaldekloe/name v1.0.0 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect - go4.org/intern v0.0.0-20211027215823-ae77deb06f29 // indirect - go4.org/unsafe/assume-no-moving-gc v0.0.0-20211027215541-db492cf91b37 // indirect golang.org/x/mod v0.5.1 // indirect golang.org/x/sys v0.0.0-20211110154304-99a53858aa08 // indirect golang.org/x/tools v0.1.9 // indirect diff --git a/go.sum b/go.sum index de3a11d9..f1673a58 100644 --- a/go.sum +++ b/go.sum @@ -9,7 +9,6 @@ github.com/dmarkham/enumer v1.5.4 h1:XaD7WWAe1qzgR7CvXKwh4rDBulcsIG24O7RYZBI9p6M github.com/dmarkham/enumer v1.5.4/go.mod h1:VE3gc4kSCXkpXohQ1zhVI0d0xGM6AYHVyHHEInjCRp4= github.com/dustin/go-humanize v1.0.0 h1:VSnTsYCnlFHaM2/igO1h6X3HA71jcobQuxemgkq4zYo= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= -github.com/dvyukov/go-fuzz v0.0.0-20210103155950-6a8e9d1f2415/go.mod h1:11Gm+ccJnvAhCNLlf5+cS9KjtbaD5I5zaZpFMsTHWTw= github.com/go-faster/city v1.0.1 h1:4WAxSZ3V2Ws4QRDrscLEDcibJY8uf41H6AhXDrNDcGw= github.com/go-faster/city v1.0.1/go.mod h1:jKcUJId49qdW3L1qKHH/3wPeUstCVpVSXTM6vO3VcTw= github.com/go-faster/errors v0.5.0 h1:hS/zHFJ2Vb14jcupq5J9tk05XW+PFTmySOkDRByHBo4= @@ -47,7 +46,6 @@ github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UV github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= -github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= go.opentelemetry.io/otel v1.6.0/go.mod h1:bfJD2DZVw0LBxghOTlgnlI0CV3hLDu9XF/QKOUXMTQQ= go.opentelemetry.io/otel v1.6.1 h1:6r1YrcTenBvYa1x491d0GGpTVBsNECmrc/K6b+zDeis= @@ -69,32 +67,22 @@ go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8= go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak= go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8= go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw= -go4.org/intern v0.0.0-20211027215823-ae77deb06f29 h1:UXLjNohABv4S58tHmeuIZDO6e3mHpW2Dx33gaNt03LE= -go4.org/intern v0.0.0-20211027215823-ae77deb06f29/go.mod h1:cS2ma+47FKrLPdXFpr7CuxiTW3eyJbWew4qx0qtQWDA= -go4.org/unsafe/assume-no-moving-gc v0.0.0-20211027215541-db492cf91b37 h1:Tx9kY6yUkLge/pFG7IEMwDZy6CS2ajFc9TvQdPCW0uA= -go4.org/unsafe/assume-no-moving-gc v0.0.0-20211027215541-db492cf91b37/go.mod h1:FftLjUGFEDu5k8lt0ddY+HcrH/qU/0qk+H8j9/nTl3E= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI= -golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= -golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.5.1 h1:OJxoQ/rynoF0dcCdI7cLPktw/hR2cueqYfjm43oqK38= golang.org/x/mod v0.5.1/go.mod h1:5OXOZSfqPIIbmVBIIKWRFfZjPR0E5r58TLhUjH0a2Ro= golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= -golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210423185535-09eb48e85fd7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -106,7 +94,6 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= -golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= golang.org/x/tools v0.1.9 h1:j9KsMiaP1c3B0OTQGth0/k+miLGTgLsAFUCrF2vLcF8= golang.org/x/tools v0.1.9/go.mod h1:nABZi5QlRsZVlzPpHl034qft6wpY4eDcsTt5AaioBiU= @@ -123,5 +110,3 @@ gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo= gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= -inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6 h1:acCzuUSQ79tGsM/O50VRFySfMm19IoMKL+sZztZkCxw= -inet.af/netaddr v0.0.0-20211027220019-c74959edd3b6/go.mod h1:y3MGhcFMlh0KZPMuXXow8mpjxxAk3yoDNsp4cQz54i8= diff --git a/proto/ipv4.go b/proto/ipv4.go index e3b5d897..bdc83f5e 100644 --- a/proto/ipv4.go +++ b/proto/ipv4.go @@ -2,13 +2,12 @@ package proto import ( "encoding/binary" - - "inet.af/netaddr" + "net/netip" ) // IPv4 represents IPv4 address as uint32 number. // -// Not using netaddr.IP because uint32 is 5 times faster, +// Not using netip.Addr because uint32 is 5 times faster, // consumes 6 times less memory and better represents IPv4. // // Use ToIP helper for convenience. @@ -19,14 +18,14 @@ func (v IPv4) String() string { } // ToIP represents IPv4 as netaddr.IP. -func (v IPv4) ToIP() netaddr.IP { +func (v IPv4) ToIP() netip.Addr { var buf [4]byte binary.BigEndian.PutUint32(buf[:], uint32(v)) - return netaddr.IPFrom4(buf) + return netip.AddrFrom4(buf) } // ToIPv4 represents ip as IPv4. Panics if ip is not ipv4. -func ToIPv4(ip netaddr.IP) IPv4 { +func ToIPv4(ip netip.Addr) IPv4 { b := ip.As4() return IPv4(binary.BigEndian.Uint32(b[:])) } diff --git a/proto/ipv4_test.go b/proto/ipv4_test.go index 3d2517d3..1b39164c 100644 --- a/proto/ipv4_test.go +++ b/proto/ipv4_test.go @@ -2,18 +2,18 @@ package proto import ( "bytes" + "net/netip" "testing" "github.com/stretchr/testify/require" - "inet.af/netaddr" "github.com/go-faster/ch/internal/gold" ) func TestIPv4_String(t *testing.T) { - for _, v := range []netaddr.IP{ - netaddr.MustParseIP("127.0.0.1"), - netaddr.MustParseIP("1.1.1.1"), + for _, v := range []netip.Addr{ + netip.MustParseAddr("127.0.0.1"), + netip.MustParseAddr("1.1.1.1"), } { d := ToIPv4(v) require.Equal(t, v.String(), d.String()) @@ -21,10 +21,10 @@ func TestIPv4_String(t *testing.T) { } func TestColIPv4_NetAddr(t *testing.T) { - input := []netaddr.IP{ - netaddr.MustParseIP("127.0.0.1"), - netaddr.MustParseIP("127.0.0.2"), - netaddr.MustParseIP("127.0.0.3"), + input := []netip.Addr{ + netip.MustParseAddr("127.0.0.1"), + netip.MustParseAddr("127.0.0.2"), + netip.MustParseAddr("127.0.0.3"), } var d ColIPv4 for _, v := range input { @@ -41,7 +41,7 @@ func TestColIPv4_NetAddr(t *testing.T) { var dec ColIPv4 require.NoError(t, dec.DecodeColumn(r, len(input))) - var output []netaddr.IP + var output []netip.Addr for _, v := range dec { output = append(output, v.ToIP()) } diff --git a/proto/ipv6.go b/proto/ipv6.go index c6d7da7b..60898c50 100644 --- a/proto/ipv6.go +++ b/proto/ipv6.go @@ -1,7 +1,7 @@ package proto import ( - "inet.af/netaddr" + "net/netip" ) // IPv6 represents IPv6 address. @@ -13,13 +13,13 @@ func (v IPv6) String() string { return v.ToIP().String() } -// ToIP represents IPv6 as netaddr.IP. -func (v IPv6) ToIP() netaddr.IP { - return netaddr.IPv6Raw(v) +// ToIP represents IPv6 as netip.IP. +func (v IPv6) ToIP() netip.Addr { + return netip.AddrFrom16(v) } // ToIPv6 represents ip as IPv6. -func ToIPv6(ip netaddr.IP) IPv6 { return ip.As16() } +func ToIPv6(ip netip.Addr) IPv6 { return ip.As16() } func binIPv6(b []byte) IPv6 { return *(*[16]byte)(b) } func binPutIPv6(b []byte, v IPv6) { copy(b, v[:]) } diff --git a/proto/ipv6_test.go b/proto/ipv6_test.go index c63f5e65..d7fcd830 100644 --- a/proto/ipv6_test.go +++ b/proto/ipv6_test.go @@ -3,18 +3,18 @@ package proto import ( "bytes" "encoding/binary" + "net/netip" "testing" "github.com/stretchr/testify/require" - "inet.af/netaddr" "github.com/go-faster/ch/internal/gold" ) func TestIPv6_String(t *testing.T) { - for _, v := range []netaddr.IP{ - netaddr.MustParseIP("2001:db8:ac10:fe01:feed:babe:cafe:0"), - netaddr.MustParseIP("2001:4860:4860::8888"), + for _, v := range []netip.Addr{ + netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:0"), + netip.MustParseAddr("2001:4860:4860::8888"), } { d := ToIPv6(v) require.Equal(t, v.String(), d.String()) @@ -28,7 +28,7 @@ func IPv6FromInt(v int) IPv6 { } func TestToIPv6(t *testing.T) { - v := netaddr.MustParseIP("2001:db8:ac10:fe01:feed:babe:cafe:0") + v := netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:0") b := make([]byte, 16) binPutIPv6(b, v.As16()) ip := binIPv6(b) @@ -36,10 +36,10 @@ func TestToIPv6(t *testing.T) { } func TestColIPv6_NetAddr(t *testing.T) { - input := []netaddr.IP{ - netaddr.MustParseIP("2001:db8:ac10:fe01:feed:babe:cafe:0"), - netaddr.MustParseIP("2001:db8:ac10:fe01:feed:babe:cafe:1"), - netaddr.MustParseIP("2001:db8:ac10:fe01:feed:babe:cafe:2"), + input := []netip.Addr{ + netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:0"), + netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:1"), + netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:2"), } var d ColIPv6 for _, v := range input { @@ -56,7 +56,7 @@ func TestColIPv6_NetAddr(t *testing.T) { var dec ColIPv6 require.NoError(t, dec.DecodeColumn(r, len(input))) - var output []netaddr.IP + var output []netip.Addr for _, v := range dec { output = append(output, v.ToIP()) } diff --git a/query_test.go b/query_test.go index 3c3a8a2d..8dfe7ca8 100644 --- a/query_test.go +++ b/query_test.go @@ -6,6 +6,7 @@ import ( "fmt" "io" "math/rand" + "net/netip" "testing" "time" @@ -13,7 +14,6 @@ import ( "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "go.uber.org/zap" - "inet.af/netaddr" "github.com/go-faster/ch/proto" ) @@ -345,7 +345,7 @@ func TestClient_Query(t *testing.T) { require.NoError(t, Conn(t).Do(ctx, selectArr)) require.Equal(t, 1, data.Rows()) t.Logf("%v %s", data[0], data[0].ToIP()) - require.Equal(t, netaddr.MustParseIP("127.1.1.5"), data[0].ToIP()) + require.Equal(t, netip.MustParseAddr("127.1.1.5"), data[0].ToIP()) }) t.Run("SelectIPv6", func(t *testing.T) { t.Parallel() @@ -362,7 +362,7 @@ func TestClient_Query(t *testing.T) { require.NoError(t, Conn(t).Do(ctx, selectArr)) require.Equal(t, 1, data.Rows()) t.Logf("%v %s", data[0], data[0].ToIP()) - expected := netaddr.MustParseIP("2001:db8:ac10:fe01:feed:babe:cafe:f00d") + expected := netip.MustParseAddr("2001:db8:ac10:fe01:feed:babe:cafe:f00d") require.Equal(t, expected, data[0].ToIP()) }) t.Run("SelectDateTime", func(t *testing.T) { @@ -414,7 +414,7 @@ func TestClient_Query(t *testing.T) { }, })) require.Equal(t, 1, data.Rows()) - require.Equal(t, netaddr.MustParseIP("10.10.0.1"), data[0].ToIP()) + require.Equal(t, netip.MustParseAddr("10.10.0.1"), data[0].ToIP()) }) t.Run("InsertDateTime", func(t *testing.T) { t.Parallel()