-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
client: Add shared client support #7
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/cilium/dns | ||
|
||
go 1.14 | ||
go 1.18 | ||
|
||
require ( | ||
golang.org/x/net v0.2.0 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,253 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright Authors of Cilium | ||
|
||
package dns | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"net" | ||
"sync" | ||
"time" | ||
) | ||
|
||
// SharedClients holds a set of SharedClient instances. | ||
type SharedClients struct { | ||
// SharedClient's lock must not be taken while this lock is held! | ||
lock sync.Mutex | ||
// clients are created and destroyed on demand, hence 'Mutex' needs to be taken. | ||
clients map[string]*SharedClient | ||
} | ||
|
||
func NewSharedClients() *SharedClients { | ||
return &SharedClients{ | ||
clients: make(map[string]*SharedClient), | ||
} | ||
} | ||
|
||
func (s *SharedClients) Exchange(key string, conf *Client, m *Msg, serverAddrStr string) (r *Msg, rtt time.Duration, closer func(), err error) { | ||
return s.ExchangeContext(context.Background(), key, conf, m, serverAddrStr) | ||
} | ||
|
||
func (s *SharedClients) ExchangeContext(ctx context.Context, key string, conf *Client, m *Msg, serverAddrStr string) (r *Msg, rtt time.Duration, closer func(), err error) { | ||
client, closer := s.GetSharedClient(key, conf, serverAddrStr) | ||
r, rtt, err = client.ExchangeSharedContext(ctx, m) | ||
return r, rtt, closer, err | ||
} | ||
|
||
// GetSharedClient gets or creates an instance of SharedClient keyed with 'key'. if 'key' is an | ||
// empty sting, a new client is always created and it is not actually shared. The returned 'closer' | ||
// must be called once the client is no longer needed. Conversely, the returned 'client' must not be | ||
// used after the closer is called. | ||
func (s *SharedClients) GetSharedClient(key string, conf *Client, serverAddrStr string) (client *SharedClient, closer func()) { | ||
if key == "" { | ||
// Simplified case when the client is actually not shared | ||
client = newSharedClient(conf, serverAddrStr) | ||
return client, client.close | ||
} | ||
for { | ||
// lock for s.clients access | ||
s.lock.Lock() | ||
// locate client to re-use if possible. | ||
client = s.clients[key] | ||
if client == nil { | ||
client = newSharedClient(conf, serverAddrStr) | ||
s.clients[key] = client | ||
s.lock.Unlock() | ||
// new client, we are done | ||
break | ||
} | ||
s.lock.Unlock() | ||
|
||
// reusing client that may start closing while we wait for its lock | ||
client.Lock() | ||
if client.refcount > 0 { | ||
// not closed, add our refcount | ||
client.refcount++ | ||
client.Unlock() | ||
break | ||
} | ||
// client was closed while we waited for it's lock, discard and try again | ||
client.Unlock() | ||
client = nil | ||
} | ||
|
||
return client, func() { | ||
client.Lock() | ||
defer client.Unlock() | ||
client.refcount-- | ||
if client.refcount == 0 { | ||
// connection close must be completed while holding the client's lock to | ||
// avoid a race where a new client dials using the same 5-tuple and gets a | ||
// bind error. | ||
// The client remains findable so that new users with the same key may wait | ||
// for this closing to be done with. | ||
client.close() | ||
// Make client unreachable | ||
// Must take s.lock for this. | ||
s.lock.Lock() | ||
delete(s.clients, key) | ||
s.lock.Unlock() | ||
} | ||
} | ||
} | ||
|
||
type request struct { | ||
ctx context.Context | ||
msg *Msg | ||
ch chan sharedClientResponse | ||
} | ||
|
||
type sharedClientResponse struct { | ||
msg *Msg | ||
rtt time.Duration | ||
err error | ||
} | ||
|
||
// A SharedClient keeps state for concurrent transactions on the same upstream client/connection. | ||
type SharedClient struct { | ||
serverAddr string | ||
|
||
*Client | ||
|
||
// requests is closed when the client needs to exit | ||
requests chan request | ||
// wg is waited on for the client finish exiting | ||
wg sync.WaitGroup | ||
|
||
sync.Mutex // protects the fields below | ||
refcount int | ||
conn *Conn | ||
} | ||
|
||
func newSharedClient(conf *Client, serverAddr string) *SharedClient { | ||
return &SharedClient{ | ||
refcount: 1, | ||
serverAddr: serverAddr, | ||
Client: conf, | ||
requests: make(chan request), | ||
} | ||
} | ||
|
||
// ExchangeShared dials a connection to the server on first invocation, and starts a handler | ||
// goroutines to send and receive responses, distributing them to appropriate concurrent caller | ||
// based on the DNS message Id. | ||
func (c *SharedClient) ExchangeShared(m *Msg) (r *Msg, rtt time.Duration, err error) { | ||
return c.ExchangeSharedContext(context.Background(), m) | ||
} | ||
|
||
// handler is started when the connection is dialed | ||
func handler(wg *sync.WaitGroup, client *Client, conn *Conn, requests chan request) { | ||
defer wg.Done() | ||
|
||
responses := make(chan sharedClientResponse) | ||
|
||
// Receive loop | ||
wg.Add(1) | ||
go func() { | ||
defer wg.Done() | ||
defer close(responses) | ||
for { | ||
r, err := conn.ReadMsg() | ||
if err != nil { | ||
// handler is not reading on the channel after closing | ||
if errors.Is(err, net.ErrClosed) { | ||
return | ||
} | ||
responses <- sharedClientResponse{nil, 0, err} | ||
} else { | ||
responses <- sharedClientResponse{r, 0, nil} | ||
} | ||
} | ||
}() | ||
|
||
type waiter struct { | ||
ch chan sharedClientResponse | ||
start time.Time | ||
} | ||
waitingResponses := make(map[uint16]waiter) | ||
defer func() { | ||
conn.Close() | ||
|
||
// Drain responses send by receive loop to allow it to exit. | ||
// It may be repeatedly reading after an i/o timeout, for example. | ||
for range responses { | ||
} | ||
|
||
for _, waiter := range waitingResponses { | ||
waiter.ch <- sharedClientResponse{nil, 0, net.ErrClosed} | ||
close(waiter.ch) | ||
} | ||
}() | ||
|
||
for { | ||
select { | ||
case req, ok := <-requests: | ||
if !ok { | ||
return | ||
} | ||
start := time.Now() | ||
err := client.SendContext(req.ctx, req.msg, conn, start) | ||
if err != nil { | ||
req.ch <- sharedClientResponse{nil, 0, err} | ||
close(req.ch) | ||
} else { | ||
waitingResponses[req.msg.Id] = waiter{req.ch, start} | ||
} | ||
|
||
case resp, ok := <-responses: | ||
if !ok { | ||
return | ||
} | ||
if resp.err != nil { | ||
// ReadMsg failed, but we cannot match it to a request, | ||
// so complete all pending requests. | ||
for _, waiter := range waitingResponses { | ||
waiter.ch <- sharedClientResponse{nil, 0, resp.err} | ||
close(waiter.ch) | ||
} | ||
waitingResponses = make(map[uint16]waiter) | ||
} else if resp.msg != nil { | ||
if waiter, ok := waitingResponses[resp.msg.Id]; ok { | ||
delete(waitingResponses, resp.msg.Id) | ||
resp.rtt = time.Since(waiter.start) | ||
waiter.ch <- resp | ||
close(waiter.ch) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
func (c *SharedClient) ExchangeSharedContext(ctx context.Context, m *Msg) (r *Msg, rtt time.Duration, err error) { | ||
c.Lock() | ||
if c.conn == nil { | ||
c.conn, err = c.DialContext(ctx, c.serverAddr) | ||
if err != nil { | ||
c.Unlock() | ||
return nil, 0, fmt.Errorf("failed to dial connection to %v: %w", c.serverAddr, err) | ||
} | ||
// Start handler for sending and receiving. | ||
c.wg.Add(1) | ||
go handler(&c.wg, c.Client, c.conn, c.requests) | ||
} | ||
c.Unlock() | ||
|
||
respCh := make(chan sharedClientResponse) | ||
c.requests <- request{ | ||
ctx: ctx, | ||
msg: m, | ||
ch: respCh, | ||
} | ||
resp := <-respCh | ||
return resp.msg, resp.rtt, resp.err | ||
} | ||
|
||
// close closes and waits for the close to finish. | ||
// Must be called while holding client's lock. | ||
func (c *SharedClient) close() { | ||
close(c.requests) | ||
c.wg.Wait() | ||
c.conn = nil | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we can get rid of these
wg.Add
andwg.Done()
here. That's becausehandler
can't exit untilresponses
is closed, and only after that thewg.Done()
deferred at the beginning of the function will be called.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems to me that handler can exit before
responses
is closed, for example ifrequests
is closed. I saw this happen in tests when I was working on getting rid of channel buffers (which can hide bugs).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right, I've missed that case. 👍