From 642c5b618d86ff4befe213227007655a00e4e231 Mon Sep 17 00:00:00 2001 From: Dusan Malusev Date: Fri, 10 May 2024 18:22:21 +0200 Subject: [PATCH 1/8] GRPC DNS Resolver -> nanodns Signed-off-by: Dusan Malusev --- go.mod | 15 +- go.sum | 36 +-- grpc/resolvers/dns_resolver.go | 326 ++++++++++++++++++++++++++++ grpc/resolvers/dns_resolver_test.go | 173 +++++++++++++++ 4 files changed, 531 insertions(+), 19 deletions(-) create mode 100644 grpc/resolvers/dns_resolver.go create mode 100644 grpc/resolvers/dns_resolver_test.go diff --git a/go.mod b/go.mod index 804113d..79514ec 100644 --- a/go.mod +++ b/go.mod @@ -9,6 +9,7 @@ require ( github.com/jackc/pgx/v5 v5.5.1 github.com/rs/zerolog v1.31.0 github.com/rzajac/zltest v0.12.0 + github.com/samber/lo v1.39.0 github.com/spf13/cobra v1.8.0 github.com/spf13/viper v1.18.1 github.com/stretchr/testify v1.8.4 @@ -16,6 +17,7 @@ require ( github.com/tinylib/msgp v1.1.9 github.com/valyala/fasthttp v1.51.0 go.mongodb.org/mongo-driver v1.13.1 + google.golang.org/grpc v1.63.2 ) replace github.com/gocql/gocql => github.com/scylladb/gocql v1.12.0 @@ -25,7 +27,7 @@ require ( github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/golang/snappy v0.0.4 // indirect - github.com/google/uuid v1.5.0 // indirect + github.com/google/uuid v1.6.0 // indirect github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed // indirect github.com/hashicorp/hcl v1.0.0 // indirect github.com/inconshreveable/mousetrap v1.1.0 // indirect @@ -58,11 +60,14 @@ require ( github.com/xdg-go/stringprep v1.0.4 // indirect github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect go.uber.org/multierr v1.11.0 // indirect - golang.org/x/crypto v0.16.0 // indirect + golang.org/x/crypto v0.23.0 // indirect golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb // indirect - golang.org/x/sync v0.5.0 // indirect - golang.org/x/sys v0.15.0 // indirect - golang.org/x/text v0.14.0 // indirect + golang.org/x/net v0.25.0 // indirect + golang.org/x/sync v0.6.0 // indirect + golang.org/x/sys v0.20.0 // indirect + golang.org/x/text v0.15.0 // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240509183442-62759503f434 // indirect + google.golang.org/protobuf v1.34.1 // indirect gopkg.in/inf.v0 v0.9.1 // indirect gopkg.in/ini.v1 v1.67.0 // indirect gopkg.in/yaml.v3 v3.0.1 // indirect diff --git a/go.sum b/go.sum index d362601..27a743e 100644 --- a/go.sum +++ b/go.sum @@ -30,10 +30,10 @@ github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= -github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= -github.com/google/uuid v1.5.0 h1:1p67kYwdtXjb0gL0BPiP1Av9wiZPo5A8z2cWkTZ+eyU= -github.com/google/uuid v1.5.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed h1:5upAirOpQc1Q53c0bnx2ufif5kANL7bfZWcc6VJWJd8= github.com/hailocab/go-hostpool v0.0.0-20160125115350-e80d13ce29ed/go.mod h1:tMWxXQ9wFIaZeTI9F+hmhFiGpFmhOHzyShyFUhRm0H4= github.com/hashicorp/hcl v1.0.0 h1:0Anlzjpi4vEasTeNFn2mLJgTSwt0+6sfsiTG8qcWGx4= @@ -100,6 +100,8 @@ github.com/sagikazarmark/locafero v0.4.0 h1:HApY1R9zGo4DBgr7dqsTH/JJxLTTsOt7u6ke github.com/sagikazarmark/locafero v0.4.0/go.mod h1:Pe1W6UlPYUk/+wc/6KFhbORCfqzgYEpgQ3O5fPuL3H4= github.com/sagikazarmark/slog-shim v0.1.0 h1:diDBnUNK9N/354PgrxMywXnAwEr1QZcOr6gto+ugjYE= github.com/sagikazarmark/slog-shim v0.1.0/go.mod h1:SrcSrq8aKtyuqEI1uvTDTK1arOWRIczQRv+GVI1AkeQ= +github.com/samber/lo v1.39.0 h1:4gTz1wUhNYLhFSKl6O+8peW0v2F4BCY034GRpU9WnuA= +github.com/samber/lo v1.39.0/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= github.com/scylladb/gocql v1.12.0 h1:KaP25dC2Mu0H382M8KZmkQp1XuasgBG97bBhFeFKVyk= github.com/scylladb/gocql v1.12.0/go.mod h1:ZLEJ0EVE5JhmtxIW2stgHq/v1P4fWap0qyyXSKyV8K0= github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo= @@ -160,8 +162,8 @@ golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8U golang.org/x/crypto v0.0.0-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/crypto v0.16.0 h1:mMMrFzRSCF0GvB7Ne27XVtVAaXLrPmgPC7/v0tkwHaY= -golang.org/x/crypto v0.16.0/go.mod h1:gCAAfMLgwOJRpTjQ2zCCt2OcSfYMTeZVSRtQlPC7Nq4= +golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= +golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb h1:c0vyKkb6yr3KR7jEfJaOSv4lG7xPkbN6r52aJz1d8a8= golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= @@ -173,13 +175,13 @@ golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96b golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220526153639-5463443f8c37/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= -golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c= -golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U= +golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= +golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= -golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= +golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 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-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= @@ -192,8 +194,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= -golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/sys v0.20.0 h1:Od9JTbYCk261bKm4M/mw7AklTlFYIa0bIp9BgSm1S8Y= +golang.org/x/sys v0.20.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= @@ -202,8 +204,8 @@ golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= -golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= -golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= +golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk= @@ -212,6 +214,12 @@ golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8T golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240509183442-62759503f434 h1:umK/Ey0QEzurTNlsV3R+MfxHAb78HCEX/IkuR+zH4WQ= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240509183442-62759503f434/go.mod h1:I7Y+G38R2bu5j1aLzfFmQfTcU/WnFuqDwLZAbvKTKpM= +google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM= +google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA= +google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg= +google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= diff --git a/grpc/resolvers/dns_resolver.go b/grpc/resolvers/dns_resolver.go new file mode 100644 index 0000000..91b60a6 --- /dev/null +++ b/grpc/resolvers/dns_resolver.go @@ -0,0 +1,326 @@ +package resolvers + +import ( + "context" + "errors" + "fmt" + "net" + "slices" + "strconv" + "strings" + "sync" + "time" + + grpclbstate "google.golang.org/grpc/balancer/grpclb/state" + "google.golang.org/grpc/resolver" +) + +type ( + DnsResolver struct { + cc resolver.ClientConn + resolver NetResolver + resolveNow chan struct{} + cancel context.CancelFunc + ips []resolver.Address + wg sync.WaitGroup + mu sync.RWMutex + } + + DnsResolverBuilder struct { + ctx context.Context + options *option + } + + NetResolver interface { + LookupHost(context.Context, string) ([]string, error) + LookupIPAddr(context.Context, string) ([]net.IPAddr, error) + LookupSRV(context.Context, string, string, string) (string, []*net.SRV, error) + } + + option struct { + resolver NetResolver + dnsAuthority string + dnsReResolving time.Duration + resolvingTimeout time.Duration + defaultPort uint16 + } + + Option func(o *option) +) + +func WithResolver(r NetResolver) Option { + return func(o *option) { + o.resolver = r + } +} + +func WithDnsReResolving(d time.Duration) Option { + return func(o *option) { + o.dnsReResolving = d + } +} + +func WithResolvingTimeout(d time.Duration) Option { + return func(o *option) { + o.resolvingTimeout = d + } +} + +func WithDnsAuthority(authority string) Option { + return func(o *option) { + o.dnsAuthority = authority + } +} + +func WithDefaultPort(p uint16) Option { + return func(o *option) { + o.defaultPort = p + } +} + +func NewDnsResolverBuilder(ctx context.Context, opts ...Option) *DnsResolverBuilder { + cfg := &option{ + dnsReResolving: 30 * time.Second, + defaultPort: 443, + resolvingTimeout: 30 * time.Second, + } + + for _, opt := range opts { + opt(cfg) + } + + return &DnsResolverBuilder{ + ctx: ctx, + options: cfg, + } +} + +func (d *DnsResolverBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) { + ctx, cancel := context.WithCancel(d.ctx) + endpoint := target.Endpoint() + + host, port, err := parseTarget(endpoint, strconv.FormatInt(int64(d.options.defaultPort), 10)) + if err != nil { + cancel() + return nil, err + } + + if ipAddr, ok := formatIP(host); ok { + addr := []resolver.Address{{Addr: ipAddr + ":" + port}} + _ = cc.UpdateState(resolver.State{Addresses: addr}) + cancel() + return resolver.Get("dead").Build(target, cc, opts) + } + + if d.options.resolver == nil { + d.options.resolver = &net.Resolver{ + PreferGo: true, + Dial: func(ctx context.Context, network, address string) (net.Conn, error) { + authority := address + + if d.options.dnsAuthority != "" { + authority = d.options.dnsAuthority + } + + dialer := net.Dialer{ + Timeout: d.options.resolvingTimeout, + KeepAlive: 1 * time.Minute, + } + + return dialer.DialContext(ctx, network, authority) + }, + } + } + + resolver := &DnsResolver{ + ips: make([]resolver.Address, 0), + resolveNow: make(chan struct{}, 1), + cancel: cancel, + cc: cc, + resolver: d.options.resolver, + } + + if err := resolver.resolve(ctx, host, port, d.options.resolvingTimeout); err != nil { + return nil, err + } + + resolver.wg.Add(1) + go func() { + defer resolver.wg.Done() + + go resolver.watcher(ctx, host, port, d.options.dnsReResolving, d.options.resolvingTimeout) + }() + + return resolver, nil +} + +func (b *DnsResolverBuilder) Scheme() string { + return "nanodns" +} + +func (d *DnsResolver) resolve(ctx context.Context, host, port string, timeout time.Duration) error { + ips, err := resolve(ctx, d.resolver, host, port, timeout) + if err != nil { + if errors.Is(err, errNoChange) { + return nil + } + d.cc.ReportError(err) + return err + } + + slices.SortStableFunc(ips.Addresses, func(a, b resolver.Address) int { + return strings.Compare(a.Addr, b.Addr) + }) + + d.mu.RLock() + { + eq := slices.EqualFunc(d.ips, ips.Addresses, func(a, b resolver.Address) bool { + return a.Addr == b.Addr + }) + + if eq { + d.mu.RUnlock() + return nil + } + } + d.mu.RUnlock() + + d.mu.Lock() + { + d.ips = d.ips[:0] + d.ips = append(d.ips, ips.Addresses...) + } + d.mu.Unlock() + + if err := d.cc.UpdateState(ips); err != nil { + d.cc.ReportError(err) + } + + return nil +} + +func (d *DnsResolver) watcher(ctx context.Context, host, port string, refresh, timeout time.Duration) { + ticker := time.NewTicker(refresh) + reResolveTimeout := time.NewTicker(refresh) + + defer func() { + ticker.Stop() + reResolveTimeout.Stop() + }() + + for { + select { + case <-ctx.Done(): + return + case <-ticker.C: + _ = d.resolve(ctx, host, port, timeout) + case <-d.resolveNow: + select { + case <-ctx.Done(): + return + case <-reResolveTimeout.C: + // Prevents spam calls to the DNS Resolver + // has to wait for at least 30 seconds + // between each call + _ = d.resolve(ctx, host, port, timeout) + } + } + } +} + +func resolve(ctx context.Context, r NetResolver, host, port string, timeout time.Duration) (resolver.State, error) { + ctx, cancel := context.WithTimeout(ctx, timeout) + defer cancel() + + addrs, err := r.LookupHost(ctx, host) + if err != nil { + return resolver.State{}, err + } + + if len(addrs) == 0 { + return resolver.State{}, nil + } + + addr := make([]resolver.Address, len(addrs)) + + for i, a := range addrs { + addr[i] = resolver.Address{Addr: net.JoinHostPort(a, port)} + } + + state := resolver.State{Addresses: addr} + + _, srvs, err := r.LookupSRV(ctx, "grpclb", "tcp", host) + + if err == nil && len(srvs) > 0 { + var newAddrs []resolver.Address + + for _, s := range srvs { + lbAddrs, err := r.LookupHost(ctx, s.Target) + if err != nil { + continue + } + + for _, a := range lbAddrs { + ip, ok := formatIP(a) + if ok { + addr := ip + ":" + strconv.Itoa(int(s.Port)) + newAddrs = append(newAddrs, resolver.Address{Addr: addr, ServerName: s.Target}) + } + } + } + + state = grpclbstate.Set(state, &grpclbstate.State{ + BalancerAddresses: newAddrs, + }) + } + + return state, nil +} + +func (d *DnsResolver) ResolveNow(resolver.ResolveNowOptions) { + select { + case d.resolveNow <- struct{}{}: + default: + } +} + +func (d *DnsResolver) Close() { + d.cancel() + d.wg.Wait() + close(d.resolveNow) +} + +func formatIP(addr string) (addrIP string, ok bool) { + ip := net.ParseIP(addr) + if ip == nil { + return "", false + } + if ip.To4() != nil { + return addr, true + } + return "[" + addr + "]", true +} + +var ( + ErrEndsWithColon = errors.New("") + errNoChange = errors.New("no ip changes") +) + +func parseTarget(target, defaultPort string) (host, port string, err error) { + if ip := net.ParseIP(target); ip != nil { + return target, defaultPort, nil + } + if host, port, err = net.SplitHostPort(target); err == nil { + if port == "" { + return "", "", ErrEndsWithColon + } + if host == "" { + host = "localhost" + } + return host, port, nil + } + if host, port, err = net.SplitHostPort(target + ":" + defaultPort); err == nil { + return host, port, nil + } + return "", "", fmt.Errorf("invalid target address %v, error info: %v", target, err) +} diff --git a/grpc/resolvers/dns_resolver_test.go b/grpc/resolvers/dns_resolver_test.go new file mode 100644 index 0000000..e3d78a3 --- /dev/null +++ b/grpc/resolvers/dns_resolver_test.go @@ -0,0 +1,173 @@ +package resolvers_test + +import ( + "context" + "net" + "net/url" + "testing" + "time" + + "github.com/nano-interactive/go-utils/v2/grpc/resolvers" + "github.com/samber/lo" + "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" + "google.golang.org/grpc/resolver" + + "google.golang.org/grpc/serviceconfig" +) + +type ( + MockClientConn struct { + mock.Mock + } + MockNetResolver struct { + mock.Mock + } +) + +func (m *MockClientConn) UpdateState(p0 resolver.State) error { + args := m.Called(p0) + return args.Error(0) +} + +func (m *MockClientConn) ReportError(p0 error) { + _ = m.Called(p0) +} + +func (m *MockClientConn) NewAddress(addresses []resolver.Address) { + panic("SHOULD PANIC: NOT EXPECTED ANYWHERE") +} + +func (m *MockClientConn) ParseServiceConfig(serviceConfigJSON string) *serviceconfig.ParseResult { + panic("SHOULD PANIC: NOT EXPECTED ANYWHERE") +} + +func (m *MockNetResolver) LookupHost(p0 context.Context, p1 string) ([]string, error) { + args := m.Called(p1) + + return args.Get(0).([]string), args.Error(1) +} + +func (m *MockNetResolver) LookupIPAddr(p0 context.Context, p1 string) ([]net.IPAddr, error) { + args := m.Called(p1) + + return args.Get(0).([]net.IPAddr), args.Error(1) +} + +func (m *MockNetResolver) LookupSRV(p0 context.Context, p1 string, p2 string, p3 string) (string, []*net.SRV, error) { + args := m.Called(p1, p2, p3) + + return args.String(0), args.Get(1).([]*net.SRV), args.Error(2) +} + +func getDNSBuilder(resolver resolvers.NetResolver, resolving time.Duration) *resolvers.DnsResolverBuilder { + return resolvers.NewDnsResolverBuilder( + context.Background(), + resolvers.WithDnsReResolving(resolving), + resolvers.WithResolvingTimeout(3*time.Second), + resolvers.WithDefaultPort(3000), + resolvers.WithResolver(resolver), + ) +} + +func TestDNSResolver(t *testing.T) { + t.Parallel() + assert := require.New(t) + + conn := &MockClientConn{} + builder := getDNSBuilder(nil, 1*time.Hour) + + conn. + On("UpdateState", mock.Anything). + Once(). + Return(nil) + + r, err := builder.Build( + resolver.Target{ + URL: *lo.Must(url.Parse("dusanmalusev.dev")), + }, + conn, + resolver.BuildOptions{}, + ) + + // As it domain has already been resolved, UpdateState should be called only Once + // on .Build(), and never again on .ResolveNow() + r.ResolveNow(resolver.ResolveNowOptions{}) + r.ResolveNow(resolver.ResolveNowOptions{}) + + assert.Equal("nanodns", builder.Scheme()) + assert.NoError(err) + assert.NotNil(r) + conn.AssertExpectations(t) + + r.Close() // Stop the watcher +} + +func TestDNSResolver_TriggerIPChange(t *testing.T) { + t.Parallel() + assert := require.New(t) + + conn := &MockClientConn{} + netResolver := &MockNetResolver{} + builder := getDNSBuilder(netResolver, 400*time.Millisecond) + + conn. + On("UpdateState", resolver.State{ + Addresses: []resolver.Address{ + {Addr: "1.1.1.1:3000"}, + {Addr: "1.1.1.2:3000"}, + }, + }). + Once(). + Return(nil) + + netResolver. + On("LookupHost", "google.com"). + Return([]string{"1.1.1.1", "1.1.1.2"}, nil) + + netResolver. + On("LookupSRV", "grpclb", "tcp", "google.com"). + Return("google.com", []*net.SRV{}, nil) + + r, err := builder.Build( + resolver.Target{ + URL: *lo.Must(url.Parse("google.com")), + }, + conn, + resolver.BuildOptions{}, + ) + + assert.NoError(err) + assert.NotNil(r) + + netResolver.AssertExpectations(t) + conn.AssertExpectations(t) + + netResolver.ExpectedCalls = nil + conn.ExpectedCalls = nil + + netResolver. + On("LookupHost", "google.com"). + Return([]string{"1.1.1.2", "1.1.1.3"}, nil) + + conn. + On("UpdateState", resolver.State{ + Addresses: []resolver.Address{ + {Addr: "1.1.1.2:3000"}, + {Addr: "1.1.1.3:3000"}, + }, + }). + Once(). + Return(nil) + + netResolver. + On("LookupSRV", "grpclb", "tcp", "google.com"). + Return("google.com", []*net.SRV{}, nil) + + r.ResolveNow(resolver.ResolveNowOptions{}) + + time.Sleep(1 * time.Second) + + netResolver.AssertExpectations(t) + conn.AssertExpectations(t) +} From 10457b8088c9b58bb03c564a697c82d108e1f7ed Mon Sep 17 00:00:00 2001 From: Dusan Malusev Date: Fri, 10 May 2024 18:23:09 +0200 Subject: [PATCH 2/8] fmt Signed-off-by: Dusan Malusev --- grpc/resolvers/dns_resolver_test.go | 7 ++++--- testing/cmd/cmd.go | 2 -- testing/fiber/fiber.go | 3 ++- testing/mysql.go | 2 -- testing/testing.go | 5 ++--- types/objectid.go | 3 ++- types/types.go | 3 ++- types/uuid.go | 1 - url.go | 7 +++---- url_test.go | 11 +---------- utils.go | 4 ---- validators/mongoid.go | 1 + 12 files changed, 17 insertions(+), 32 deletions(-) diff --git a/grpc/resolvers/dns_resolver_test.go b/grpc/resolvers/dns_resolver_test.go index e3d78a3..ae55451 100644 --- a/grpc/resolvers/dns_resolver_test.go +++ b/grpc/resolvers/dns_resolver_test.go @@ -7,13 +7,14 @@ import ( "testing" "time" - "github.com/nano-interactive/go-utils/v2/grpc/resolvers" "github.com/samber/lo" + "google.golang.org/grpc/resolver" + "google.golang.org/grpc/serviceconfig" + "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" - "google.golang.org/grpc/resolver" - "google.golang.org/grpc/serviceconfig" + "github.com/nano-interactive/go-utils/v2/grpc/resolvers" ) type ( diff --git a/testing/cmd/cmd.go b/testing/cmd/cmd.go index 850ae0b..561a285 100644 --- a/testing/cmd/cmd.go +++ b/testing/cmd/cmd.go @@ -110,8 +110,6 @@ func startCommand(t testing.TB, ctx context.Context, root *cobra.Command, args A } } -type () - func WithTimeout(wait time.Duration) Option { return func(options *Options) { options.timeout = wait diff --git a/testing/fiber/fiber.go b/testing/fiber/fiber.go index 8fc4748..9578267 100644 --- a/testing/fiber/fiber.go +++ b/testing/fiber/fiber.go @@ -1,11 +1,12 @@ package fiber import ( - "github.com/gofiber/fiber/v2" "net/http" "testing" "time" + "github.com/gofiber/fiber/v2" + nano_http "github.com/nano-interactive/go-utils/v2/testing/http" "github.com/valyala/fasthttp" ) diff --git a/testing/mysql.go b/testing/mysql.go index d985d5e..d77d092 100644 --- a/testing/mysql.go +++ b/testing/mysql.go @@ -78,7 +78,6 @@ func CreateMySQL(t testing.TB, optMaker MySQLOptions) (*sql.DB, string) { _, err = mysqlClient.Exec( fmt.Sprintf("GRANT ALL PRIVILEGES ON %s.* TO '%s'@'%%' WITH GRANT OPTION;", dbName, opts.User), ) - if err != nil { t.Errorf("Failed to grant privileges for database %s to user %s: %v", dbName, opts.User, err) t.FailNow() @@ -95,7 +94,6 @@ func CreateMySQL(t testing.TB, optMaker MySQLOptions) (*sql.DB, string) { dbName, ), ) - if err != nil { t.Errorf("failed to open connection to database: %s %v", dbName, err) t.FailNow() diff --git a/testing/testing.go b/testing/testing.go index f70a9eb..843b94a 100644 --- a/testing/testing.go +++ b/testing/testing.go @@ -2,12 +2,13 @@ package testing import ( "context" - "github.com/nano-interactive/go-utils/v2" "os" "path/filepath" "testing" "time" + "github.com/nano-interactive/go-utils/v2" + "github.com/nano-interactive/go-utils/v2/config" "github.com/spf13/viper" ) @@ -60,7 +61,6 @@ func FindFile(t testing.TB, fileName string) string { } workingDir, err = utils.GetAbsolutePath(filepath.Join(workingDir, "..")) - if err != nil { t.Errorf("failed to get absolute path from %s", filepath.Join(workingDir, "..")) t.FailNow() @@ -93,7 +93,6 @@ func ProjectRootDir(t testing.TB) string { } workingDir, err = utils.GetAbsolutePath(filepath.Join(workingDir, "..")) - if err != nil { t.Errorf("failed to get absolute path from %s", filepath.Join(workingDir, "..")) t.FailNow() diff --git a/types/objectid.go b/types/objectid.go index b517b63..37af276 100644 --- a/types/objectid.go +++ b/types/objectid.go @@ -2,9 +2,10 @@ package types import ( "errors" - "github.com/nano-interactive/go-utils/v2" "time" + "github.com/nano-interactive/go-utils/v2" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsontype" "go.mongodb.org/mongo-driver/bson/primitive" diff --git a/types/types.go b/types/types.go index 311b2ed..60da821 100644 --- a/types/types.go +++ b/types/types.go @@ -6,10 +6,11 @@ import ( "database/sql/driver" "encoding/json" "errors" - "github.com/nano-interactive/go-utils/v2" "reflect" "time" + "github.com/nano-interactive/go-utils/v2" + "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson/bsontype" ) diff --git a/types/uuid.go b/types/uuid.go index 567e98f..64604de 100644 --- a/types/uuid.go +++ b/types/uuid.go @@ -107,7 +107,6 @@ func (u *CQLUUID) DecodeMsg(dc *msgp.Reader) (err error) { case "CQLUUID": var uuid [16]byte _, err = dc.ReadBytes(uuid[:]) - if err != nil { err = msgp.WrapError(err, "CQLUUID") return diff --git a/url.go b/url.go index 48a4c85..e8d0c64 100644 --- a/url.go +++ b/url.go @@ -20,12 +20,11 @@ var ( ) func TrimUrlForScylla(fullUrl string) (scyllaUrl string, hostName string, err error) { - trimmedUrl := strings.TrimSpace(fullUrl) if len(trimmedUrl) == 0 { return "", "", ErrInvalidUrl } - //remove everything behind ? or # + // remove everything behind ? or # if index := strings.Index(trimmedUrl, "?"); index > 0 { trimmedUrl = trimmedUrl[0:index] } @@ -59,10 +58,10 @@ func TrimUrlForScyllaOld(fullUrl string) (scyllaUrl string, hostName string, err var data strings.Builder trimmedUrl := strings.TrimSpace(fullUrl) - //todo remove url parse and use simple cut all after '?' and '#' , replace http with https if needed + // todo remove url parse and use simple cut all after '?' and '#' , replace http with https if needed urlObject, err := url.Parse(trimmedUrl) if err != nil { - //remove everything behind ? or # + // remove everything behind ? or # if index := strings.Index(trimmedUrl, "?"); index > 0 { trimmedUrl = trimmedUrl[0:index] } diff --git a/url_test.go b/url_test.go index eded025..9f016a3 100644 --- a/url_test.go +++ b/url_test.go @@ -161,43 +161,36 @@ func TestTrimUrlForScylla(t *testing.T) { wantHost: "www.oraridiapertura24.it", }, { - fullUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\\ufffd\\ufffd", wantUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\\ufffd\\ufffd/", wantHost: "www.goal.com", }, { - fullUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\ufffd\ufffd", wantUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\ufffd\ufffd/", wantHost: "www.goal.com", }, { - fullUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\xf0\x8c\xbc", wantUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล/", wantHost: "www.goal.com", }, { - fullUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\xf8\xa1\xa1\xa1\xa1", wantUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล/", wantHost: "www.goal.com", }, { - fullUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\ufffd\ufffd", wantUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล��/", wantHost: "www.goal.com", }, { - fullUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล��", wantUrl: "https://www.goal.com/th/ข่าว/แดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล\ufffd\ufffd/", wantHost: "www.goal.com", }, { - fullUrl: "https://www.goal.com/th/ข่าว/ʘԊ ԋꙨ ꙩんԽ խแดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล", wantUrl: "https://www.goal.com/th/ข่าว/ʘԊ ԋꙨ ꙩんԽ խแดงเดือดมาตามนัด-แมนฯ-ยูฯ-ดวล/", wantHost: "www.goal.com", @@ -246,21 +239,19 @@ func TestTrimUrlForScylla(t *testing.T) { } for _, tt := range tests { t.Run(tt.fullUrl, func(_ *testing.T) { - resultUrl, resultHost, err := TrimUrlForScylla(tt.fullUrl) assert.NoError(err) assert.Equal(tt.wantUrl, resultUrl) assert.Equal(tt.wantHost, resultHost) - //to do check if double parse is changing anything + // to do check if double parse is changing anything resultUrl, resultHost, err = TrimUrlForScylla(tt.fullUrl) resultUrl, resultHost, err = TrimUrlForScylla(resultUrl) assert.NoError(err) assert.Equal(tt.wantUrl, resultUrl) assert.Equal(tt.wantHost, resultHost) - }) } } diff --git a/utils.go b/utils.go index 244b1c7..8bb6456 100644 --- a/utils.go +++ b/utils.go @@ -73,7 +73,6 @@ func GetAbsolutePath(path string) (string, error) { if !filepath.IsAbs(path) { path, err = filepath.Abs(path) - if err != nil { return "", err } @@ -105,7 +104,6 @@ func CreateDirectoryFromFile(path string, perm fs.FileMode) (string, error) { // Returns file instance and error if it fails func CreateFile(path string, flags int, dirMode, mode fs.FileMode) (file *os.File, err error) { path, err = CreateDirectoryFromFile(path, dirMode|fs.ModeDir) - if err != nil { return nil, err } @@ -117,7 +115,6 @@ func CreateFile(path string, flags int, dirMode, mode fs.FileMode) (file *os.Fil //#nosec G304 file, err = os.Create(path) - if err != nil { return nil, err } @@ -184,7 +181,6 @@ func init() { if pixel == nil { var err error pixel, err = base64.StdEncoding.DecodeString(image) - if err != nil { panic(err) } diff --git a/validators/mongoid.go b/validators/mongoid.go index e78f86d..9d43068 100644 --- a/validators/mongoid.go +++ b/validators/mongoid.go @@ -2,6 +2,7 @@ package validators import ( "encoding/hex" + "github.com/nano-interactive/go-utils/v2" "github.com/nano-interactive/go-utils/v2/types" From 35f06d583c1faabc5d3adaaca5c4a759f0e1a5b9 Mon Sep 17 00:00:00 2001 From: Dusan Malusev Date: Fri, 10 May 2024 18:31:18 +0200 Subject: [PATCH 3/8] lints Signed-off-by: Dusan Malusev --- randint/randint_test.go | 13 ++++--------- testing/cmd/cmd.go | 1 + types/types.go | 22 +++++++++++----------- url.go | 3 +-- url_test.go | 4 ++++ validators/mongoid_test.go | 2 +- 6 files changed, 22 insertions(+), 23 deletions(-) diff --git a/randint/randint_test.go b/randint/randint_test.go index 2cc8020..3ec70da 100644 --- a/randint/randint_test.go +++ b/randint/randint_test.go @@ -2,7 +2,6 @@ package randint import ( "testing" - "unsafe" "github.com/stretchr/testify/require" ) @@ -14,11 +13,10 @@ func TestUint8(t *testing.T) { assert := require.New(t) // act - v, err := Uint8() + _, err := Uint8() // assert assert.NoError(err) - assert.Equal(uint8Len, int(unsafe.Sizeof(v))) } func TestUint16(t *testing.T) { @@ -28,11 +26,10 @@ func TestUint16(t *testing.T) { assert := require.New(t) // act - v, err := Uint16() + _, err := Uint16() // assert assert.NoError(err) - assert.Equal(uint16Len, int(unsafe.Sizeof(v))) } func TestUint32(t *testing.T) { @@ -42,11 +39,10 @@ func TestUint32(t *testing.T) { assert := require.New(t) // act - v, err := Uint32() + _, err := Uint32() // assert assert.NoError(err) - assert.Equal(uint32Len, int(unsafe.Sizeof(v))) } func TestUint64(t *testing.T) { @@ -56,9 +52,8 @@ func TestUint64(t *testing.T) { assert := require.New(t) // act - v, err := Uint64() + _, err := Uint64() // assert assert.NoError(err) - assert.Equal(uint64Len, int(unsafe.Sizeof(v))) } diff --git a/testing/cmd/cmd.go b/testing/cmd/cmd.go index 561a285..ec6221a 100644 --- a/testing/cmd/cmd.go +++ b/testing/cmd/cmd.go @@ -82,6 +82,7 @@ func startCommand(t testing.TB, ctx context.Context, root *cobra.Command, args A } for k, item := range args.ContextItems { + //nolint:staticcheck ctx = context.WithValue(ctx, k, item) } diff --git a/types/types.go b/types/types.go index 60da821..1779473 100644 --- a/types/types.go +++ b/types/types.go @@ -437,7 +437,7 @@ func (o NullUint64) MarshalJSON() ([]byte, error) { } func (o *NullByte) UnmarshalJSON(value []byte) error { - if bytes.Compare(jsonNullBytes, value) == 0 { + if bytes.Equal(jsonNullBytes, value) { o.Valid = false return nil } @@ -453,7 +453,7 @@ func (o *NullByte) UnmarshalJSON(value []byte) error { } func (o *NullFloat32) UnmarshalJSON(value []byte) error { - if bytes.Compare(jsonNullBytes, value) == 0 { + if bytes.Equal(jsonNullBytes, value) { o.Valid = false return nil } @@ -469,7 +469,7 @@ func (o *NullFloat32) UnmarshalJSON(value []byte) error { } func (o *NullFloat64) UnmarshalJSON(value []byte) error { - if bytes.Compare(jsonNullBytes, value) == 0 { + if bytes.Equal(jsonNullBytes, value) { o.Valid = false return nil } @@ -485,7 +485,7 @@ func (o *NullFloat64) UnmarshalJSON(value []byte) error { } func (o *NullTime) UnmarshalJSON(value []byte) error { - if bytes.Compare(jsonNullBytes, value) == 0 { + if bytes.Equal(jsonNullBytes, value) { o.Valid = false return nil } @@ -501,7 +501,7 @@ func (o *NullTime) UnmarshalJSON(value []byte) error { } func (o *NullString) UnmarshalJSON(value []byte) error { - if bytes.Compare(jsonNullBytes, value) == 0 { + if bytes.Equal(jsonNullBytes, value) { o.Valid = false return nil } @@ -517,7 +517,7 @@ func (o *NullString) UnmarshalJSON(value []byte) error { } func (o *NullInt16) UnmarshalJSON(value []byte) error { - if bytes.Compare(jsonNullBytes, value) == 0 { + if bytes.Equal(jsonNullBytes, value) { o.Valid = false return nil } @@ -533,7 +533,7 @@ func (o *NullInt16) UnmarshalJSON(value []byte) error { } func (o *NullInt32) UnmarshalJSON(value []byte) error { - if bytes.Compare(jsonNullBytes, value) == 0 { + if bytes.Equal(jsonNullBytes, value) { o.Valid = false return nil } @@ -549,7 +549,7 @@ func (o *NullInt32) UnmarshalJSON(value []byte) error { } func (o *NullInt64) UnmarshalJSON(value []byte) error { - if bytes.Compare(jsonNullBytes, value) == 0 { + if bytes.Equal(jsonNullBytes, value) { o.Valid = false return nil } @@ -565,7 +565,7 @@ func (o *NullInt64) UnmarshalJSON(value []byte) error { } func (o *NullUint16) UnmarshalJSON(value []byte) error { - if bytes.Compare(jsonNullBytes, value) == 0 { + if bytes.Equal(jsonNullBytes, value) { o.Valid = false return nil } @@ -581,7 +581,7 @@ func (o *NullUint16) UnmarshalJSON(value []byte) error { } func (o *NullUint32) UnmarshalJSON(value []byte) error { - if bytes.Compare(jsonNullBytes, value) == 0 { + if bytes.Equal(jsonNullBytes, value) { o.Valid = false return nil } @@ -597,7 +597,7 @@ func (o *NullUint32) UnmarshalJSON(value []byte) error { } func (o *NullUint64) UnmarshalJSON(value []byte) error { - if bytes.Compare(jsonNullBytes, value) == 0 { + if bytes.Equal(jsonNullBytes, value) { o.Valid = false return nil } diff --git a/url.go b/url.go index e8d0c64..f48265e 100644 --- a/url.go +++ b/url.go @@ -37,7 +37,7 @@ func TrimUrlForScylla(fullUrl string) (scyllaUrl string, hostName string, err er trimmedUrl = trimmedUrl + "/" } - if strings.Index(trimmedUrl, "http") == -1 { + if strings.Index(trimmedUrl, "http") == 0 { trimmedUrl = "https://" + trimmedUrl } else { trimmedUrl = strings.Replace(trimmedUrl, "http://", "https://", 1) @@ -58,7 +58,6 @@ func TrimUrlForScyllaOld(fullUrl string) (scyllaUrl string, hostName string, err var data strings.Builder trimmedUrl := strings.TrimSpace(fullUrl) - // todo remove url parse and use simple cut all after '?' and '#' , replace http with https if needed urlObject, err := url.Parse(trimmedUrl) if err != nil { // remove everything behind ? or # diff --git a/url_test.go b/url_test.go index 9f016a3..23e10db 100644 --- a/url_test.go +++ b/url_test.go @@ -247,6 +247,10 @@ func TestTrimUrlForScylla(t *testing.T) { // to do check if double parse is changing anything resultUrl, resultHost, err = TrimUrlForScylla(tt.fullUrl) + assert.NoError(err) + assert.Equal(tt.wantUrl, resultUrl) + assert.Equal(tt.wantHost, resultHost) + resultUrl, resultHost, err = TrimUrlForScylla(resultUrl) assert.NoError(err) diff --git a/validators/mongoid_test.go b/validators/mongoid_test.go index df19436..7239f6a 100644 --- a/validators/mongoid_test.go +++ b/validators/mongoid_test.go @@ -61,7 +61,7 @@ func BenchmarkIsObjectId(b *testing.B) { objectId := "652e77eeadd7d603e4420c3" for i := 0; i < b.N; i++ { - NewObjectIDRule().Validate(objectId) + _ = NewObjectIDRule().Validate(objectId) } }) } From edaa7f091acfead256d6d7b82fbe2e048482fc88 Mon Sep 17 00:00:00 2001 From: Dusan Malusev Date: Fri, 10 May 2024 18:32:57 +0200 Subject: [PATCH 4/8] update deps Signed-off-by: Dusan Malusev --- go.mod | 34 +++++++++++++++------------- go.sum | 71 +++++++++++++++++++++++++--------------------------------- 2 files changed, 48 insertions(+), 57 deletions(-) diff --git a/go.mod b/go.mod index 79514ec..6bd6410 100644 --- a/go.mod +++ b/go.mod @@ -1,29 +1,31 @@ module github.com/nano-interactive/go-utils/v2 -go 1.21 +go 1.22 + +toolchain go1.22.2 require ( github.com/gocql/gocql v1.6.0 - github.com/gofiber/fiber/v2 v2.51.0 + github.com/gofiber/fiber/v2 v2.52.4 github.com/invopop/validation v0.3.0 - github.com/jackc/pgx/v5 v5.5.1 - github.com/rs/zerolog v1.31.0 + github.com/jackc/pgx/v5 v5.5.5 + github.com/rs/zerolog v1.32.0 github.com/rzajac/zltest v0.12.0 github.com/samber/lo v1.39.0 github.com/spf13/cobra v1.8.0 - github.com/spf13/viper v1.18.1 - github.com/stretchr/testify v1.8.4 + github.com/spf13/viper v1.18.2 + github.com/stretchr/testify v1.9.0 github.com/tanimutomo/sqlfile v1.0.0 github.com/tinylib/msgp v1.1.9 - github.com/valyala/fasthttp v1.51.0 - go.mongodb.org/mongo-driver v1.13.1 + github.com/valyala/fasthttp v1.52.0 + go.mongodb.org/mongo-driver v1.15.0 google.golang.org/grpc v1.63.2 ) replace github.com/gocql/gocql => github.com/scylladb/gocql v1.12.0 require ( - github.com/andybalholm/brotli v1.0.6 // indirect + github.com/andybalholm/brotli v1.1.0 // indirect github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/golang/snappy v0.0.4 // indirect @@ -34,36 +36,36 @@ require ( github.com/jackc/pgpassfile v1.0.0 // indirect github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 // indirect github.com/jackc/puddle/v2 v2.2.1 // indirect - github.com/klauspost/compress v1.17.4 // indirect + github.com/klauspost/compress v1.17.8 // indirect github.com/magiconair/properties v1.8.7 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.15 // indirect github.com/mitchellh/mapstructure v1.5.0 // indirect github.com/montanaflynn/stats v0.7.1 // indirect - github.com/pelletier/go-toml/v2 v2.1.1 // indirect + github.com/pelletier/go-toml/v2 v2.2.2 // indirect github.com/philhofer/fwd v1.1.2 // indirect github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect - github.com/rivo/uniseg v0.4.4 // indirect + github.com/rivo/uniseg v0.4.7 // indirect github.com/sagikazarmark/locafero v0.4.0 // indirect github.com/sagikazarmark/slog-shim v0.1.0 // indirect github.com/sourcegraph/conc v0.3.0 // indirect github.com/spf13/afero v1.11.0 // indirect github.com/spf13/cast v1.6.0 // indirect github.com/spf13/pflag v1.0.5 // indirect - github.com/stretchr/objx v0.5.1 // indirect + github.com/stretchr/objx v0.5.2 // indirect github.com/subosito/gotenv v1.6.0 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/tcplisten v1.0.0 // indirect github.com/xdg-go/pbkdf2 v1.0.0 // indirect github.com/xdg-go/scram v1.1.2 // indirect github.com/xdg-go/stringprep v1.0.4 // indirect - github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a // indirect + github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/crypto v0.23.0 // indirect - golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb // indirect + golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 // indirect golang.org/x/net v0.25.0 // indirect - golang.org/x/sync v0.6.0 // indirect + golang.org/x/sync v0.7.0 // indirect golang.org/x/sys v0.20.0 // indirect golang.org/x/text v0.15.0 // indirect google.golang.org/genproto/googleapis/rpc v0.0.0-20240509183442-62759503f434 // indirect diff --git a/go.sum b/go.sum index 27a743e..7b9bb21 100644 --- a/go.sum +++ b/go.sum @@ -1,7 +1,7 @@ github.com/DATA-DOG/go-sqlmock v1.5.0 h1:Shsta01QNfFxHCfpW6YH2STWB0MudeXXEWMr20OEh60= github.com/DATA-DOG/go-sqlmock v1.5.0/go.mod h1:f/Ixk793poVmq4qj/V1dPUg2JEAKC73Q5eFN3EC/SaM= -github.com/andybalholm/brotli v1.0.6 h1:Yf9fFpf49Zrxb9NlQaluyE92/+X7UVHlhMNJN2sxfOI= -github.com/andybalholm/brotli v1.0.6/go.mod h1:fO7iG3H7G2nSZ7m0zPUDn85XEX2GTukHGRSepvi9Eig= +github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M= +github.com/andybalholm/brotli v1.1.0/go.mod h1:sms7XGricyQI9K10gOSf56VKKWS4oLer58Q+mhRPtnY= github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ= github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw= github.com/bitly/go-hostpool v0.0.0-20171023180738-a3a6125de932 h1:mXoPYz/Ul5HYEDvkta6I8/rnYM5gSdSV2tJ6XbZuEtY= @@ -22,14 +22,12 @@ github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyT github.com/go-sql-driver/mysql v1.5.0 h1:ozyZYNQW3x3HtqT1jira07DN2PArx2v7/mN66gGcHOs= github.com/go-sql-driver/mysql v1.5.0/go.mod h1:DCzpHaOWr8IXmIStZouvnhqoel9Qv2LBy8hT2VhHyBg= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= -github.com/gofiber/fiber/v2 v2.51.0 h1:JNACcZy5e2tGApWB2QrRpenTWn0fq0hkFm6k0C86gKQ= -github.com/gofiber/fiber/v2 v2.51.0/go.mod h1:xaQRZQJGqnKOQnbQw+ltvku3/h8QxvNi8o6JiJ7Ll0U= -github.com/golang/snappy v0.0.1/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= +github.com/gofiber/fiber/v2 v2.52.4 h1:P+T+4iK7VaqUsq2PALYEfBBo6bJZ4q3FP8cZ84EggTM= +github.com/gofiber/fiber/v2 v2.52.4/go.mod h1:KEOE+cXMhXG0zHc9d8+E38hoX+ZN7bhOtgeF2oT6jrQ= github.com/golang/snappy v0.0.3/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM= github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q= github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= -github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= @@ -46,13 +44,12 @@ github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsI github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg= github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9 h1:L0QtFUgDarD7Fpv9jeVMgy/+Ec0mtnmYuImjTz6dtDA= github.com/jackc/pgservicefile v0.0.0-20231201235250-de7065d80cb9/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM= -github.com/jackc/pgx/v5 v5.5.1 h1:5I9etrGkLrN+2XPCsi6XLlV5DITbSL/xBZdmAxFcXPI= -github.com/jackc/pgx/v5 v5.5.1/go.mod h1:Ig06C2Vu0t5qXC60W8sqIthScaEnFvojjj9dSljmHRA= +github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw= +github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A= github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk= github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4= -github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk= -github.com/klauspost/compress v1.17.4 h1:Ej5ixsIri7BrIjBkRZLTo6ghwrEtHFk7ijlczPW4fZ4= -github.com/klauspost/compress v1.17.4/go.mod h1:/dCuZOvVtNoHsyb+cuJD3itjs3NbnF6KH9zAO4BDxPM= +github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU= +github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw= github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= @@ -72,11 +69,10 @@ github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZ github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w= github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY= github.com/mitchellh/mapstructure v1.5.0/go.mod h1:bFUtVrKA4DC2yAKiSyO/QUcy7e+RRV2QTWOzhPopBRo= -github.com/montanaflynn/stats v0.0.0-20171201202039-1bf9dbcd8cbe/go.mod h1:wL8QJuTMNUDYhXwkmfOly8iTdp5TEcJFWZD2D7SIkUc= github.com/montanaflynn/stats v0.7.1 h1:etflOAAHORrCC44V+aR6Ftzort912ZU+YLiSTuV8eaE= github.com/montanaflynn/stats v0.7.1/go.mod h1:etXPPgVO6n31NxCd9KQUMvCM+ve0ruNzt6R8Bnaayow= -github.com/pelletier/go-toml/v2 v2.1.1 h1:LWAJwfNvjQZCFIDKWYQaM62NcYeYViCmWIwmOStowAI= -github.com/pelletier/go-toml/v2 v2.1.1/go.mod h1:tJU2Z3ZkXwnxa4DPO899bsyIoywizdUvyaeZurnPPDc= +github.com/pelletier/go-toml/v2 v2.2.2 h1:aYUidT7k73Pcl9nb2gScu7NSrKCSHIDE89b3+6Wq+LM= +github.com/pelletier/go-toml/v2 v2.2.2/go.mod h1:1t835xjRzz80PqgE6HHgN2JOsmgYu/h4qDAS4n929Rs= github.com/philhofer/fwd v1.1.2 h1:bnDivRJ1EWPjUIRXV5KfORO897HTbpFAQddBdE8t7Gw= github.com/philhofer/fwd v1.1.2/go.mod h1:qkPdfjR2SIEbspLqpe1tO4n5yICnr2DY7mqEx2tUTP0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= @@ -84,15 +80,15 @@ github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZN github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 h1:Jamvg5psRIccs7FGNTlIRMkT8wgtp5eCXdBlqhYGL6U= github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc= -github.com/rivo/uniseg v0.4.4 h1:8TfxU8dW6PdqD27gjM8MVNuicgxIjxpm4K7x4jp8sis= -github.com/rivo/uniseg v0.4.4/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= +github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ= +github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rs/xid v1.3.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/xid v1.5.0/go.mod h1:trrq9SKmegXys3aeAKXMUTdJsYXVwGY3RLcfgqegfbg= github.com/rs/zerolog v1.25.0/go.mod h1:7KHcEGe0QZPOm2IE4Kpb5rTh6n1h2hIgS5OOnu1rUaI= -github.com/rs/zerolog v1.31.0 h1:FcTR3NnLWW+NnTwwhFWiJSZr4ECLpqCm6QsEnyvbV4A= -github.com/rs/zerolog v1.31.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= +github.com/rs/zerolog v1.32.0 h1:keLypqrlIjaFsbmJOBdB/qvyF8KEtCWHwobLp5l/mQ0= +github.com/rs/zerolog v1.32.0/go.mod h1:/7mN4D5sKwJLZQ2b/znpjC3/GQWY/xaDXUM0kKWRHss= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/rzajac/zltest v0.12.0 h1:9WPX0UhhXG66iuRT9+jYSl9SAGyl+PmKsC4+UGKLJVU= github.com/rzajac/zltest v0.12.0/go.mod h1:wZSsCw1RyFaEIfUOCUw8DiicX4U6yB1IZdOg8Uj0yDI= @@ -114,22 +110,22 @@ github.com/spf13/cobra v1.8.0 h1:7aJaZx1B85qltLMc546zn58BxxfZdR/W22ej9CFoEf0= github.com/spf13/cobra v1.8.0/go.mod h1:WXLWApfZ71AjXPya3WOlMsY9yMs7YeiHhFVlvLyhcho= github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= -github.com/spf13/viper v1.18.1 h1:rmuU42rScKWlhhJDyXZRKJQHXFX02chSVW1IvkPGiVM= -github.com/spf13/viper v1.18.1/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= +github.com/spf13/viper v1.18.2 h1:LUXCnvUvSM6FXAsj6nnfc8Q2tp1dIgUfY9Kc8GsSOiQ= +github.com/spf13/viper v1.18.2/go.mod h1:EKmWIqdnk5lOcmR72yw6hS+8OPYcwD0jteitLMVB+yk= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw= github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo= -github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0= -github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0= +github.com/stretchr/objx v0.5.2 h1:xuMeJ0Sdp5ZMRXx/aWO6RZxdr3beISkG5/G/aIRr3pY= +github.com/stretchr/objx v0.5.2/go.mod h1:FRsXN1f5AsAjCGJKqEizvkpNtU+EGNCLh3NxZ/8L+MA= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU= github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.2/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4= -github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk= github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= github.com/subosito/gotenv v1.6.0 h1:9NlTDc1FTs4qu0DDq7AEtTPNw6SVm7uBMsUCUjABIf8= github.com/subosito/gotenv v1.6.0/go.mod h1:Dk4QP5c2W3ibzajGcXpNraDfq2IrhjMIvMSWPKKo0FU= github.com/tanimutomo/sqlfile v1.0.0 h1:8Nnkd1ra7vBDb7yrv4zvvEdto0vO5Yuegv87C6Tcyp8= @@ -138,8 +134,8 @@ github.com/tinylib/msgp v1.1.9 h1:SHf3yoO2sGA0veCJeCBYLHuttAVFHGm2RHgNodW7wQU= github.com/tinylib/msgp v1.1.9/go.mod h1:BCXGB54lDD8qUEPmiG0cQQUANC4IUQyB2ItS2UDlO/k= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= -github.com/valyala/fasthttp v1.51.0 h1:8b30A5JlZ6C7AS81RsWjYMQmrZG6feChmgAolCl1SqA= -github.com/valyala/fasthttp v1.51.0/go.mod h1:oI2XroL+lI7vdXyYoQk03bXBThfFl2cVdIA3Xl7cH8g= +github.com/valyala/fasthttp v1.52.0 h1:wqBQpxH71XW0e2g+Og4dzQM8pk34aFYlA1Ga8db7gU0= +github.com/valyala/fasthttp v1.52.0/go.mod h1:hf5C4QnVMkNXMspnsUlfM3WitlgYflyhHYoKol/szxQ= github.com/valyala/tcplisten v1.0.0 h1:rBHj/Xf+E1tRGZyWIWwJDiRY0zc1Js+CV5DqwacVSA8= github.com/valyala/tcplisten v1.0.0/go.mod h1:T0xQ8SeCZGxckz9qRXTfG43PvQ/mcWh7FwZEA7Ioqkc= github.com/xdg-go/pbkdf2 v1.0.0 h1:Su7DPu48wXMwC3bs7MCNG+z4FhcyEuz5dlvchbq0B0c= @@ -148,31 +144,27 @@ github.com/xdg-go/scram v1.1.2 h1:FHX5I5B4i4hKRVRBCFRxq1iQRej7WO3hhBuJf+UUySY= github.com/xdg-go/scram v1.1.2/go.mod h1:RT/sEzTbU5y00aCK8UOx6R7YryM0iF1N2MOmC3kKLN4= github.com/xdg-go/stringprep v1.0.4 h1:XLI/Ng3O1Atzq0oBs3TWm+5ZVgkq2aqdlvP9JtoZ6c8= github.com/xdg-go/stringprep v1.0.4/go.mod h1:mPGuuIYwz7CmR2bT9j4GbQqutWS1zV24gijq1dTyGkM= -github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d/go.mod h1:rHwXgn7JulP+udvsHwJoVG1YGAP6VLg4y9I5dyZdqmA= -github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a h1:fZHgsYlfvtyqToslyjUt3VOPF4J7aK/3MPcK7xp3PDk= -github.com/youmark/pkcs8 v0.0.0-20201027041543-1326539a0a0a/go.mod h1:ul22v+Nro/R083muKhosV54bj5niojjWZvU8xrevuH4= +github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76 h1:tBiBTKHnIjovYoLX/TPkcf+OjqqKGQrPtGT3Foz+Pgo= +github.com/youmark/pkcs8 v0.0.0-20240424034433-3c2c7870ae76/go.mod h1:SQliXeA7Dhkt//vS29v3zpbEwoa+zb2Cn5xj5uO4K5U= github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= -go.mongodb.org/mongo-driver v1.13.1 h1:YIc7HTYsKndGK4RFzJ3covLz1byri52x0IoMB0Pt/vk= -go.mongodb.org/mongo-driver v1.13.1/go.mod h1:wcDf1JBCXy2mOW0bWHwO/IOYqdca1MPCwDtFu/Z9+eo= +go.mongodb.org/mongo-driver v1.15.0 h1:rJCKC8eEliewXjZGf0ddURtl7tTVy1TK3bfl0gkUSLc= +go.mongodb.org/mongo-driver v1.15.0/go.mod h1:Vzb0Mk/pa7e6cWw85R4F/endUC3u0U9jGcNU603k65c= go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0= go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y= 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-20200302210943-78000ba7a073/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= -golang.org/x/crypto v0.0.0-20220622213112-05595931fe9d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= golang.org/x/crypto v0.23.0 h1:dIJU/v2J8Mdglj/8rJ6UUOM3Zc9zLZxVZwwxMooUSAI= golang.org/x/crypto v0.23.0/go.mod h1:CKFgDieR+mRhux2Lsu27y0fO304Db0wZe70UKqHu0v8= -golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb h1:c0vyKkb6yr3KR7jEfJaOSv4lG7xPkbN6r52aJz1d8a8= -golang.org/x/exp v0.0.0-20231206192017-f3f8817b8deb/go.mod h1:iRJReGqOEeBhDZGkGbynYwcHlctCvnjTYIamk7uXpHI= +golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842 h1:vr/HnozRka3pE4EsMEg1lgkXJkTFJCVUX+S/ZT6wYzM= +golang.org/x/exp v0.0.0-20240506185415-9bf2ced13842/go.mod h1:XtvwrStGgqGPLc4cjQfWqZHG1YFdYs6swckp8vpsjnc= golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= 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-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= golang.org/x/net v0.0.0-20220526153639-5463443f8c37/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.25.0 h1:d/OCCoBEUq33pjydKrGQhw7IlUPI2Oylr+8qLx49kac= @@ -180,13 +172,12 @@ golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= -golang.org/x/sync v0.6.0 h1:5BMeUDZ7vkXGfEr1x9B4bRcTH4lpkTkpdh0T/J+qjbQ= -golang.org/x/sync v0.6.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= 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-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= @@ -200,10 +191,8 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.3.8/go.mod h1:E6s5w1FMmriuDzIBO73fBruAKo1PCIq6d2Q6DHfQ8WQ= -golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.15.0 h1:h1V/4gjBv8v9cjcR6+AR5+/cIYK5N/WAgiv4xlsEtAk= golang.org/x/text v0.15.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= From 7fa89606112492768dd7f30e38eb7ab20ec06ca6 Mon Sep 17 00:00:00 2001 From: Dusan Malusev Date: Fri, 10 May 2024 18:36:09 +0200 Subject: [PATCH 5/8] update github actions Signed-off-by: Dusan Malusev --- .github/workflows/lint.yml | 16 ++++++++++--- .github/workflows/release.yml | 44 +++++++++++++++++++++++++++++++++++ .github/workflows/sec.yml | 13 +++++++++++ .github/workflows/test.yml | 24 ++++++++----------- go.mod | 2 -- 5 files changed, 80 insertions(+), 19 deletions(-) create mode 100644 .github/workflows/release.yml create mode 100644 .github/workflows/sec.yml diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml index e2c6286..107933f 100644 --- a/.github/workflows/lint.yml +++ b/.github/workflows/lint.yml @@ -1,10 +1,13 @@ +name: 'GolangCI Lint' + on: - pull_request: + push: branches: - master - push: + pull_request: branches: - master + jobs: lint: runs-on: ubuntu-latest @@ -12,7 +15,14 @@ jobs: - uses: actions/checkout@v4 with: fetch-depth: 1 + - uses: actions/cache@v4 + with: + path: ~/go/pkg/mod + key: ${{ runner.os }}-go-golangci-${{ hashFiles('**/go.sum') }} + restore-keys: | + ${{ runner.os }}-go-golangci-${{ hashFiles('**/go.sum') }} - name: golangci-lint uses: reviewdog/action-golangci-lint@v2 with: - golangci_lint_flags: "--tests=false" + golangci_lint_flags: "--config=.golangci.yml" + cache: true \ No newline at end of file diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml new file mode 100644 index 0000000..6b48973 --- /dev/null +++ b/.github/workflows/release.yml @@ -0,0 +1,44 @@ +name: 'Create Release' + +on: + push: + tags: + - 'v*' + +jobs: + create_release: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 1 + - name: Get Tag + if: startsWith(github.ref, 'refs/tags/v') + uses: olegtarasov/get-tag@v2.1.3 + id: version_tag + with: + tagRegex: "v(.*)" + - name: Upload Release Asset + uses: softprops/action-gh-release@v1 + id: release + with: + name: "v${{ steps.version_tag.outputs.tag }}" + tag_name: "v${{ steps.version_tag.outputs.tag }}" + generate_release_notes: true + append_body: true + prerelease: false + fail_on_unmatched_files: true + - name: "Generate release changelog" + uses: heinrichreimer/action-github-changelog-generator@v2.3 + with: + token: ${{ secrets.GITHUB_TOKEN }} + author: true + releaseUrl: ${{ steps.release.outputs.url }} + issues: false + pullRequests: true + - uses: stefanzweifel/git-auto-commit-action@v5 + with: + commit_message: "Update CHANGELOG.md" + branch: master + commit_options: '--no-verify --signoff' + file_pattern: CHANGELOG.md \ No newline at end of file diff --git a/.github/workflows/sec.yml b/.github/workflows/sec.yml new file mode 100644 index 0000000..f1a9deb --- /dev/null +++ b/.github/workflows/sec.yml @@ -0,0 +1,13 @@ +on: [push, pull_request] +name: Security +jobs: + Gosec: + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 1 + - name: Run Gosec + uses: securego/gosec@master + with: + args: ./... \ No newline at end of file diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9a39115..8dc908f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -1,4 +1,4 @@ -name: 'Testing' +name: "Testing" on: push: @@ -7,23 +7,22 @@ on: pull_request: branches: - master + jobs: - test: + unit-tests: strategy: matrix: - os: [ubuntu-latest] - go: ['1.21'] - runs-on: ${{ matrix.os }} - container: golang:${{ matrix.go }}-bullseye + go: ["1.22"] + runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 with: fetch-depth: 1 - - uses: actions/setup-go@v4 + - uses: actions/setup-go@v5 with: go-version: ${{ matrix.go }} - name: Install Task - uses: arduino/setup-task@v1 + uses: arduino/setup-task@v2 with: version: 3.x repo-token: ${{ secrets.GITHUB_TOKEN }} @@ -31,11 +30,8 @@ jobs: run: | go install github.com/gotesttools/gotestfmt/v2/cmd/gotestfmt@latest task test - - uses: codecov/codecov-action@v3 + - name: Upload coverage reports to Codecov + uses: codecov/codecov-action@v4 with: token: ${{ secrets.CODECOV_TOKEN }} - file: ./coverage.txt - flags: unit-tests - name: codecov-umbrella - fail_ci_if_error: false - verbose: false \ No newline at end of file + slug: nano-interactive/go-utils \ No newline at end of file diff --git a/go.mod b/go.mod index 6bd6410..a17a200 100644 --- a/go.mod +++ b/go.mod @@ -2,8 +2,6 @@ module github.com/nano-interactive/go-utils/v2 go 1.22 -toolchain go1.22.2 - require ( github.com/gocql/gocql v1.6.0 github.com/gofiber/fiber/v2 v2.52.4 From 7498035e79aa52be8c8d09640a055551db354986 Mon Sep 17 00:00:00 2001 From: Dusan Malusev Date: Fri, 10 May 2024 18:38:07 +0200 Subject: [PATCH 6/8] Adding golangci lint Signed-off-by: Dusan Malusev --- .golangci.yml | 100 +++++++++++++++++++++++++++++++++++++ validators/mongoid_test.go | 4 +- 2 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 .golangci.yml diff --git a/.golangci.yml b/.golangci.yml new file mode 100644 index 0000000..2e0e877 --- /dev/null +++ b/.golangci.yml @@ -0,0 +1,100 @@ +run: + concurrency: 16 + timeout: 5m + issues-exit-code: 1 + tests: true + go: '1.22' + modules-download-mode: mod + allow-parallel-runners: true + +issues: + exclude-rules: + - path: _test\.go + linters: + - gocyclo + - errcheck + - dupl + - gosec + exclude-files: + - .*_gen\.go$ + - .*setup\.go$ + +output: + print-issued-lines: true + print-linter-name: true + uniq-by-line: true + sort-results: true + +linters: + disable-all: true + enable: + - asasalint + - bidichk + - bodyclose + - decorder + - dupl + - durationcheck + - errcheck + - errchkjson + - errname + - errorlint + - exportloopref + - forbidigo + - gofumpt + - ginkgolinter + - gocheckcompilerdirectives + - gocognit + - goconst + - gocritic + - gocyclo + - godox + - goerr113 + - goheader + - goimports + - gomodguard + - goprintffuncname + - gosec + - gosimple + - gosmopolitan + - govet + - grouper + - importas + - ineffassign + - interfacebloat + - loggercheck + - maintidx + - makezero + - mirror + - misspell + - nakedret + - nestif + - nilerr + - nilnil + - noctx + - nolintlint + - nonamedreturns + - nosprintfhostport + - paralleltest + - prealloc + - predeclared + - promlinter + - reassign + - revive + - rowserrcheck + - sqlclosecheck + - staticcheck + - stylecheck + - tenv + - testableexamples + - testpackage + - thelper + - tparallel + - unconvert + - unparam + - unused + - usestdlibvars + - wastedassign + - whitespace + - zerologlint + - prealloc + - perfsprint \ No newline at end of file diff --git a/validators/mongoid_test.go b/validators/mongoid_test.go index 7239f6a..1013a11 100644 --- a/validators/mongoid_test.go +++ b/validators/mongoid_test.go @@ -42,10 +42,10 @@ func TestIsObjectId(t *testing.T) { func BenchmarkIsObjectId(b *testing.B) { b.Run("Good", func(b *testing.B) { - objectId := "652e77eeadd7d603e4420c3d" + objectID := "652e77eeadd7d603e4420c3d" for i := 0; i < b.N; i++ { - _ = NewObjectIDRule().Validate(objectId) + _ = NewObjectIDRule().Validate(objectID) } }) From 48c34143f811bedfb256af58bb225691e0972da6 Mon Sep 17 00:00:00 2001 From: Dusan Malusev Date: Fri, 10 May 2024 18:42:52 +0200 Subject: [PATCH 7/8] lints Signed-off-by: Dusan Malusev --- grpc/resolvers/dns_resolver.go | 39 ++++++++++++++++------------- grpc/resolvers/dns_resolver_test.go | 10 ++++---- 2 files changed, 26 insertions(+), 23 deletions(-) diff --git a/grpc/resolvers/dns_resolver.go b/grpc/resolvers/dns_resolver.go index 91b60a6..b34af5e 100644 --- a/grpc/resolvers/dns_resolver.go +++ b/grpc/resolvers/dns_resolver.go @@ -16,7 +16,7 @@ import ( ) type ( - DnsResolver struct { + DNSResolver struct { cc resolver.ClientConn resolver NetResolver resolveNow chan struct{} @@ -26,7 +26,7 @@ type ( mu sync.RWMutex } - DnsResolverBuilder struct { + DNSResolverBuilder struct { ctx context.Context options *option } @@ -54,7 +54,7 @@ func WithResolver(r NetResolver) Option { } } -func WithDnsReResolving(d time.Duration) Option { +func WithDNSReResolving(d time.Duration) Option { return func(o *option) { o.dnsReResolving = d } @@ -78,7 +78,7 @@ func WithDefaultPort(p uint16) Option { } } -func NewDnsResolverBuilder(ctx context.Context, opts ...Option) *DnsResolverBuilder { +func NewDNSResolverBuilder(ctx context.Context, opts ...Option) *DNSResolverBuilder { cfg := &option{ dnsReResolving: 30 * time.Second, defaultPort: 443, @@ -89,13 +89,13 @@ func NewDnsResolverBuilder(ctx context.Context, opts ...Option) *DnsResolverBuil opt(cfg) } - return &DnsResolverBuilder{ + return &DNSResolverBuilder{ ctx: ctx, options: cfg, } } -func (d *DnsResolverBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) { +func (d *DNSResolverBuilder) Build(target resolver.Target, cc resolver.ClientConn, opts resolver.BuildOptions) (resolver.Resolver, error) { ctx, cancel := context.WithCancel(d.ctx) endpoint := target.Endpoint() @@ -132,7 +132,7 @@ func (d *DnsResolverBuilder) Build(target resolver.Target, cc resolver.ClientCon } } - resolver := &DnsResolver{ + resolver := &DNSResolver{ ips: make([]resolver.Address, 0), resolveNow: make(chan struct{}, 1), cancel: cancel, @@ -154,11 +154,11 @@ func (d *DnsResolverBuilder) Build(target resolver.Target, cc resolver.ClientCon return resolver, nil } -func (b *DnsResolverBuilder) Scheme() string { +func (d *DNSResolverBuilder) Scheme() string { return "nanodns" } -func (d *DnsResolver) resolve(ctx context.Context, host, port string, timeout time.Duration) error { +func (d *DNSResolver) resolve(ctx context.Context, host, port string, timeout time.Duration) error { ips, err := resolve(ctx, d.resolver, host, port, timeout) if err != nil { if errors.Is(err, errNoChange) { @@ -199,7 +199,7 @@ func (d *DnsResolver) resolve(ctx context.Context, host, port string, timeout ti return nil } -func (d *DnsResolver) watcher(ctx context.Context, host, port string, refresh, timeout time.Duration) { +func (d *DNSResolver) watcher(ctx context.Context, host, port string, refresh, timeout time.Duration) { ticker := time.NewTicker(refresh) reResolveTimeout := time.NewTicker(refresh) @@ -277,20 +277,20 @@ func resolve(ctx context.Context, r NetResolver, host, port string, timeout time return state, nil } -func (d *DnsResolver) ResolveNow(resolver.ResolveNowOptions) { +func (d *DNSResolver) ResolveNow(resolver.ResolveNowOptions) { select { case d.resolveNow <- struct{}{}: default: } } -func (d *DnsResolver) Close() { +func (d *DNSResolver) Close() { d.cancel() d.wg.Wait() close(d.resolveNow) } -func formatIP(addr string) (addrIP string, ok bool) { +func formatIP(addr string) (string, bool) { ip := net.ParseIP(addr) if ip == nil { return "", false @@ -306,11 +306,11 @@ var ( errNoChange = errors.New("no ip changes") ) -func parseTarget(target, defaultPort string) (host, port string, err error) { +func parseTarget(target, defaultPort string) (string, string, error) { if ip := net.ParseIP(target); ip != nil { return target, defaultPort, nil } - if host, port, err = net.SplitHostPort(target); err == nil { + if host, port, err := net.SplitHostPort(target); err == nil { if port == "" { return "", "", ErrEndsWithColon } @@ -319,8 +319,11 @@ func parseTarget(target, defaultPort string) (host, port string, err error) { } return host, port, nil } - if host, port, err = net.SplitHostPort(target + ":" + defaultPort); err == nil { - return host, port, nil + + host, port, err := net.SplitHostPort(target + ":" + defaultPort) + if err != nil { + return host, port, fmt.Errorf("invalid target address %v, error info: %w", target, err) } - return "", "", fmt.Errorf("invalid target address %v, error info: %v", target, err) + + return host, port, nil } diff --git a/grpc/resolvers/dns_resolver_test.go b/grpc/resolvers/dns_resolver_test.go index ae55451..17e665d 100644 --- a/grpc/resolvers/dns_resolver_test.go +++ b/grpc/resolvers/dns_resolver_test.go @@ -35,11 +35,11 @@ func (m *MockClientConn) ReportError(p0 error) { _ = m.Called(p0) } -func (m *MockClientConn) NewAddress(addresses []resolver.Address) { +func (m *MockClientConn) NewAddress(_ []resolver.Address) { panic("SHOULD PANIC: NOT EXPECTED ANYWHERE") } -func (m *MockClientConn) ParseServiceConfig(serviceConfigJSON string) *serviceconfig.ParseResult { +func (m *MockClientConn) ParseServiceConfig(_ string) *serviceconfig.ParseResult { panic("SHOULD PANIC: NOT EXPECTED ANYWHERE") } @@ -61,10 +61,10 @@ func (m *MockNetResolver) LookupSRV(p0 context.Context, p1 string, p2 string, p3 return args.String(0), args.Get(1).([]*net.SRV), args.Error(2) } -func getDNSBuilder(resolver resolvers.NetResolver, resolving time.Duration) *resolvers.DnsResolverBuilder { - return resolvers.NewDnsResolverBuilder( +func getDNSBuilder(resolver resolvers.NetResolver, resolving time.Duration) *resolvers.DNSResolverBuilder { + return resolvers.NewDNSResolverBuilder( context.Background(), - resolvers.WithDnsReResolving(resolving), + resolvers.WithDNSReResolving(resolving), resolvers.WithResolvingTimeout(3*time.Second), resolvers.WithDefaultPort(3000), resolvers.WithResolver(resolver), From 67be99a76ba99c639ad7d122006eb23c58b2f454 Mon Sep 17 00:00:00 2001 From: Dusan Malusev Date: Fri, 10 May 2024 18:44:22 +0200 Subject: [PATCH 8/8] lints Signed-off-by: Dusan Malusev --- url.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/url.go b/url.go index f48265e..f2ab049 100644 --- a/url.go +++ b/url.go @@ -37,7 +37,7 @@ func TrimUrlForScylla(fullUrl string) (scyllaUrl string, hostName string, err er trimmedUrl = trimmedUrl + "/" } - if strings.Index(trimmedUrl, "http") == 0 { + if strings.Index(trimmedUrl, "http") != 0 { trimmedUrl = "https://" + trimmedUrl } else { trimmedUrl = strings.Replace(trimmedUrl, "http://", "https://", 1)