Skip to content

Commit

Permalink
upstream: remove sync.Once
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneOne1 committed Dec 17, 2020
1 parent 251fb36 commit 744370d
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions upstream/upstream_doh.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ const DoHMaxConnsPerHost = 1
type dnsOverHTTPS struct {
boot *bootstrapper

// mu exists for lazy initialization purposes and protects client from
// data race during lazy initialization. It provides the exchange with
// invalid upstream possibility, which is needed for now. Should be
// refactored further.
mu sync.Mutex

// The Client's Transport typically has internal state (cached TCP
// connections), so Clients should be reused instead of created as
// needed. Clients are safe for concurrent use by multiple goroutines.
client *http.Client

// For lazy initialization purposes.
sync.Once
// For lazy initialization purposes.
sync.Mutex
}

func (p *dnsOverHTTPS) Address() string { return p.boot.address }
Expand Down Expand Up @@ -94,8 +95,8 @@ func (p *dnsOverHTTPS) exchangeHTTPSClient(m *dns.Msg, client *http.Client) (*dn
func (p *dnsOverHTTPS) getClient() (c *http.Client, err error) {
startTime := time.Now()

p.Lock()
defer p.Unlock()
p.mu.Lock()
defer p.mu.Unlock()
if p.client != nil {
return p.client, nil
}
Expand All @@ -107,12 +108,9 @@ func (p *dnsOverHTTPS) getClient() (c *http.Client, err error) {
return nil, fmt.Errorf("timeout exceeded: %d ms", int(elapsed/time.Millisecond))
}

// TODO(e.burkov): get rid of lazy initialization.
p.Do(func() {
c, err = p.createClient()
})
p.client, err = p.createClient()

return c, err
return p.client, err
}

func (p *dnsOverHTTPS) createClient() (*http.Client, error) {
Expand Down

0 comments on commit 744370d

Please sign in to comment.