Skip to content
Snippets Groups Projects
Commit 73272f8b authored by Jonah Husson's avatar Jonah Husson
Browse files

Update with timeout and locking fix

parent ef04339f
Branches origin-fork
No related tags found
No related merge requests found
...@@ -22,4 +22,6 @@ type ConnectOptions struct { ...@@ -22,4 +22,6 @@ type ConnectOptions struct {
ExpectContinueTimeout time.Duration ExpectContinueTimeout time.Duration
// Skip standard tls certificate verifications // Skip standard tls certificate verifications
TlsInsecureSkipVerify bool TlsInsecureSkipVerify bool
Timeout time.Duration
} }
...@@ -18,6 +18,7 @@ import ( ...@@ -18,6 +18,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"sync" "sync"
"time"
) )
// UnaryTransport is the public interface for the transport package // UnaryTransport is the public interface for the transport package
...@@ -30,7 +31,7 @@ type UnaryTransport interface { ...@@ -30,7 +31,7 @@ type UnaryTransport interface {
type httpTransport struct { type httpTransport struct {
host string host string
client *http.Client client *http.Client
clientLock *sync.Mutex clientLock *sync.RWMutex
opts *ConnectOptions opts *ConnectOptions
header http.Header header http.Header
...@@ -68,8 +69,8 @@ func (t *httpTransport) Send(ctx context.Context, endpoint, contentType string, ...@@ -68,8 +69,8 @@ func (t *httpTransport) Send(ctx context.Context, endpoint, contentType string,
req.Header.Add("content-type", contentType) req.Header.Add("content-type", contentType)
req.Header.Add("x-grpc-web", "1") req.Header.Add("x-grpc-web", "1")
t.clientLock.Lock() t.clientLock.RLock()
defer t.clientLock.Unlock() defer t.clientLock.RUnlock()
res, err := t.client.Do(req) res, err := t.client.Do(req)
if err != nil { if err != nil {
return nil, nil, errors.Wrap(err, "failed to send the API") return nil, nil, errors.Wrap(err, "failed to send the API")
...@@ -126,6 +127,11 @@ var NewUnary = func(host string, opts *ConnectOptions) UnaryTransport { ...@@ -126,6 +127,11 @@ var NewUnary = func(host string, opts *ConnectOptions) UnaryTransport {
transport.TLSClientConfig = &tls.Config{RootCAs: certPool, ServerName: cert.DNSNames[0]} transport.TLSClientConfig = &tls.Config{RootCAs: certPool, ServerName: cert.DNSNames[0]}
transport.TLSClientConfig.InsecureSkipVerify = opts.TlsInsecureSkipVerify transport.TLSClientConfig.InsecureSkipVerify = opts.TlsInsecureSkipVerify
} }
if opts.Timeout != 0 {
cl.Timeout = time.Second
} else {
cl.Timeout = opts.Timeout
}
if opts.IdleConnTimeout != 0 { if opts.IdleConnTimeout != 0 {
transport.IdleConnTimeout = opts.IdleConnTimeout transport.IdleConnTimeout = opts.IdleConnTimeout
...@@ -145,7 +151,7 @@ var NewUnary = func(host string, opts *ConnectOptions) UnaryTransport { ...@@ -145,7 +151,7 @@ var NewUnary = func(host string, opts *ConnectOptions) UnaryTransport {
client: cl, client: cl,
opts: opts, opts: opts,
header: make(http.Header), header: make(http.Header),
clientLock: &sync.Mutex{}, clientLock: &sync.RWMutex{},
} }
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment