"rsync for cloud storage" - Google Drive, S3, Dropbox, Backblaze B2, One Drive, Swift, Hubic, Wasabi, Google Cloud Storage, Azure Blob, Azure Files, Yandex Files
Go azure-blobazure-blob-storageazure-filesbackblaze-b2cloud-storagedropboxencryptionftpfuse-filesystemgogolanggoogle-cloud-storagegoogle-driveonedriveopenstack-swiftrclones3sftpsyncwebdav
Make rest Set methods safe for concurrent calling
Nick Craig-Wood70902b4051d9
diff --git a/rest/rest.go b/rest/rest.goindex 20ac23a3c..7acf35fed 100644--- a/rest/rest.go+++ b/rest/rest.go@@ -1,4 +1,6 @@// Package rest implements a simple REST wrapper+//+// All methods are safe for concurrent calling.package restimport (@@ -8,12 +10,14 @@ import ("io""io/ioutil""net/http"+ "sync""github.com/ncw/rclone/fs")// Client contains the info to sustain the APItype Client struct {+ mu sync.RWMutexc *http.ClientrootURL stringerrorHandler func(resp *http.Response) error@@ -45,18 +49,24 @@ func defaultErrorHandler(resp *http.Response) (err error) {// SetErrorHandler sets the handler to decode an error response when// the HTTP status code is not 2xx. The handler should close resp.Body.func (api *Client) SetErrorHandler(fn func(resp *http.Response) error) *Client {+ api.mu.Lock()+ defer api.mu.Unlock()api.errorHandler = fnreturn api}// SetRoot sets the default root URLfunc (api *Client) SetRoot(RootURL string) *Client {+ api.mu.Lock()+ defer api.mu.Unlock()api.rootURL = RootURLreturn api}// SetHeader sets a header for all requestsfunc (api *Client) SetHeader(key, value string) *Client {+ api.mu.Lock()+ defer api.mu.Unlock()api.headers[key] = valuereturn api}@@ -89,6 +99,8 @@ func DecodeJSON(resp *http.Response, result interface{}) (err error) {//// it will return resp if at all possible, even if err is setfunc (api *Client) Call(opts *Opts) (resp *http.Response, err error) {+ api.mu.RLock()+ defer api.mu.RUnlock()if opts == nil {return nil, fmt.Errorf("call() called with nil opts")}@@ -127,12 +139,16 @@ func (api *Client) Call(opts *Opts) (resp *http.Response, err error) {}// Now set the headersfor k, v := range headers {- req.Header.Add(k, v)+ if v != "" {+ req.Header.Add(k, v)+ }}if opts.UserName != "" || opts.Password != "" {req.SetBasicAuth(opts.UserName, opts.Password)}+ api.mu.RUnlock()resp, err = api.c.Do(req)+ api.mu.RLock()if err != nil {return nil, err}