-
Notifications
You must be signed in to change notification settings - Fork 66
feat(flipt): add WithHTTPClient option for custom HTTP client configuration #784
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,6 +6,7 @@ import ( | |
| "crypto/x509" | ||
| "errors" | ||
| "fmt" | ||
| "net/http" | ||
| "net/url" | ||
| "os" | ||
| "strings" | ||
|
|
@@ -40,11 +41,19 @@ type Service struct { | |
| once sync.Once | ||
| tokenProvider sdk.ClientTokenProvider | ||
| grpcDialOptions []grpc.DialOption | ||
| httpClient *http.Client | ||
| } | ||
|
|
||
| // Option is a service option. | ||
| type Option func(*Service) | ||
|
|
||
| // WithHTTPClient returns an [Option] that specifies the HTTP client to use as the basis of communications. | ||
| func WithHTTPClient(client *http.Client) Option { | ||
| return func(s *Service) { | ||
| s.httpClient = client | ||
|
Comment on lines
+51
to
+53
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To ensure thread-safe access to the Consider the following code suggestion to address this issue. func WithHTTPClient(client *http.Client) Option {
return func(s *Service) {
// Acquire a write lock before modifying the httpClient.
// Release the lock after the modification is complete.
// This ensures that only one goroutine can modify the httpClient at a time.
// Prevents race conditions.
s.httpClientMutex.Lock()
s.httpClient = client
s.httpClientMutex.Unlock()
}
} |
||
| } | ||
| } | ||
|
|
||
| // WithAddress sets the address for the remote Flipt gRPC API. | ||
| func WithAddress(address string) Option { | ||
| return func(s *Service) { | ||
|
|
@@ -91,6 +100,7 @@ func New(opts ...Option) *Service { | |
| grpcDialOptions: []grpc.DialOption{ | ||
| grpc.WithStatsHandler(otelgrpc.NewClientHandler()), | ||
| }, | ||
| httpClient: http.DefaultClient, | ||
| } | ||
|
|
||
| for _, opt := range opts { | ||
|
|
@@ -158,7 +168,7 @@ func (s *Service) instance() (offlipt.Client, error) { | |
| opts = append(opts, sdk.WithClientTokenProvider(s.tokenProvider)) | ||
| } | ||
|
|
||
| hclient := sdk.New(sdkhttp.NewTransport(s.address), opts...) | ||
| hclient := sdk.New(sdkhttp.NewTransport(s.address, sdkhttp.WithHTTPClient(s.httpClient)), opts...) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To ensure thread-safe access to the Consider the following code suggestion to address this issue. // Acquire a read lock before accessing the httpClient.
// Release the lock after the access is complete.
// This allows multiple goroutines to read the httpClient concurrently,
// but prevents them from modifying it while it's being read.
s.httpClientMutex.RLock()
hclient := sdk.New(sdkhttp.NewTransport(s.address, sdkhttp.WithHTTPClient(s.httpClient)), opts...)
s.httpClientMutex.RUnlock()
if u.Scheme == "https" || u.Scheme == "http" { |
||
| if u.Scheme == "https" || u.Scheme == "http" { | ||
| s.client = &fclient{ | ||
| hclient.Flipt(), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
httpClientfield in theServicestruct is not protected by a mutex. If multiple goroutines access theServiceconcurrently, they could potentially modify thehttpClientsimultaneously, leading to a race condition. To prevent this, consider adding a mutex to protect access to thehttpClientfield.Then, use the mutex to protect access to the
httpClientin theWithHTTPClientoption and theinstancemethod.