From 333afe25d0e00ac79732dc7d0a2a3f4b3ac5af06 Mon Sep 17 00:00:00 2001 From: Pierre Millot Date: Sun, 21 Jul 2024 09:42:53 +0200 Subject: [PATCH] feat(go): expose ExposeIntermediateNetworkErrors to the config --- .../algolia/errs/no_more_host_to_try_err.go | 2 +- .../algolia/transport/configuration.go | 17 +++---- .../algolia/transport/transport.go | 46 +++++++++---------- templates/go/client.mustache | 16 +------ 4 files changed, 33 insertions(+), 48 deletions(-) diff --git a/clients/algoliasearch-client-go/algolia/errs/no_more_host_to_try_err.go b/clients/algoliasearch-client-go/algolia/errs/no_more_host_to_try_err.go index cf58272a4a6..4c71ffa0b55 100644 --- a/clients/algoliasearch-client-go/algolia/errs/no_more_host_to_try_err.go +++ b/clients/algoliasearch-client-go/algolia/errs/no_more_host_to_try_err.go @@ -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." } diff --git a/clients/algoliasearch-client-go/algolia/transport/configuration.go b/clients/algoliasearch-client-go/algolia/transport/configuration.go index 411e7de940e..431cad634b7 100644 --- a/clients/algoliasearch-client-go/algolia/transport/configuration.go +++ b/clients/algoliasearch-client-go/algolia/transport/configuration.go @@ -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 } diff --git a/clients/algoliasearch-client-go/algolia/transport/transport.go b/clients/algoliasearch-client-go/algolia/transport/transport.go index 00e53e0ff4d..345f98278a5 100644 --- a/clients/algoliasearch-client-go/algolia/transport/transport.go +++ b/clients/algoliasearch-client-go/algolia/transport/transport.go @@ -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 @@ -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...) } @@ -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 diff --git a/templates/go/client.mustache b/templates/go/client.mustache index 6a5d77cddf4..6314d1e0330 100644 --- a/templates/go/client.mustache +++ b/templates/go/client.mustache @@ -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.") } @@ -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() @@ -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 }