client.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package libcontainerd
  2. import (
  3. "fmt"
  4. "sync"
  5. "github.com/docker/docker/pkg/locker"
  6. )
  7. // clientCommon contains the platform agnostic fields used in the client structure
  8. type clientCommon struct {
  9. backend Backend
  10. containers map[string]*container
  11. locker *locker.Locker
  12. mapMutex sync.RWMutex // protects read/write oprations from containers map
  13. }
  14. func (clnt *client) lock(containerID string) {
  15. clnt.locker.Lock(containerID)
  16. }
  17. func (clnt *client) unlock(containerID string) {
  18. clnt.locker.Unlock(containerID)
  19. }
  20. // must hold a lock for cont.containerID
  21. func (clnt *client) appendContainer(cont *container) {
  22. clnt.mapMutex.Lock()
  23. clnt.containers[cont.containerID] = cont
  24. clnt.mapMutex.Unlock()
  25. }
  26. func (clnt *client) deleteContainer(friendlyName string) {
  27. clnt.mapMutex.Lock()
  28. delete(clnt.containers, friendlyName)
  29. clnt.mapMutex.Unlock()
  30. }
  31. func (clnt *client) getContainer(containerID string) (*container, error) {
  32. clnt.mapMutex.RLock()
  33. container, ok := clnt.containers[containerID]
  34. defer clnt.mapMutex.RUnlock()
  35. if !ok {
  36. return nil, fmt.Errorf("invalid container: %s", containerID) // fixme: typed error
  37. }
  38. return container, nil
  39. }