Skip to content

Commit

Permalink
Create Store in a thread safe manner (#60)
Browse files Browse the repository at this point in the history
* create Store in a thread safe manner
  • Loading branch information
nguyenhjustin authored and nishaad78 committed Oct 29, 2019
1 parent 66d0bfb commit 4afccfd
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,14 @@ type S struct {
var (
c *S
once sync.Once
mu sync.Mutex
)

// Init initiates the global config store with the given Config cfg
func Init(cfg *Config) {
mu.Lock()
defer mu.Unlock()

c = newStore(cfg)
}

Expand Down Expand Up @@ -362,9 +366,13 @@ func (c *S) RunHooks() error {

// Instance returns the singleton global config store
func Instance() Store {
mu.Lock()
defer mu.Unlock()

if c == nil {
c = newStore(DefaultConfig())
}

return c
}

Expand All @@ -385,16 +393,24 @@ func (c *S) stop() {
}

func instance() *S {
mu.Lock()
defer mu.Unlock()

if c == nil {
c = newStore(DefaultConfig())
}

return c
}

func reset() {
var cc = instance()
if cc != nil {
mu.Lock()
defer mu.Unlock()

if c != nil {
c = newStore(c.cfg)
} else {
c = newStore(DefaultConfig())
}
}

Expand Down

0 comments on commit 4afccfd

Please sign in to comment.