From c6d9c0e340b80cdc53ab078353519082a82cc243 Mon Sep 17 00:00:00 2001 From: idk Date: Thu, 15 Apr 2021 19:16:11 -0400 Subject: [PATCH] protect the dialer with a mutex --- client.go | 3 ++- dial.go | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/client.go b/client.go index 2c3b84b..d57c041 100644 --- a/client.go +++ b/client.go @@ -11,7 +11,7 @@ import ( "math/rand" "net" "strings" - // "sync" + "sync" "time" ) @@ -53,6 +53,7 @@ type Client struct { compression bool debug bool + mutex sync.Mutex //NEVER, EVER modify lastaddr or id yourself. They are used internally only. id int32 sammin int diff --git a/dial.go b/dial.go index 6933983..bb36b36 100644 --- a/dial.go +++ b/dial.go @@ -9,6 +9,8 @@ import ( // DialContext implements the net.DialContext function and can be used for http.Transport func (c *Client) DialContext(ctx context.Context, network, addr string) (net.Conn, error) { + c.mutex.Lock() + defer c.mutex.Unlock() errCh := make(chan error, 1) connCh := make(chan net.Conn, 1) go func() { @@ -29,6 +31,7 @@ func (c *Client) DialContext(ctx context.Context, network, addr string) (net.Con case <-ctx.Done(): return nil, ctx.Err() } + } func (c *Client) Dial(network, addr string) (net.Conn, error) {