Merge pull request #56 from technosophos/fix/memory-storage-mutex

fix(storage): add mutex to in-memory
pull/613/head
Matt Butcher 9 years ago
commit a0aac1ad73

@ -2,12 +2,14 @@ package storage
import ( import (
"errors" "errors"
"sync"
"github.com/deis/tiller/pkg/proto/hapi/release" "github.com/deis/tiller/pkg/proto/hapi/release"
) )
// Memory is an in-memory ReleaseStorage implementation. // Memory is an in-memory ReleaseStorage implementation.
type Memory struct { type Memory struct {
sync.RWMutex
releases map[string]*release.Release releases map[string]*release.Release
} }
@ -25,6 +27,8 @@ var ErrNotFound = errors.New("release not found")
// //
// If the release is not found, an ErrNotFound error is returned. // If the release is not found, an ErrNotFound error is returned.
func (m *Memory) Read(k string) (*release.Release, error) { func (m *Memory) Read(k string) (*release.Release, error) {
m.RLock()
defer m.RUnlock()
v, ok := m.releases[k] v, ok := m.releases[k]
if !ok { if !ok {
return v, ErrNotFound return v, ErrNotFound
@ -34,12 +38,16 @@ func (m *Memory) Read(k string) (*release.Release, error) {
// Create sets a release. // Create sets a release.
func (m *Memory) Create(rel *release.Release) error { func (m *Memory) Create(rel *release.Release) error {
m.Lock()
defer m.Unlock()
m.releases[rel.Name] = rel m.releases[rel.Name] = rel
return nil return nil
} }
// Update sets a release. // Update sets a release.
func (m *Memory) Update(rel *release.Release) error { func (m *Memory) Update(rel *release.Release) error {
m.Lock()
defer m.Unlock()
if _, ok := m.releases[rel.Name]; !ok { if _, ok := m.releases[rel.Name]; !ok {
return ErrNotFound return ErrNotFound
} }
@ -52,6 +60,8 @@ func (m *Memory) Update(rel *release.Release) error {
// Delete removes a release. // Delete removes a release.
func (m *Memory) Delete(name string) (*release.Release, error) { func (m *Memory) Delete(name string) (*release.Release, error) {
m.Lock()
defer m.Unlock()
rel, ok := m.releases[name] rel, ok := m.releases[name]
if !ok { if !ok {
return nil, ErrNotFound return nil, ErrNotFound
@ -62,6 +72,8 @@ func (m *Memory) Delete(name string) (*release.Release, error) {
// List returns all releases. // List returns all releases.
func (m *Memory) List() ([]*release.Release, error) { func (m *Memory) List() ([]*release.Release, error) {
m.RLock()
defer m.RUnlock()
buf := make([]*release.Release, len(m.releases)) buf := make([]*release.Release, len(m.releases))
i := 0 i := 0
for _, v := range m.releases { for _, v := range m.releases {
@ -73,5 +85,7 @@ func (m *Memory) List() ([]*release.Release, error) {
// Query searches all releases for matches. // Query searches all releases for matches.
func (m *Memory) Query(labels map[string]string) ([]*release.Release, error) { func (m *Memory) Query(labels map[string]string) ([]*release.Release, error) {
m.RLock()
defer m.RUnlock()
return []*release.Release{}, errors.New("Cannot implement until release.Release is defined.") return []*release.Release{}, errors.New("Cannot implement until release.Release is defined.")
} }

Loading…
Cancel
Save