Skip to content

Commit

Permalink
Improvement: reuse RPC connection
Browse files Browse the repository at this point in the history
  • Loading branch information
aopoltorzhicky committed Aug 18, 2023
1 parent fb7a6f9 commit b2736a5
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
6 changes: 3 additions & 3 deletions pkg/rpc/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ type API struct {
// NewAPI - constructor of API
func NewAPI(baseURL string, opts ...ApiOption) API {
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = 100
t.MaxConnsPerHost = 100
t.MaxIdleConnsPerHost = 100
t.MaxIdleConns = 10
t.MaxConnsPerHost = 10
t.MaxIdleConnsPerHost = 10

client := &http.Client{
Transport: t,
Expand Down
8 changes: 7 additions & 1 deletion pkg/rpc/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package api
import (
"bytes"
"context"
"io"
"net/http"

"github.com/goccy/go-json"
Expand Down Expand Up @@ -33,11 +34,16 @@ func post[T any](ctx context.Context, api API, req Request, output *Response[T])
}
defer response.Body.Close()

buffer := new(bytes.Buffer)
if _, err := io.Copy(buffer, response.Body); err != nil {
return err
}

if response.StatusCode != http.StatusOK {
return errors.Wrapf(ErrRequest, "request %d invalid status code: %d", output.ID, response.StatusCode)
}

if err := json.NewDecoder(response.Body).Decode(output); err != nil {
if err := json.NewDecoder(buffer).Decode(output); err != nil {
return err
}

Expand Down
34 changes: 23 additions & 11 deletions pkg/sequencer/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,12 @@ type API struct {
feederGatewayUrl string
cacheDir string
rateLimit *rate.Limiter
rps int
}

// NewAPI - constructor of API
func NewAPI(gatewayUrl, feederGatewayUrl string, opts ...ApiOption) API {
t := http.DefaultTransport.(*http.Transport).Clone()
t.MaxIdleConns = 100
t.MaxConnsPerHost = 100
t.MaxIdleConnsPerHost = 100

client := &http.Client{
Transport: t,
}
api := API{
client: client,
gatewayUrl: gatewayUrl,
feederGatewayUrl: feederGatewayUrl,
}
Expand All @@ -43,6 +35,21 @@ func NewAPI(gatewayUrl, feederGatewayUrl string, opts ...ApiOption) API {
opts[i](&api)
}

t := http.DefaultTransport.(*http.Transport).Clone()
if api.rps < 1 || api.rps > 100 {
t.MaxIdleConns = 10
t.MaxConnsPerHost = 10
t.MaxIdleConnsPerHost = 10
} else {
t.MaxIdleConns = api.rps
t.MaxConnsPerHost = api.rps
t.MaxIdleConnsPerHost = api.rps
}

api.client = &http.Client{
Transport: t,
}

return api
}

Expand Down Expand Up @@ -153,14 +160,19 @@ func (api API) post(ctx context.Context, baseURL, path string, args map[string]s
}
defer response.Body.Close()

buffer := new(bytes.Buffer)
if _, err := io.Copy(buffer, response.Body); err != nil {
return err
}

if response.StatusCode != http.StatusOK {
var e Error
if err := json.NewDecoder(response.Body).Decode(&e); err != nil {
if err := json.NewDecoder(buffer).Decode(&e); err != nil {
return errors.Wrap(ErrRequest, err.Error())
}
return errors.Wrap(ErrRequest, e.Error())
}

err = json.NewDecoder(response.Body).Decode(output)
err = json.NewDecoder(buffer).Decode(output)
return err
}
1 change: 1 addition & 0 deletions pkg/sequencer/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ func WithRateLimit(requestPerSecond int) ApiOption {
return func(api *API) {
if requestPerSecond > 0 {
api.rateLimit = rate.NewLimiter(rate.Every(time.Second/time.Duration(requestPerSecond)), requestPerSecond)
api.rps = requestPerSecond
}
}
}
Expand Down

0 comments on commit b2736a5

Please sign in to comment.