Make config thread safe

This commit is contained in:
Elara 2023-10-06 15:07:19 -07:00
parent 88bd90ef89
commit eb8dd3ad35
3 changed files with 20 additions and 2 deletions

View File

@ -21,6 +21,7 @@ package config
import (
"context"
"os"
"sync"
"github.com/pelletier/go-toml/v2"
"go.elara.ws/lure/internal/types"
@ -39,13 +40,19 @@ var defaultConfig = &types.Config{
},
}
var config *types.Config
var (
configMtx sync.Mutex
config *types.Config
)
// Config returns a LURE configuration struct.
// The first time it's called, it'll load the config from a file.
// Subsequent calls will just return the same value.
func Config(ctx context.Context) *types.Config {
configMtx.Lock()
defer configMtx.Unlock()
log := loggerctx.From(ctx)
if config == nil {
cfgFl, err := os.Open(GetPaths(ctx).ConfigPath)
if err != nil {

View File

@ -22,12 +22,14 @@ import (
"context"
"os"
"strings"
"sync"
"go.elara.ws/lure/pkg/loggerctx"
"golang.org/x/text/language"
)
var (
langMtx sync.Mutex
lang language.Tag
langSet bool
)
@ -37,6 +39,8 @@ var (
// the $LANG environment variable.
// Subsequent calls will just return the same value.
func Language(ctx context.Context) language.Tag {
langMtx.Lock()
defer langMtx.Unlock()
log := loggerctx.From(ctx)
if !langSet {
syslang := SystemLang()

View File

@ -22,6 +22,7 @@ import (
"context"
"os"
"path/filepath"
"sync"
"github.com/pelletier/go-toml/v2"
"go.elara.ws/lure/pkg/loggerctx"
@ -37,13 +38,19 @@ type Paths struct {
DBPath string
}
var paths *Paths
var (
pathsMtx sync.Mutex
paths *Paths
)
// GetPaths returns a Paths struct.
// The first time it's called, it'll generate the struct
// using information from the system.
// Subsequent calls will return the same value.
func GetPaths(ctx context.Context) *Paths {
pathsMtx.Lock()
defer pathsMtx.Unlock()
log := loggerctx.From(ctx)
if paths == nil {
paths = &Paths{}