Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/actions/setup/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ runs:
if: ${{ inputs.language == 'go' }}
shell: bash
run: |
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.60.3
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/HEAD/install.sh | sh -s -- -b $(go env GOPATH)/bin v2.5.0
go install golang.org/x/tools/cmd/[email protected]
echo "$(go env GOPATH)/bin" >> $GITHUB_PATH
- name: Cache golangci-lint analysis
if: ${{ inputs.language == 'go' }}
Expand Down
109 changes: 49 additions & 60 deletions clients/algoliasearch-client-go/.golangci.yml
Original file line number Diff line number Diff line change
@@ -1,71 +1,60 @@
linters-settings:
govet:
enable-all: true
disable:
- fieldalignment

revive:
rules:
- name: var-naming
disabled: true

version: "2"
run:
concurrency: 2
linters:
enable-all: true

default: all
disable:
- godox
- bodyclose
- contextcheck
- interfacebloat
- gci
- gosmopolitan
- wsl
- varnamelen
- nlreturn
- canonicalheader
- containedctx
- copyloopvar
- cyclop
- depguard
- dupl
- dupword
- err113
- gochecknoglobals
- exhaustruct
- exhaustive
- depguard
- lll
- exhaustruct
- forbidigo
- gochecknoinits
- cyclop
- errorlint
- gomnd
- tagliatelle
- nilnil
- stylecheck
- musttag
- errchkjson
- nonamedreturns
- inamedparam
- ineffassign
- dupword
- nestif
- goconst
- funlen
- dupl
- unparam
- gochecknoglobals
- gocognit
- forcetypeassert
- wastedassign
- goconst
- gocyclo
- maintidx
- copyloopvar
- godoclint
- inamedparam
- intrange
- canonicalheader
- lll
- maintidx
- mnd
- nestif
- nilnil
- nonamedreturns
- perfsprint
- containedctx

# Deprecated
- execinquery
- exportloopref

issues:
exclude-generated: disable

