Skip to content

Commit

Permalink
feat(go): expose ExposeIntermediateNetworkErrors to the config (#3404)
Browse files Browse the repository at this point in the history
  • Loading branch information
millotp authored Jul 21, 2024
1 parent 441febb commit 82562cc
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,5 @@ func (e *NoMoreHostToTryError) IntermediateNetworkErrors() []error {
}

func (e *NoMoreHostToTryError) Error() string {
return "all hosts have been contacted unsuccessfully, it can either be a server or a network error or wrong appID/key credentials were used. You can use opt.ExposeIntermediateNetworkErrors(true) to investigate."
return "all hosts have been contacted unsuccessfully, it can either be a server or a network error or wrong appID/key credentials were used. You can use 'ExposeIntermediateNetworkErrors: true' in the config to investigate."
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,13 @@ type Configuration struct {
AppID string
ApiKey string

Hosts []StatefulHost
DefaultHeader map[string]string
UserAgent string
Requester Requester
ReadTimeout time.Duration
WriteTimeout time.Duration
ConnectTimeout time.Duration
Compression compression.Compression
Hosts []StatefulHost
DefaultHeader map[string]string
UserAgent string
Requester Requester
ReadTimeout time.Duration
WriteTimeout time.Duration
ConnectTimeout time.Duration
Compression compression.Compression
ExposeIntermediateNetworkErrors bool
}
46 changes: 21 additions & 25 deletions clients/algoliasearch-client-go/algolia/transport/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,38 +17,34 @@ import (
)

type Transport struct {
requester Requester
retryStrategy *RetryStrategy
compression compression.Compression
connectTimeout time.Duration
requester Requester
retryStrategy *RetryStrategy
compression compression.Compression
connectTimeout time.Duration
exposeIntermediateNetworkErrors bool
}

func New(
hosts []StatefulHost,
requester Requester,
readTimeout time.Duration,
writeTimeout time.Duration,
connectTimeout time.Duration,
compression compression.Compression,
) *Transport {
if connectTimeout == 0 {
connectTimeout = DefaultConnectTimeout
func New(cfg Configuration) *Transport {
transport := &Transport{
requester: cfg.Requester,
retryStrategy: newRetryStrategy(cfg.Hosts, cfg.ReadTimeout, cfg.WriteTimeout),
connectTimeout: cfg.ConnectTimeout,
compression: cfg.Compression,
exposeIntermediateNetworkErrors: cfg.ExposeIntermediateNetworkErrors,
}

if requester == nil {
requester = NewDefaultRequester(&connectTimeout)
if transport.connectTimeout == 0 {
transport.connectTimeout = DefaultConnectTimeout
}

return &Transport{
requester: requester,
retryStrategy: newRetryStrategy(hosts, readTimeout, writeTimeout),
compression: compression,
connectTimeout: connectTimeout,
if transport.requester == nil {
transport.requester = NewDefaultRequester(&transport.connectTimeout)
}

return transport
}

func (t *Transport) Request(ctx context.Context, req *http.Request, k call.Kind) (*http.Response, []byte, error) {
exposeIntermediateNetworkErrors := false // todo: expose this option to the user
var intermediateNetworkErrors []error

// Add Content-Encoding header, if needed
Expand Down Expand Up @@ -109,7 +105,7 @@ func (t *Transport) Request(ctx context.Context, req *http.Request, k call.Kind)
cancel()
}

if exposeIntermediateNetworkErrors {
if t.exposeIntermediateNetworkErrors {
return nil, nil, errs.NewNoMoreHostToTryError(intermediateNetworkErrors...)
}

Expand All @@ -126,8 +122,8 @@ func (t *Transport) request(req *http.Request, host Host, timeout time.Duration,

if err != nil {
msg := fmt.Sprintf("cannot perform request:\n\terror=%v\n\tmethod=%s\n\turl=%s", err, req.Method, req.URL)
nerr, ok := err.(net.Error)
if ok {
var nerr net.Error
if errors.As(err, &nerr) {
// Because net.Error and error have different meanings for the
// retry strategy, we cannot simply return a new error, which
// would make all net.Error simple errors instead. To keep this
Expand Down
16 changes: 2 additions & 14 deletions templates/go/client.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ return NewClientWithConfig({{#lambda.titlecase}}{{#lambda.camelcase}}{{client}}{

// NewClientWithConfig creates a new API client with the given configuration to fully customize the client behaviour.
func NewClientWithConfig(cfg {{#lambda.titlecase}}{{#lambda.camelcase}}{{client}}{{/lambda.camelcase}}{{/lambda.titlecase}}Configuration) (*APIClient, error) {
var hosts []transport.StatefulHost
if cfg.AppID == "" {
return nil, errors.New("`appId` is missing.")
}
Expand All @@ -61,12 +59,7 @@ func NewClientWithConfig(cfg {{#lambda.titlecase}}{{#lambda.camelcase}}{{client}
if {{^fallbackToAliasHost}}cfg.Region == "" || {{/fallbackToAliasHost}}(cfg.Region != "" && !slices.Contains(allowedRegions[:], string(cfg.Region))) {
return nil, fmt.Errorf("`region` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: %s", strings.Join(allowedRegions[:], ", "))
}{{/hasRegionalHost}}
hosts = getDefaultHosts({{#hasRegionalHost}}cfg.Region{{/hasRegionalHost}}{{#hostWithAppID}}cfg.AppID{{/hostWithAppID}})
} else {
hosts = cfg.Hosts
}
if cfg.Requester == nil {
cfg.Requester = transport.NewDefaultRequester(&cfg.ConnectTimeout)
cfg.Hosts = getDefaultHosts({{#hasRegionalHost}}cfg.Region{{/hasRegionalHost}}{{#hostWithAppID}}cfg.AppID{{/hostWithAppID}})
}
if cfg.UserAgent == "" {
cfg.UserAgent = getUserAgent()
Expand All @@ -76,12 +69,7 @@ func NewClientWithConfig(cfg {{#lambda.titlecase}}{{#lambda.camelcase}}{{client}
appID: cfg.AppID,
cfg: &cfg,
transport: transport.New(
hosts,
cfg.Requester,
cfg.ReadTimeout,
cfg.WriteTimeout,
cfg.ConnectTimeout,
cfg.Compression,
cfg.Configuration,
),
}, nil
}
Expand Down

0 comments on commit 82562cc

Please sign in to comment.