fix(storage): add mutex to in-memory

This adds a RW Mutex on the in-memory storage.
pull/613/head
Matt Butcher 9 years ago
parent ab906a4f27
commit 627a0e24e8

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

Loading…
Cancel
Save