From 813328f25faa58397f1bd9e7401b9d94f9bf53e2 Mon Sep 17 00:00:00 2001 From: "Cuong. Duong Manh" Date: Fri, 18 Oct 2024 09:47:05 +0700 Subject: [PATCH] [fix] concurrent map iteration and map write --- vngcloud/client/http.go | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/vngcloud/client/http.go b/vngcloud/client/http.go index a96dacc..36dcc21 100644 --- a/vngcloud/client/http.go +++ b/vngcloud/client/http.go @@ -9,6 +9,7 @@ import ( ljtime "github.com/cuongpiger/joat/timer" lreq "github.com/imroc/req/v3" + lserr "github.com/vngcloud/vngcloud-go-sdk/v2/vngcloud/sdk_error" ) @@ -96,7 +97,7 @@ func (s *httpClient) WithReauthFunc(pauthOpt AuthOpts, preauthFunc func() (ISdkA } func (s *httpClient) DoRequest(purl string, preq IRequest) (*lreq.Response, lserr.IError) { - req := s.client.R().SetContext(s.context).SetHeaders(s.defaultHeaders).SetHeaders(preq.GetMoreHeaders()) + req := s.client.R().SetContext(s.context).SetHeaders(s.getDefaultHeaders()).SetHeaders(preq.GetMoreHeaders()) if opt := preq.GetRequestBody(); opt != nil { req.SetBodyJsonMarshal(opt) } @@ -216,6 +217,16 @@ func (s *httpClient) setAccessToken(pnewToken ISdkAuthentication) IHttpClient { return s } +func (s *httpClient) getDefaultHeaders() map[string]string { + s.mut.RLock() + defer s.mut.RUnlock() + if s.defaultHeaders == nil { + s.defaultHeaders = make(map[string]string) + } + + return s.defaultHeaders +} + func newReauthFuture() *reauthFuture { return &reauthFuture{ done: make(chan struct{}),