Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat support retry pattern on status code 502, 503, 504 #581

Merged
merged 8 commits into from
Oct 2, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@
## Table of Contents <!-- omit in TOC -->

- [📖 Documentation](#-documentation)
- [ Supercharge your Meilisearch experience](#-supercharge-your-meilisearch-experience)
- [💫 Supercharge your Meilisearch experience](#-supercharge-your-meilisearch-experience)
- [🔧 Installation](#-installation)
- [🚀 Getting started](#-getting-started)
- [🤖 Compatibility with Meilisearch](#-compatibility-with-meilisearch)
- [⚡️ Benchmark Performance](#-benchmark-performance)
- [💡 Learn more](#-learn-more)
- [⚙️ Contributing](#️-contributing)

Expand Down Expand Up @@ -238,6 +239,25 @@ searchRes, err := index.Search("wonder",

This package guarantees compatibility with [version v1.x of Meilisearch](https://github.com/meilisearch/meilisearch/releases/latest), but some features may not be present. Please check the [issues](https://github.com/meilisearch/meilisearch-go/issues?q=is%3Aissue+is%3Aopen+label%3A%22good+first+issue%22+label%3Aenhancement) for more info.

## ⚡️ Benchmark Performance

The Meilisearch client performance was tested in [client_bench_test.go](/client_bench_test.go).

```shell
goos: linux
goarch: amd64
pkg: github.com/meilisearch/meilisearch-go
cpu: AMD Ryzen 7 5700U with Radeon Graphics
```

**Results**

```shell
Benchmark_ExecuteRequest-16 10000 105880 ns/op 7241 B/op 87 allocs/op
Benchmark_ExecuteRequestWithEncoding-16 2716 455548 ns/op 1041998 B/op 169 allocs/op
Benchmark_ExecuteRequestWithoutRetries-16 1 3002787257 ns/op 56528 B/op 332 allocs/op
```

## 💡 Learn more

The following sections in our main documentation website may interest you:
Expand Down
97 changes: 88 additions & 9 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"net/url"
"sync"
"time"
)

type client struct {
Expand All @@ -19,6 +20,18 @@ type client struct {
bufferPool *sync.Pool
encoder encoder
contentEncoding ContentEncoding
retryOnStatus map[int]bool
disableRetry bool
maxRetries uint8
retryBackoff func(attempt uint8) time.Duration
}

type clientConfig struct {
contentEncoding ContentEncoding
encodingCompressionLevel EncodingCompressionLevel
retryOnStatus map[int]bool
disableRetry bool
maxRetries uint8
}

type internalRequest struct {
Expand All @@ -34,7 +47,7 @@ type internalRequest struct {
functionName string
}

func newClient(cli *http.Client, host, apiKey string, ce ContentEncoding, cl EncodingCompressionLevel) *client {
func newClient(cli *http.Client, host, apiKey string, cfg clientConfig) *client {
c := &client{
client: cli,
host: host,
Expand All @@ -44,11 +57,28 @@ func newClient(cli *http.Client, host, apiKey string, ce ContentEncoding, cl Enc
return new(bytes.Buffer)
},
},
disableRetry: cfg.disableRetry,
maxRetries: cfg.maxRetries,
retryOnStatus: cfg.retryOnStatus,
}

if !ce.IsZero() {
c.contentEncoding = ce
c.encoder = newEncoding(ce, cl)
if c.retryOnStatus == nil {
c.retryOnStatus = map[int]bool{
502: true,
503: true,
504: true,
}
}

if !c.disableRetry && c.retryBackoff == nil {
c.retryBackoff = func(attempt uint8) time.Duration {
return time.Second * time.Duration(attempt)
}
}

if !cfg.contentEncoding.IsZero() {
c.contentEncoding = cfg.contentEncoding
c.encoder = newEncoding(cfg.contentEncoding, cfg.encodingCompressionLevel)
}

return c
Expand Down Expand Up @@ -197,12 +227,9 @@ func (c *client) sendRequest(

request.Header.Set("User-Agent", GetQualifiedVersion())

resp, err := c.client.Do(request)
resp, err := c.do(request, internalError)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return nil, internalError.WithErrCode(MeilisearchTimeoutError, err)
}
return nil, internalError.WithErrCode(MeilisearchCommunicationError, err)
return nil, err
}

if body != nil {
Expand All @@ -213,6 +240,58 @@ func (c *client) sendRequest(
return resp, nil
}

func (c *client) do(req *http.Request, internalError *Error) (resp *http.Response, err error) {
retriesCount := uint8(0)

for {
resp, err = c.client.Do(req)
if err != nil {
if errors.Is(err, context.DeadlineExceeded) {
return nil, internalError.WithErrCode(MeilisearchTimeoutError, err)
}
return nil, internalError.WithErrCode(MeilisearchCommunicationError, err)
}

// Exit if retries are disabled
if c.disableRetry {
break
}

// Check if response status is retryable and we haven't exceeded max retries
if c.retryOnStatus[resp.StatusCode] && retriesCount < c.maxRetries {
retriesCount++

// Close response body to prevent memory leaks
resp.Body.Close()

// Handle backoff with context cancellation support
backoff := c.retryBackoff(retriesCount)
timer := time.NewTimer(backoff)

select {
case <-req.Context().Done():
err := req.Context().Err()
timer.Stop()
return nil, internalError.WithErrCode(MeilisearchTimeoutError, err)
case <-timer.C:
// Retry after backoff
timer.Stop()
}

continue
}

break
}

// Return error if retries exceeded the maximum limit
if !c.disableRetry && retriesCount >= c.maxRetries {
return nil, internalError.WithErrCode(MeilisearchMaxRetriesExceeded, nil)
}

return resp, nil
}

func (c *client) handleStatusCode(req *internalRequest, statusCode int, body []byte, internalError *Error) error {
if req.acceptedStatusCodes != nil {

Expand Down
146 changes: 146 additions & 0 deletions client_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package meilisearch

import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"testing"
)

func Benchmark_ExecuteRequest(b *testing.B) {
b.ReportAllocs()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && r.URL.Path == "/test" {
w.WriteHeader(http.StatusOK)
_, _ = w.Write([]byte(`{"message":"get successful"}`))
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()

c := newClient(&http.Client{}, ts.URL, "testApiKey", clientConfig{
disableRetry: true,
})

b.ResetTimer()
for i := 0; i < b.N; i++ {
err := c.executeRequest(context.Background(), &internalRequest{
endpoint: "/test",
method: http.MethodGet,
withResponse: &mockResponse{},
acceptedStatusCodes: []int{http.StatusOK},
})
if err != nil {
b.Fatal(err)
}
}
}

func Benchmark_ExecuteRequestWithEncoding(b *testing.B) {
b.ReportAllocs()
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPost && r.URL.Path == "/test" {
accept := r.Header.Get("Accept-Encoding")
ce := r.Header.Get("Content-Encoding")

reqEnc := newEncoding(ContentEncoding(ce), DefaultCompression)
respEnc := newEncoding(ContentEncoding(accept), DefaultCompression)
req := new(mockData)

if len(ce) != 0 {
body, err := io.ReadAll(r.Body)
if err != nil {
b.Fatal(err)
}

err = reqEnc.Decode(body, req)
if err != nil {
b.Fatal(err)
}
}

if len(accept) != 0 {
d, err := json.Marshal(req)
if err != nil {
b.Fatal(err)
}
res, err := respEnc.Encode(bytes.NewReader(d))
if err != nil {
b.Fatal(err)
}
_, _ = w.Write(res.Bytes())
w.WriteHeader(http.StatusOK)
}
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()

c := newClient(&http.Client{}, ts.URL, "testApiKey", clientConfig{
disableRetry: true,
contentEncoding: GzipEncoding,
encodingCompressionLevel: DefaultCompression,
})

b.ResetTimer()
for i := 0; i < b.N; i++ {
err := c.executeRequest(context.Background(), &internalRequest{
endpoint: "/test",
method: http.MethodPost,
contentType: contentTypeJSON,
withRequest: &mockData{Name: "foo", Age: 30},
withResponse: &mockData{},
acceptedStatusCodes: []int{http.StatusOK},
})
if err != nil {
b.Fatal(err)
}
}
}

func Benchmark_ExecuteRequestWithoutRetries(b *testing.B) {
b.ReportAllocs()
retryCount := 0
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodGet && r.URL.Path == "/test" {
if retryCount == 2 {
w.WriteHeader(http.StatusOK)
return
}
w.WriteHeader(http.StatusBadGateway)
retryCount++
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()

c := newClient(&http.Client{}, ts.URL, "testApiKey", clientConfig{
disableRetry: false,
maxRetries: 3,
retryOnStatus: map[int]bool{
502: true,
503: true,
504: true,
},
})

b.ResetTimer()
for i := 0; i < b.N; i++ {
err := c.executeRequest(context.Background(), &internalRequest{
endpoint: "/test",
method: http.MethodGet,
withResponse: nil,
withRequest: nil,
acceptedStatusCodes: []int{http.StatusOK},
})
if err != nil {
b.Fatal(err)
}
}
}
Loading
Loading