Skip to content

Commit 82562cc

Browse files
authored
feat(go): expose ExposeIntermediateNetworkErrors to the config (#3404)
1 parent 441febb commit 82562cc

File tree

4 files changed

+33
-48
lines changed

4 files changed

+33
-48
lines changed

clients/algoliasearch-client-go/algolia/errs/no_more_host_to_try_err.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,5 @@ func (e *NoMoreHostToTryError) IntermediateNetworkErrors() []error {
1717
}
1818

1919
func (e *NoMoreHostToTryError) Error() string {
20-
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."
20+
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."
2121
}

clients/algoliasearch-client-go/algolia/transport/configuration.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,13 @@ type Configuration struct {
1010
AppID string
1111
ApiKey string
1212

13-
Hosts []StatefulHost
14-
DefaultHeader map[string]string
15-
UserAgent string
16-
Requester Requester
17-
ReadTimeout time.Duration
18-
WriteTimeout time.Duration
19-
ConnectTimeout time.Duration
20-
Compression compression.Compression
13+
Hosts []StatefulHost
14+
DefaultHeader map[string]string
15+
UserAgent string
16+
Requester Requester
17+
ReadTimeout time.Duration
18+
WriteTimeout time.Duration
19+
ConnectTimeout time.Duration
20+
Compression compression.Compression
21+
ExposeIntermediateNetworkErrors bool
2122
}

clients/algoliasearch-client-go/algolia/transport/transport.go

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,38 +17,34 @@ import (
1717
)
1818

1919
type Transport struct {
20-
requester Requester
21-
retryStrategy *RetryStrategy
22-
compression compression.Compression
23-
connectTimeout time.Duration
20+
requester Requester
21+
retryStrategy *RetryStrategy
22+
compression compression.Compression
23+
connectTimeout time.Duration
24+
exposeIntermediateNetworkErrors bool
2425
}
2526

26-
func New(
27-
hosts []StatefulHost,
28-
requester Requester,
29-
readTimeout time.Duration,
30-
writeTimeout time.Duration,
31-
connectTimeout time.Duration,
32-
compression compression.Compression,
33-
) *Transport {
34-
if connectTimeout == 0 {
35-
connectTimeout = DefaultConnectTimeout
27+
func New(cfg Configuration) *Transport {
28+
transport := &Transport{
29+
requester: cfg.Requester,
30+
retryStrategy: newRetryStrategy(cfg.Hosts, cfg.ReadTimeout, cfg.WriteTimeout),
31+
connectTimeout: cfg.ConnectTimeout,
32+
compression: cfg.Compression,
33+
exposeIntermediateNetworkErrors: cfg.ExposeIntermediateNetworkErrors,
3634
}
3735

38-
if requester == nil {
39-
requester = NewDefaultRequester(&connectTimeout)
36+
if transport.connectTimeout == 0 {
37+
transport.connectTimeout = DefaultConnectTimeout
4038
}
4139

42-
return &Transport{
43-
requester: requester,
44-
retryStrategy: newRetryStrategy(hosts, readTimeout, writeTimeout),
45-
compression: compression,
46-
connectTimeout: connectTimeout,
40+
if transport.requester == nil {
41+
transport.requester = NewDefaultRequester(&transport.connectTimeout)
4742
}
43+
44+
return transport
4845
}
4946

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

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

112-
if exposeIntermediateNetworkErrors {
108+
if t.exposeIntermediateNetworkErrors {
113109
return nil, nil, errs.NewNoMoreHostToTryError(intermediateNetworkErrors...)
114110
}
115111

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

127123
if err != nil {
128124
msg := fmt.Sprintf("cannot perform request:\n\terror=%v\n\tmethod=%s\n\turl=%s", err, req.Method, req.URL)
129-
nerr, ok := err.(net.Error)
130-
if ok {
125+
var nerr net.Error
126+
if errors.As(err, &nerr) {
131127
// Because net.Error and error have different meanings for the
132128
// retry strategy, we cannot simply return a new error, which
133129
// would make all net.Error simple errors instead. To keep this

templates/go/client.mustache

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ return NewClientWithConfig({{#lambda.titlecase}}{{#lambda.camelcase}}{{client}}{
4949

5050
// NewClientWithConfig creates a new API client with the given configuration to fully customize the client behaviour.
5151
func NewClientWithConfig(cfg {{#lambda.titlecase}}{{#lambda.camelcase}}{{client}}{{/lambda.camelcase}}{{/lambda.titlecase}}Configuration) (*APIClient, error) {
52-
var hosts []transport.StatefulHost
53-
5452
if cfg.AppID == "" {
5553
return nil, errors.New("`appId` is missing.")
5654
}
@@ -61,12 +59,7 @@ func NewClientWithConfig(cfg {{#lambda.titlecase}}{{#lambda.camelcase}}{{client}
6159
if {{^fallbackToAliasHost}}cfg.Region == "" || {{/fallbackToAliasHost}}(cfg.Region != "" && !slices.Contains(allowedRegions[:], string(cfg.Region))) {
6260
return nil, fmt.Errorf("`region` {{^fallbackToAliasHost}}is required and {{/fallbackToAliasHost}}must be one of the following: %s", strings.Join(allowedRegions[:], ", "))
6361
}{{/hasRegionalHost}}
64-
hosts = getDefaultHosts({{#hasRegionalHost}}cfg.Region{{/hasRegionalHost}}{{#hostWithAppID}}cfg.AppID{{/hostWithAppID}})
65-
} else {
66-
hosts = cfg.Hosts
67-
}
68-
if cfg.Requester == nil {
69-
cfg.Requester = transport.NewDefaultRequester(&cfg.ConnectTimeout)
62+
cfg.Hosts = getDefaultHosts({{#hasRegionalHost}}cfg.Region{{/hasRegionalHost}}{{#hostWithAppID}}cfg.AppID{{/hostWithAppID}})
7063
}
7164
if cfg.UserAgent == "" {
7265
cfg.UserAgent = getUserAgent()
@@ -76,12 +69,7 @@ func NewClientWithConfig(cfg {{#lambda.titlecase}}{{#lambda.camelcase}}{{client}
7669
appID: cfg.AppID,
7770
cfg: &cfg,
7871
transport: transport.New(
79-
hosts,
80-
cfg.Requester,
81-
cfg.ReadTimeout,
82-
cfg.WriteTimeout,
83-
cfg.ConnectTimeout,
84-
cfg.Compression,
72+
cfg.Configuration,
8573
),
8674
}, nil
8775
}

0 commit comments

Comments
 (0)