Make Client safe for concurrent use

This commit is contained in:
Adnan Maolood 2020-11-24 16:28:58 -05:00
parent 0c75e5d5ad
commit 4b653032e4

View File

@ -9,10 +9,13 @@ import (
"net/url" "net/url"
"path" "path"
"strings" "strings"
"sync"
"time" "time"
) )
// Client is a Gemini client. // Client is a Gemini client.
//
// Clients are safe for concurrent use by multiple goroutines.
type Client struct { type Client struct {
// KnownHosts is a list of known hosts. // KnownHosts is a list of known hosts.
KnownHosts KnownHostsFile KnownHosts KnownHostsFile
@ -59,6 +62,8 @@ type Client struct {
// If TrustCertificate returns TrustAlways, the certificate will also be // If TrustCertificate returns TrustAlways, the certificate will also be
// written to the known hosts file. // written to the known hosts file.
TrustCertificate func(hostname string, cert *x509.Certificate) Trust TrustCertificate func(hostname string, cert *x509.Certificate) Trust
mu sync.Mutex
} }
// Get performs a Gemini request for the given url. // Get performs a Gemini request for the given url.
@ -76,6 +81,9 @@ func (c *Client) Do(req *Request) (*Response, error) {
} }
func (c *Client) do(req *Request, via []*Request) (*Response, error) { func (c *Client) do(req *Request, via []*Request) (*Response, error) {
c.mu.Lock()
defer c.mu.Unlock()
// Connect to the host // Connect to the host
config := &tls.Config{ config := &tls.Config{
InsecureSkipVerify: true, InsecureSkipVerify: true,