run:
concurrency: 2
timeout: 10m
- recvcheck
- tagliatelle
- varnamelen
- wsl
settings:
govet:
enable-all: true
disable:
- fieldalignment
revive:
rules:
- name: var-naming
disabled: true
staticcheck:
checks: ["all", "-ST1005", "-ST1016"]
exclusions:
generated: disable
presets:
- comments
- std-error-handling
formatters:
enable:
- gofmt
- gofumpt
- golines
settings:
golines:
max-len: 150
5 changes: 5 additions & 0 deletions clients/algoliasearch-client-go/algolia/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ func Display(input any) {
if !debug {
return
}

start := time.Now()

var msg string

switch v := input.(type) {
case *http.Request:
msg = debugRequest(v)
Expand All @@ -40,6 +43,7 @@ func Display(input any) {
default:
msg = fmt.Sprintf("do not know how to display %#v", v)
}

Println(msg)
fmt.Printf("took %s\n", time.Since(start))
}
Expand All @@ -52,6 +56,7 @@ func Printf(format string, a ...any) {
if !debug {
return
}

msg := fmt.Sprintf(format, a...)
fmt.Printf("> ALGOLIA DEBUG: %s", msg)
}
10 changes: 10 additions & 0 deletions clients/algoliasearch-client-go/algolia/debug/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,14 @@ func copyReadCloser(r io.ReadCloser) (io.ReadCloser, string) {
if r == nil {
return nil, ""
}

data, err := io.ReadAll(r)
_ = r.Close()

if err != nil {
return nil, ""
}

return io.NopCloser(bytes.NewReader(data)), string(data)
}

Expand All @@ -29,10 +32,12 @@ func decodeGzipContent(in string) (string, error) {
if err != nil {
return in, fmt.Errorf("cannot open content with gzip.Reader: %w", err)
}

out, err := io.ReadAll(gr)
if err != nil {
return in, fmt.Errorf("cannot read content from gzip.Reader: %w", err)
}

return string(out), nil
}

Expand All @@ -58,10 +63,12 @@ func extractBody(body io.ReadCloser, c compression.Compression) (io.ReadCloser,

func prettyPrintJSON(input string) string {
var b bytes.Buffer

err := json.Indent(&b, []byte(input), "\t", " ")
if err != nil {
return input
}

return strings.TrimSuffix(b.String(), "\n")
}

Expand All @@ -88,8 +95,10 @@ func debugRequest(req *http.Request) string {
if strings.Contains(strings.ToLower(k), "algolia") {
str = strings.Repeat("*", len(str))
}

msg += fmt.Sprintf("\theader=%s:%q\n", k, str)
}

msg += fmt.Sprintf("\tbody=\n\t%s\n", prettyPrintJSON(body))

return msg
Expand All @@ -101,6 +110,7 @@ func debugResponse(res *http.Response) string {
}

var body string

res.Body, body = extractBody(res.Body, compression.NONE)

msg := "> ALGOLIA DEBUG response:\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func (e *NoMoreHostToTryError) Error() string {
if len(e.intermediateNetworkErrors) > 0 {
return fmt.Errorf("%s %w", baseErr, errors.Join(e.intermediateNetworkErrors...)).Error()
}

return fmt.Sprintf("%s You can use 'ExposeIntermediateNetworkErrors: true' in the config to investigate.", baseErr)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

type Configuration struct {
AppID string
ApiKey string
ApiKey string //nolint:staticcheck

Hosts []StatefulHost
DefaultHeader map[string]string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package transport

import (
"context"
"errors"
"net"
"strings"
"sync"
Expand All @@ -13,14 +14,16 @@ import (
type Outcome int

const (
DefaultReadTimeout = 5 * time.Second
DefaultWriteTimeout = 30 * time.Second

Success Outcome = iota
Failure
Retry
)

const (
DefaultReadTimeout = 5 * time.Second
DefaultWriteTimeout = 30 * time.Second
)

type Host struct {
scheme string
host string
Expand All @@ -29,6 +32,7 @@ type Host struct {

type RetryStrategy struct {
sync.RWMutex

hosts []StatefulHost
writeTimeout time.Duration
readTimeout time.Duration
Expand Down Expand Up @@ -99,16 +103,19 @@ func (s *RetryStrategy) Decide(h Host, code int, err error) Outcome {

if err == nil && is2xx(code) {
s.markUp(h)

return Success
}

if isTimeoutError(err) {
s.markTimeout(h)

return Retry
}

if !(isZero(code) || is4xx(code) || is2xx(code)) || isNetworkError(err) {
if (!isZero(code) && !is4xx(code) && !is2xx(code)) || isNetworkError(err) {
s.markDown(h)

return Retry
}

Expand All @@ -119,6 +126,7 @@ func (s *RetryStrategy) markUp(host Host) {
for _, h := range s.hosts {
if h.host == host.host {
h.markUp()

return
}
}
Expand All @@ -128,6 +136,7 @@ func (s *RetryStrategy) markTimeout(host Host) {
for _, h := range s.hosts {
if h.host == host.host {
h.markTimeout()

return
}
}
Expand All @@ -137,6 +146,7 @@ func (s *RetryStrategy) markDown(host Host) {
for _, h := range s.hosts {
if h.host == host.host {
h.markDown()

return
}
}
Expand All @@ -146,7 +156,10 @@ func isNetworkError(err error) bool {
if err == nil {
return false
}
_, ok := err.(net.Error)

var netError net.Error

ok := errors.As(err, &netError)
// We need to ensure that the error is a net.Error but not a
// context.DeadlineExceeded error (which is actually a net.Error), because
// we do not want to consider context.DeadlineExceeded as an error.
Expand All @@ -157,6 +170,7 @@ func isTimeoutError(err error) bool {
if err == nil {
return false
}

return strings.Contains(err.Error(), context.DeadlineExceeded.Error())
}

Expand Down
Loading