Skip to content

Commit

Permalink
xlo ref lo, fixed pkg ip (#19)
Browse files Browse the repository at this point in the history
  • Loading branch information
xwi88 authored Oct 18, 2022
1 parent 701b1c5 commit 58d7b28
Show file tree
Hide file tree
Showing 8 changed files with 250 additions and 12 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ GOFILES := $(shell find . -name "*.go" -type f -not -path "./vendor/*")
TESTFOLDER := $(shell $(GO) list ./...)
TESTTAGS ?=
GCFLAGS ?= "-gcflags=all=-l"
GCFLAGS_ESCAPE ?= "-gcflags=-m -l"
ESCAPE_PATH ?= ""

.PHONY: check
check: fmt-check misspell-check golangci cover
Expand Down Expand Up @@ -112,3 +114,8 @@ golangci:
.PHONY: mod
mod:
@go mod tidy

.PHONY: escape
escape:
@$(GO) test ${GCFLAGS_ESCAPE} -cover ./${ESCAPE_PATH}...
@echo "escape done"
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,22 @@
- [x] [number](number) round, bytes convert.
- [x] [random](random) rand, random.
- [x] [uuid](uuid) requestID, go.uuid, ksuid, xid.
- [x] [xlo](xlo) some utils ref *lo*, more pls use [lo](https://github.com/samber/lo) directly.

## Install

`go get -u github.com/v8fg/kit4go`

## Notes

>If test failed, maybe effected by the inline, you can try: `go test -v -gcflags=all=-l xxx_test.go`.
> If test failed, maybe effected by the inline, you can try: `go test -v -gcflags=all=-l xxx_test.go`.
## CMD

- **release check**: `make`
- **coverage**: `make cover`
- **format check**: `make fmt-check`
- **format fixed**: `make fmt`
- **misspell check**: `make misspell-check`
- **golang lint**: `make golangci`
- **escape analysis**: `make escape` or `ESCAPE_PATH=ip make escape`
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,16 @@ require (
)

require (
github.com/bits-and-blooms/bitset v1.3.3 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06 // indirect
github.com/gopherjs/gopherjs v1.17.2 // indirect
github.com/jtolds/gls v4.20.0+incompatible // indirect
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/samber/lo v1.33.0 // indirect
github.com/smartystreets/assertions v1.13.0 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 // indirect
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 // indirect
)
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
github.com/agiledragon/gomonkey v2.0.2+incompatible h1:eXKi9/piiC3cjJD1658mEE2o3NjkJ5vDLgYjCQu0Xlw=
github.com/agiledragon/gomonkey v2.0.2+incompatible/go.mod h1:2NGfXu1a80LLr2cmWXGBDaHEjb1idR6+FVlX5T3D9hw=
github.com/bits-and-blooms/bitset v1.3.3 h1:R1XWiopGiXf66xygsiLpzLo67xEYvMkHw3w+rCOSAwg=
github.com/bits-and-blooms/bitset v1.3.3/go.mod h1:gIdJ4wp64HaoK2YrL1Q5/N7Y16edYb8uY+O0FJTyyDA=
github.com/bytedance/sonic v1.5.0 h1:XWdTi8bwPgxIML+eNV1IwNuTROK6EUrQ65ey8yd6fRQ=
github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM=
github.com/chenzhuoyu/base64x v0.0.0-20211019084208-fb5309c8db06 h1:1sDoSuDPWzhkdzNVxCxtIaKiAe96ESVPv8coGwc1gZ4=
Expand Down Expand Up @@ -27,6 +29,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rs/xid v1.4.0 h1:qd7wPTDkN6KQx2VmMBLrpHkiyQwgFXRnkOLacUiaSNY=
github.com/rs/xid v1.4.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg=
github.com/samber/lo v1.33.0 h1:2aKucr+rQV6gHpY3bpeZu69uYoQOzVhGT3J22Op6Cjk=
github.com/samber/lo v1.33.0/go.mod h1:HLeWcJRRyLKp3+/XBJvOrerCQn9mhdKMHyd7IRlgeQ8=
github.com/satori/go.uuid v1.2.0 h1:0uYX9dsZ2yD7q2RtLRtPSdGDWzjeM3TbMJP9utgA0ww=
github.com/satori/go.uuid v1.2.0/go.mod h1:dA0hQrYB0VpLJoorglMZABFdXlWrHn1NEOzdhQKdks0=
github.com/segmentio/ksuid v1.0.4 h1:sBo2BdShXjmcugAMwjugoGUdUV0pcxY5mW4xKRn3v4c=
Expand All @@ -45,6 +49,8 @@ github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2
golang.org/x/arch v0.0.0-20210923205945-b76863e36670 h1:18EFjUmQOcUvxNYSkA6jO9VAiXCnxFY6NyDX0bHDmkU=
golang.org/x/arch v0.0.0-20210923205945-b76863e36670/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM=
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
31 changes: 20 additions & 11 deletions ip/net_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,16 @@ const (
// APIListForPublicIP api list for get the public ip.
var APIListForPublicIP = []string{
"https://ipinfo.io/ip",
"https://ipinfo.io", // application/json
"https://ifconfig.me/ip",
"https://ifconfig.me",
"https://ipinfo.io", // application/json
"https://ifconfig.co",
"https://api.ipify.org",
"https://api.ipify.org?format=json",
"https://api64.ipify.org?format=json",
"https://icanhazip.com",
"https://ident.me",
"https://ipecho.net/plain",
"https://ifconfig.co",
// "https://icanhazip.com", // maybe ipv6 formatted, ignore now
}

func (cacheLocalIP *localIP) LocalIP() (localIP string) {
Expand Down Expand Up @@ -290,15 +290,19 @@ func getPublicIPMultiChannel(timeout time.Duration, urls []string) (ip string) {
}(url)
}

select {
case _ip := <-ips:
if len(_ip) != 0 {
ip = _ip
break
Loop:
for {
select {
case _ip := <-ips:
if len(strings.TrimSpace(_ip)) != 0 {
ip = strings.TrimSpace(_ip)
break Loop
}
case <-tm.C:
break Loop
}
case <-tm.C:
break
}

return
}

Expand All @@ -313,7 +317,12 @@ func PublicIP(timeout time.Duration, apiListForPublic ...string) (url string) {
timeout = time.Second * 5
}

return getPublicIPMultiChannel(timeout, apiListForPublic)
// most retries
for i := 0; i < 3 && len(url) == 0; i++ {
time.Sleep(time.Millisecond * 100)
url = getPublicIPMultiChannel(timeout, apiListForPublic)
}
return
}

// ClientIP implements one best effort algorithm to return the real client IP.
Expand Down
87 changes: 87 additions & 0 deletions xlo/benchmark_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package xlo_test

import (
"strconv"
"testing"

"github.com/samber/lo"

"github.com/v8fg/kit4go/xlo"
)

func BenchmarkFindUniquesInt(b *testing.B) {
testSet := [][]int{
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
{1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2},
{1, 2, 2, 2, 2, 2, 1, 1, 1, 2, 1, 2},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < len(testSet); j++ {
xlo.Uniq(testSet[j])
}
}
}

func BenchmarkFindUniquesStr(b *testing.B) {
testSet := [][]string{
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
{"1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2"},
{"1", "2", "2", "2", "2", "2", "1", "1", "1", "2", "1", "2"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < len(testSet); j++ {
xlo.Uniq(testSet[j])
}
}
}

func BenchmarkLoFindUniquesInt(b *testing.B) {
testSet := [][]int{
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12},
{1, 2, 1, 2, 1, 2, 1, 2, 1, 2, 1, 2},
{1, 2, 2, 2, 2, 2, 1, 1, 1, 2, 1, 2},
}

b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < len(testSet); j++ {
xlo.LoUniq(testSet[j])
}
}
}

func BenchmarkLoFindUniquesStr(b *testing.B) {
testSet := [][]string{
{"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"},
{"1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2"},
{"1", "2", "2", "2", "2", "2", "1", "1", "1", "2", "1", "2"},
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j < len(testSet); j++ {
xlo.LoUniq(testSet[j])
}
}
}

func BenchmarkLoMap(b *testing.B) {
original := lo.Range(1_024)
b.ResetTimer()
for i := 0; i < b.N; i++ {
xlo.LoMap(original, func(item int, index int) string {
return strconv.FormatInt(int64(item), 10)
})
}
}

func BenchmarkLopMap(b *testing.B) {
original := lo.Range(1_024)
b.ResetTimer()
for i := 0; i < b.N; i++ {
xlo.LopMap(original, func(item int, index int) string {
return strconv.FormatInt(int64(item), 10)
})
}
}
52 changes: 52 additions & 0 deletions xlo/xlo.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package xlo

import (
"github.com/samber/lo"
lop "github.com/samber/lo/parallel"
)

// Maximum average load of a bucket that triggers growth is 6.5.
// Represent as loadFactorNum/loadFactorDen, to allow integer math.
// loadFactorDen = 2
// defined in runtime./map.go
const loadFactorNum = 13

// Uniq returns a duplicate-free version of an array, in which only the first occurrence of each element is kept.
// The order of result values is determined by the order they occur in the array.
//
// Some optimizations for small capacity temp map, if capacity less than loadFactorNum.
func Uniq[T comparable](collection []T) []T {
size := len(collection)
result := make([]T, 0, size)
var temp map[T]struct{}
if size < loadFactorNum {
temp = map[T]struct{}{}
} else {
temp = make(map[T]struct{}, size)
}

for _, item := range collection {
if _, ok := temp[item]; !ok {
temp[item] = struct{}{}
result = append(result, item)
}
}
return result
}

// LoUniq returns a duplicate-free version of an array, in which only the first occurrence of each element is kept.
// The order of result values is determined by the order they occur in the array.
func LoUniq[T comparable](collection []T) []T {
return lo.Uniq(collection)
}

// LoMap manipulates a slice and transforms it to a slice of another type.
func LoMap[T any, R any](collection []T, iteratee func(item T, index int) R) []R {
return lo.Map(collection, iteratee)
}

// LopMap manipulates a slice and transforms it to a slice of another type.
// `iteratee` is call in parallel. Result keep the same order.
func LopMap[T any, R any](collection []T, iteratee func(item T, index int) R) []R {
return lop.Map(collection, iteratee)
}
63 changes: 63 additions & 0 deletions xlo/xlo_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package xlo_test

import (
"strconv"
"testing"

"github.com/smartystreets/goconvey/convey"

"github.com/v8fg/kit4go/xlo"
)

func TestFindUniques(t *testing.T) {
convey.SetDefaultFailureMode(convey.FailureContinues)
convey.Convey("TestFindUniques", t, func() {
convey.Convey("int case", func() {
convey.So(xlo.Uniq([]int{1, 2, 3}), convey.ShouldResemble, []int{1, 2, 3})
convey.So(xlo.Uniq([]int{1, 2, 2, 3, 1, 2, 1, 2, 2, 3, 1, 2}), convey.ShouldResemble, []int{1, 2, 3})
convey.So(xlo.Uniq([]int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}), convey.ShouldResemble, []int{1})
})
convey.Convey("string case", func() {
convey.So(xlo.Uniq([]string{"1", "2", "3"}), convey.ShouldResemble, []string{"1", "2", "3"})
convey.So(xlo.Uniq([]string{"1", "2", "1", "2", "1"}), convey.ShouldResemble, []string{"1", "2"})
})

})
}

func TestLoFindUniques(t *testing.T) {
convey.SetDefaultFailureMode(convey.FailureContinues)
convey.Convey("TestLoFindUniques", t, func() {
convey.Convey("int case", func() {
convey.So(xlo.LoUniq([]int{1, 2, 3}), convey.ShouldResemble, []int{1, 2, 3})
convey.So(xlo.LoUniq([]int{1, 2, 2, 3, 1, 2, 1, 2, 2, 3, 1, 2}), convey.ShouldResemble, []int{1, 2, 3})
convey.So(xlo.LoUniq([]int{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}), convey.ShouldResemble, []int{1})
})
convey.Convey("string case", func() {
convey.So(xlo.LoUniq([]string{"1", "2", "3"}), convey.ShouldResemble, []string{"1", "2", "3"})
convey.So(xlo.LoUniq([]string{"1", "2", "1", "2", "1"}), convey.ShouldResemble, []string{"1", "2"})
})
})
}

func TestLoMap(t *testing.T) {
convey.SetDefaultFailureMode(convey.FailureContinues)
convey.Convey("TestLoMap", t, func() {
original := []int{1, 2, 3}
ret := xlo.LoMap(original, func(item int, index int) string {
return strconv.FormatInt(int64(item), 10)
})
convey.So(ret, convey.ShouldResemble, []string{"1", "2", "3"})
})
}

func TestLopMap(t *testing.T) {
convey.SetDefaultFailureMode(convey.FailureContinues)
convey.Convey("TestLopMap", t, func() {
original := []int{1, 2, 3}
ret := xlo.LopMap(original, func(item int, index int) string {
return strconv.FormatInt(int64(item), 10)
})
convey.So(ret, convey.ShouldResemble, []string{"1", "2", "3"})
})
}

0 comments on commit 58d7b28

Please sign in to comment.