summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Thomas <nick@gitlab.com>2021-04-09 15:52:18 +0100
committerNick Thomas <nick@gitlab.com>2021-04-09 15:52:18 +0100
commit4af9308b126b282d9ca44c46dbb5ad01e98e0180 (patch)
treec6405e1bb6d26c332bac83665fe5696ec4629a34
parent88f94337bb87c0cc51f6badf7a4ff1826f25efaa (diff)
downloadgitlab-shell-518-fix-thread-safety.tar.gz
Fix thread-safety issues in gitlab-shell518-fix-thread-safety
-rw-r--r--internal/config/config.go44
-rw-r--r--internal/gitlabnet/client.go2
2 files changed, 23 insertions, 23 deletions
diff --git a/internal/config/config.go b/internal/config/config.go
index 36f8625..ad24b17 100644
--- a/internal/config/config.go
+++ b/internal/config/config.go
@@ -6,6 +6,7 @@ import (
"net/url"
"path"
"path/filepath"
+ "sync"
"gitlab.com/gitlab-org/gitlab-shell/client"
yaml "gopkg.in/yaml.v2"
@@ -46,21 +47,23 @@ type Config struct {
SslCertDir string `yaml:"ssl_cert_dir"`
HttpSettings HttpSettingsConfig `yaml:"http_settings"`
Server ServerConfig `yaml:"sshd"`
- HttpClient *client.HttpClient `-`
+
+ httpClient *client.HttpClient
+ httpClientOnce sync.Once
}
// The defaults to apply before parsing the config file(s).
var (
DefaultConfig = Config{
- LogFile: "gitlab-shell.log",
+ LogFile: "gitlab-shell.log",
LogFormat: "text",
- Server: DefaultServerConfig,
- User: "git",
+ Server: DefaultServerConfig,
+ User: "git",
}
DefaultServerConfig = ServerConfig{
- Listen: "[::]:22",
- WebListen: "localhost:9122",
+ Listen: "[::]:22",
+ WebListen: "localhost:9122",
ConcurrentSessionsLimit: 10,
HostKeyFiles: []string{
"/run/secrets/ssh-hostkeys/ssh_host_rsa_key",
@@ -70,22 +73,19 @@ var (
}
)
-func (c *Config) GetHttpClient() *client.HttpClient {
- if c.HttpClient != nil {
- return c.HttpClient
- }
-
- client := client.NewHTTPClient(
- c.GitlabUrl,
- c.GitlabRelativeURLRoot,
- c.HttpSettings.CaFile,
- c.HttpSettings.CaPath,
- c.HttpSettings.SelfSignedCert,
- c.HttpSettings.ReadTimeoutSeconds)
-
- c.HttpClient = client
-
- return client
+func (c *Config) HttpClient() *client.HttpClient {
+ c.httpClientOnce.Do(func() {
+ c.httpClient = client.NewHTTPClient(
+ c.GitlabUrl,
+ c.GitlabRelativeURLRoot,
+ c.HttpSettings.CaFile,
+ c.HttpSettings.CaPath,
+ c.HttpSettings.SelfSignedCert,
+ c.HttpSettings.ReadTimeoutSeconds,
+ )
+ })
+
+ return c.httpClient
}
// NewFromDirExternal returns a new config from a given root dir. It also applies defaults appropriate for
diff --git a/internal/gitlabnet/client.go b/internal/gitlabnet/client.go
index 18eac67..c0b72c4 100644
--- a/internal/gitlabnet/client.go
+++ b/internal/gitlabnet/client.go
@@ -15,7 +15,7 @@ var (
)
func GetClient(config *config.Config) (*client.GitlabNetClient, error) {
- httpClient := config.GetHttpClient()
+ httpClient := config.HttpClient()
if httpClient == nil {
return nil, fmt.Errorf("Unsupported protocol")