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 ( import (
"context" "context"
"os" "os"
"sync"
"github.com/pelletier/go-toml/v2" "github.com/pelletier/go-toml/v2"
"go.elara.ws/lure/internal/types" "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. // Config returns a LURE configuration struct.
// The first time it's called, it'll load the config from a file. // The first time it's called, it'll load the config from a file.
// Subsequent calls will just return the same value. // Subsequent calls will just return the same value.
func Config(ctx context.Context) *types.Config { func Config(ctx context.Context) *types.Config {
configMtx.Lock()
defer configMtx.Unlock()
log := loggerctx.From(ctx) log := loggerctx.From(ctx)
if config == nil { if config == nil {
cfgFl, err := os.Open(GetPaths(ctx).ConfigPath) cfgFl, err := os.Open(GetPaths(ctx).ConfigPath)
if err != nil { if err != nil {

View File

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

View File

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