/
context.go
239 lines (202 loc) · 7.67 KB
/
context.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Copyright 2023 Cisco Systems, Inc. and its affiliates
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
package httpclient
import (
"context"
"github.com/cisco-open/go-lanai/pkg/discovery"
"github.com/cisco-open/go-lanai/pkg/log"
"github.com/cisco-open/go-lanai/pkg/utils"
"net/http"
"net/url"
"time"
)
type Client interface {
// Execute send the provided request and parse the response using provided ResponseOptions
// When using default decoding function:
// - it returns non-nil Response only if the response has 2XX status code
// - it returns non-nil error for 4XX, 5XX status code or any other type of errors
// - the returned error can be casted to *Error
Execute(ctx context.Context, request *Request, opts ...ResponseOptions) (*Response, error)
// WithService create a client with specific service with given instance selectors.
// The returned client is responsible to track service instance changes with help of discovery package,
// and to perform load-balancing and retrying.
// The returned client is goroutine-safe and can be reused
WithService(service string, opts ...SDOptions) (Client, error)
// WithBaseUrl create a client with specific base URL.
// The returned client is responsible to perform retrying.
// The returned client is goroutine-safe and can be reused
WithBaseUrl(baseUrl string) (Client, error)
// WithConfig create a shallow copy of the client with specified config.
// Service (with LB) or BaseURL cannot be changed with this method.
// If any field of provided config is zero value, this value is not applied.
// The returned client is goroutine-safe and can be reused
WithConfig(config *ClientConfig) Client
}
// ClientOptions is used for creating Client and its customizers
type ClientOptions func(opt *ClientOption)
// ClientOption carries initial configurations of Clients
type ClientOption struct {
ClientConfig
SDClient discovery.Client
DefaultSelector discovery.InstanceMatcher
DefaultBeforeHooks []BeforeHook
DefaultAfterHooks []AfterHook
}
// ClientConfig is used to change Client's config
type ClientConfig struct {
// HTTPClient underlying http.Client to use
HTTPClient *http.Client
// BeforeHooks hooks to use before sending HTTP request
BeforeHooks []BeforeHook
// AfterHooks hooks to use before sending HTTP request
AfterHooks []AfterHook
// MaxRetries number of retries in case of error. Negative value means no retry.
// Note: by default, non-2XX response status code error is not retried
MaxRetries int
// RetryBackoff time to wait between retries. Negative means retry immediately
RetryBackoff time.Duration
// RetryCallback allows fine control when and how to retry.
// If set, this override MaxRetries and RetryBackoff
RetryCallback RetryCallback
// Timeout how long to wait for each execution.
// Note: this is total duration including RetryBackoff between each attempt, not per-retry timeout.
Timeout time.Duration
// Logger used for logging request/response
Logger log.ContextualLogger
// Logging configuration of request/response logging
Logging LoggingConfig
}
type LoggingConfig struct {
Level log.LoggingLevel
DetailsLevel LogDetailsLevel
SanitizeHeaders utils.StringSet
ExcludeHeaders utils.StringSet
}
type ClientCustomizer interface {
Customize(opt *ClientOption)
}
type ClientCustomizerFunc func(opt *ClientOption)
func (fn ClientCustomizerFunc) Customize(opt *ClientOption) {
fn(opt)
}
// Hook is used for intercepting is used for ClientConfig and ClientOptions,
type Hook interface {
// Before is invoked after the HTTP request is encoded and before the request is sent.
// The implementing class could also implement order.Ordered interface. Highest order is invoked first
Before(context.Context, *http.Request) context.Context
// After is invoked after HTTP response is returned and before the response is decoded.
// The implementing class could also implement order.Ordered interface. Highest order is invoked first
After(context.Context, *http.Response) context.Context
}
// BeforeHook is used for ClientConfig and ClientOptions,
// The implementing class could also implement order.Ordered interface. Highest order is invoked first
type BeforeHook interface {
// Before is invoked after the HTTP request is encoded and before the request is sent.
Before(context.Context, *http.Request) context.Context
}
// ConfigurableBeforeHook is an additional interface that BeforeHook can implement
type ConfigurableBeforeHook interface {
WithConfig(cfg *ClientConfig) BeforeHook
}
// AfterHook is used for ClientConfig and ClientOptions,
// The implementing class could also implement order.Ordered interface. Highest order is invoked first
type AfterHook interface {
// After is invoked after HTTP response is returned and before the response is decoded.
After(context.Context, *http.Response) context.Context
}
// ConfigurableAfterHook is an additional interface that AfterHook can implement
type ConfigurableAfterHook interface {
WithConfig(cfg *ClientConfig) AfterHook
}
type TargetResolver interface {
Resolve(ctx context.Context, req *Request) (*url.URL, error)
}
type TargetResolverFunc func(ctx context.Context, req *Request) (*url.URL, error)
func (fn TargetResolverFunc) Resolve(ctx context.Context, req *Request) (*url.URL, error) {
return fn(ctx, req)
}
/************************
Common Impl.
************************/
func DefaultConfig() *ClientConfig {
return &ClientConfig{
HTTPClient: http.DefaultClient,
BeforeHooks: []BeforeHook{},
AfterHooks: []AfterHook{},
MaxRetries: 3,
Timeout: 1 * time.Minute,
Logger: logger,
Logging: LoggingConfig{
DetailsLevel: LogDetailsLevelHeaders,
SanitizeHeaders: utils.NewStringSet(HeaderAuthorization),
ExcludeHeaders: utils.NewStringSet(),
},
}
}
// defaultServiceConfig add necessary configs/hooks for internal load balanced service
func defaultServiceConfig() *ClientConfig {
return &ClientConfig{
BeforeHooks: []BeforeHook{HookTokenPassthrough()},
}
}
// defaultExtHostConfig add necessary configs/hooks for external hosts
func defaultExtHostConfig() *ClientConfig {
return &ClientConfig{}
}
func mergeConfig(dst *ClientConfig, src *ClientConfig) {
if dst.HTTPClient == nil {
dst.HTTPClient = src.HTTPClient
}
if dst.Logger == nil {
dst.Logger = src.Logger
}
if dst.Timeout <= 0 {
dst.Timeout = src.Timeout
}
if dst.BeforeHooks == nil {
dst.BeforeHooks = src.BeforeHooks
}
if dst.AfterHooks == nil {
dst.AfterHooks = src.AfterHooks
}
switch {
case dst.MaxRetries < 0:
dst.MaxRetries = 0
case dst.MaxRetries == 0:
dst.MaxRetries = src.MaxRetries
}
switch {
case dst.RetryBackoff < 0:
dst.RetryBackoff = 0
case dst.RetryBackoff == 0:
dst.RetryBackoff = src.RetryBackoff
}
if dst.RetryCallback == nil {
dst.RetryCallback = src.RetryCallback
}
if dst.Logging.SanitizeHeaders == nil {
dst.Logging.SanitizeHeaders = src.Logging.SanitizeHeaders
}
if dst.Logging.ExcludeHeaders == nil {
dst.Logging.ExcludeHeaders = src.Logging.ExcludeHeaders
}
if dst.Logging.DetailsLevel == LogDetailsLevelUnknown {
dst.Logging.DetailsLevel = src.Logging.DetailsLevel
}
if dst.Logging.Level == log.LevelOff {
dst.Logging.Level = src.Logging.Level
}
}