From 453a5bb728b4edb86dbb39a25305cf7666fa2801 Mon Sep 17 00:00:00 2001 From: Nathan Hyland Date: Mon, 16 Oct 2017 10:50:48 -0700 Subject: [PATCH] Changed method receiver to pointer, removing the need for a mutex address --- error_test.go | 5 ++--- stripe.go | 30 +++++++++++++++--------------- stripe_test.go | 18 +++++++++--------- 3 files changed, 26 insertions(+), 27 deletions(-) diff --git a/error_test.go b/error_test.go index defcabf14a..919a6137cc 100644 --- a/error_test.go +++ b/error_test.go @@ -4,7 +4,6 @@ import ( "fmt" "net/http" "net/http/httptest" - "sync" "testing" assert "github.com/stretchr/testify/require" @@ -23,8 +22,8 @@ func TestErrorResponse(t *testing.T) { })) defer ts.Close() - SetBackend("api", BackendConfiguration{ - Mutex: &sync.Mutex{}, Type: APIBackend, URL: ts.URL, HTTPClient: &http.Client{}, + SetBackend("api", &BackendConfiguration{ + Type: APIBackend, URL: ts.URL, HTTPClient: &http.Client{}, }) err := GetBackend(APIBackend).Call("GET", "/v1/account", "sk_test_badKey", nil, nil, nil) diff --git a/stripe.go b/stripe.go index 407e9edf86..7a8d40592b 100644 --- a/stripe.go +++ b/stripe.go @@ -76,10 +76,10 @@ type Backend interface { // BackendConfiguration is the internal implementation for making HTTP calls to Stripe. type BackendConfiguration struct { - Mutex *sync.Mutex Type SupportedBackend URL string HTTPClient *http.Client + sync.Mutex } // SupportedBackend is an enumeration of supported Stripe endpoints. @@ -159,10 +159,10 @@ func SetHTTPClient(client *http.Client) { // should only need to use this for testing purposes or on App Engine. func NewBackends(httpClient *http.Client) *Backends { return &Backends{ - API: BackendConfiguration{ - Mutex: &sync.Mutex{}, Type: APIBackend, URL: APIURL, HTTPClient: httpClient}, - Uploads: BackendConfiguration{ - Mutex: &sync.Mutex{}, Type: UploadsBackend, URL: UploadsURL, HTTPClient: httpClient}, + API: &BackendConfiguration{ + Type: APIBackend, URL: APIURL, HTTPClient: httpClient}, + Uploads: &BackendConfiguration{ + Type: UploadsBackend, URL: UploadsURL, HTTPClient: httpClient}, } } @@ -172,15 +172,15 @@ func GetBackend(backend SupportedBackend) Backend { switch backend { case APIBackend: if backends.API == nil { - backends.API = BackendConfiguration{ - Mutex: &sync.Mutex{}, Type: backend, URL: apiURL, HTTPClient: httpClient} + backends.API = &BackendConfiguration{ + Type: backend, URL: apiURL, HTTPClient: httpClient} } ret = backends.API case UploadsBackend: if backends.Uploads == nil { - backends.Uploads = BackendConfiguration{ - Mutex: &sync.Mutex{}, Type: backend, URL: uploadsURL, HTTPClient: httpClient} + backends.Uploads = &BackendConfiguration{ + Type: backend, URL: uploadsURL, HTTPClient: httpClient} } ret = backends.Uploads } @@ -199,7 +199,7 @@ func SetBackend(backend SupportedBackend, b Backend) { } // Call is the Backend.Call implementation for invoking Stripe APIs. -func (s BackendConfiguration) Call(method, path, key string, form *form.Values, params *Params, v interface{}) error { +func (s *BackendConfiguration) Call(method, path, key string, form *form.Values, params *Params, v interface{}) error { var body io.Reader if form != nil && !form.Empty() { data := form.Encode() @@ -223,7 +223,7 @@ func (s BackendConfiguration) Call(method, path, key string, form *form.Values, } // CallMultipart is the Backend.CallMultipart implementation for invoking Stripe APIs. -func (s BackendConfiguration) CallMultipart(method, path, key, boundary string, body io.Reader, params *Params, v interface{}) error { +func (s *BackendConfiguration) CallMultipart(method, path, key, boundary string, body io.Reader, params *Params, v interface{}) error { contentType := "multipart/form-data; boundary=" + boundary req, err := s.NewRequest(method, path, key, contentType, body, params) @@ -241,8 +241,8 @@ func (s BackendConfiguration) CallMultipart(method, path, key, boundary string, // NewRequest is used by Call to generate an http.Request. It handles encoding // parameters and attaching the appropriate headers. func (s *BackendConfiguration) NewRequest(method, path, key, contentType string, body io.Reader, params *Params) (*http.Request, error) { - s.Mutex.Lock() - defer s.Mutex.Unlock() + s.Lock() + defer s.Unlock() if !strings.HasPrefix(path, "/") { path = "/" + path } @@ -298,8 +298,8 @@ func (s *BackendConfiguration) NewRequest(method, path, key, contentType string, // the backend's HTTP client to execute the request and unmarshals the response // into v. It also handles unmarshaling errors returned by the API. func (s *BackendConfiguration) Do(req *http.Request, v interface{}) error { - s.Mutex.Lock() - defer s.Mutex.Unlock() + s.Lock() + defer s.Unlock() if LogLevel > 1 { Logger.Printf("Requesting %v %v%v\n", req.Method, req.URL.Host, req.URL.Path) } diff --git a/stripe_test.go b/stripe_test.go index fc0bd4fcf0..9f77cc2d7e 100644 --- a/stripe_test.go +++ b/stripe_test.go @@ -14,7 +14,7 @@ import ( ) func TestCheckinUseBearerAuth(t *testing.T) { - c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL} + c := &stripe.BackendConfiguration{URL: stripe.APIURL} key := "apiKey" req, err := c.NewRequest("", "", key, "", nil, nil) @@ -30,7 +30,7 @@ func TestMultipleAPICalls(t *testing.T) { wg.Add(1) go func() { defer wg.Done() - c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL} + c := &stripe.BackendConfiguration{URL: stripe.APIURL} key := "apiKey" req, err := c.NewRequest("", "", key, "", nil, nil) @@ -43,7 +43,7 @@ func TestMultipleAPICalls(t *testing.T) { } func TestIdempotencyKey(t *testing.T) { - c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL} + c := &stripe.BackendConfiguration{URL: stripe.APIURL} p := &stripe.Params{IdempotencyKey: "idempotency-key"} req, err := c.NewRequest("", "", "", "", nil, p) @@ -53,7 +53,7 @@ func TestIdempotencyKey(t *testing.T) { } func TestStripeAccount(t *testing.T) { - c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL} + c := &stripe.BackendConfiguration{URL: stripe.APIURL} p := &stripe.Params{StripeAccount: TestMerchantID} req, err := c.NewRequest("", "", "", "", nil, p) @@ -72,7 +72,7 @@ func TestStripeAccount(t *testing.T) { } func TestUserAgent(t *testing.T) { - c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL} + c := &stripe.BackendConfiguration{URL: stripe.APIURL} req, err := c.NewRequest("", "", "", "", nil, nil) assert.NoError(t, err) @@ -94,7 +94,7 @@ func TestUserAgentWithAppInfo(t *testing.T) { stripe.SetAppInfo(appInfo) defer stripe.SetAppInfo(nil) - c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL} + c := &stripe.BackendConfiguration{URL: stripe.APIURL} req, err := c.NewRequest("", "", "", "", nil, nil) assert.NoError(t, err) @@ -108,7 +108,7 @@ func TestUserAgentWithAppInfo(t *testing.T) { } func TestStripeClientUserAgent(t *testing.T) { - c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL} + c := &stripe.BackendConfiguration{URL: stripe.APIURL} req, err := c.NewRequest("", "", "", "", nil, nil) assert.NoError(t, err) @@ -142,7 +142,7 @@ func TestStripeClientUserAgentWithAppInfo(t *testing.T) { stripe.SetAppInfo(appInfo) defer stripe.SetAppInfo(nil) - c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL} + c := &stripe.BackendConfiguration{URL: stripe.APIURL} req, err := c.NewRequest("", "", "", "", nil, nil) assert.NoError(t, err) @@ -161,7 +161,7 @@ func TestStripeClientUserAgentWithAppInfo(t *testing.T) { } func TestResponseToError(t *testing.T) { - c := &stripe.BackendConfiguration{Mutex: &sync.Mutex{}, URL: stripe.APIURL} + c := &stripe.BackendConfiguration{URL: stripe.APIURL} // A test response that includes a status code and request ID. res := &http.Response{