Skip to content

Commit

Permalink
DE-1329 Fix race conditions with capturedCurlOutput fields (#333)
Browse files Browse the repository at this point in the history
  • Loading branch information
vtopc authored Oct 29, 2024
1 parent 9999804 commit 609ce8b
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 12 deletions.
2 changes: 2 additions & 0 deletions exports.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,9 @@ func (mg *MailgunImpl) GetExportLink(ctx context.Context, id string) (string, er

if Debug {
if CaptureCurlOutput {
r.mu.Lock()
r.capturedCurlOutput = r.curlString(req, nil)
r.mu.Unlock()
} else {
fmt.Println(r.curlString(req, nil))
}
Expand Down
17 changes: 11 additions & 6 deletions httphelpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,22 @@ import (
"path"
"regexp"
"strings"
"sync"

"github.com/mailgun/errors"
)

var validURL = regexp.MustCompile(`/v[1-5].*`)

type httpRequest struct {
URL string
Parameters map[string][]string
Headers map[string]string
BasicAuthUser string
BasicAuthPassword string
Client *http.Client
URL string
Parameters map[string][]string
Headers map[string]string
BasicAuthUser string
BasicAuthPassword string
Client *http.Client

mu sync.RWMutex
capturedCurlOutput string
}

Expand Down Expand Up @@ -294,7 +297,9 @@ func (r *httpRequest) makeRequest(ctx context.Context, method string, payload pa

if Debug {
if CaptureCurlOutput {
r.mu.Lock()
r.capturedCurlOutput = r.curlString(req, payload)
r.mu.Unlock()
} else {
fmt.Println(r.curlString(req, payload))
}
Expand Down
18 changes: 12 additions & 6 deletions mailgun.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ import (
"io"
"net/http"
"os"
"sync"
"time"
)

Expand Down Expand Up @@ -267,12 +268,14 @@ type Mailgun interface {
// MailgunImpl bundles data needed by a large number of methods in order to interact with the Mailgun API.
// Colloquially, we refer to instances of this structure as "clients."
type MailgunImpl struct {
apiBase string
domain string
apiKey string
client *http.Client
baseURL string
overrideHeaders map[string]string
apiBase string
domain string
apiKey string
client *http.Client
baseURL string
overrideHeaders map[string]string

mu sync.RWMutex
capturedCurlOutput string
}

Expand Down Expand Up @@ -370,6 +373,9 @@ func (mg *MailgunImpl) AddOverrideHeader(k string, v string) {
// mailgun.CaptureCurlOutput must be set to true
// This is mostly useful for testing the Mailgun API hosted at a different endpoint.
func (mg *MailgunImpl) GetCurlOutput() string {
mg.mu.RLock()
defer mg.mu.RUnlock()

return mg.capturedCurlOutput
}

Expand Down
4 changes: 4 additions & 0 deletions messages.go
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,11 @@ func (mg *MailgunImpl) Send(ctx context.Context, message *Message) (mes string,
id = response.Id
}

r.mu.RLock()
defer r.mu.RUnlock()
if r.capturedCurlOutput != "" {
mg.mu.Lock()
defer mg.mu.Unlock()
mg.capturedCurlOutput = r.capturedCurlOutput
}

Expand Down

0 comments on commit 609ce8b

Please sign in to comment.