diff --git a/state/ephemeral.go b/state/ephemeral.go index e01ac90f88824fbbc1595ead4a31c9ec394f03e3..0d3704074847b6e350796ce6783a66587b73e48a 100644 --- a/state/ephemeral.go +++ b/state/ephemeral.go @@ -2,17 +2,20 @@ package state import ( "errors" + "sync" ) type Ephemeral struct { - store map[ string ] *Item + store map[ string ] Item + mux sync.Mutex } func NewEphemeralStore() *Ephemeral { return &Ephemeral{ - store: map[ string ] *Item {}, + store: map[ string ] Item {}, + mux: sync.Mutex{}, } } @@ -23,7 +26,11 @@ func ( e *Ephemeral ) Add( i *Item ) error { } name := i.Name() + + e.mux.Lock() e.store[ name ] = i + e.mux.Unlock() + return nil } @@ -33,7 +40,10 @@ func ( e *Ephemeral ) Remove( name string ) error { return errors.New( "ephemeral storage not available" ) } + e.mux.Lock() delete( e.store, name ) + e.mux.Unlock() + return nil } @@ -43,7 +53,10 @@ func ( e *Ephemeral ) Fetch( name string ) ( *Item, error ) { return nil, errors.New( "ephemeral storage not available" ) } + e.mux.Lock() item, found := e.store[ name ] + e.mux.Unlock() + if !found { return nil, nil } @@ -56,10 +69,13 @@ func ( e *Ephemeral ) Show() ( []string, error ) { return nil, errors.New( "ephemeral storage not available" ) } + e.mux.Lock() names := make( []string, 0, len( e.store ) ) for k := range e.store { names = append( names, k ) } + e.mux.Unlock() + return names, nil }