2014-10-21 22:02:20 +00:00
|
|
|
package ipc
|
|
|
|
|
|
|
|
import (
|
2014-10-25 01:33:23 +00:00
|
|
|
"bytes"
|
2014-10-21 22:02:20 +00:00
|
|
|
"encoding/json"
|
2014-11-06 22:06:16 +00:00
|
|
|
"fmt"
|
2014-10-21 22:02:20 +00:00
|
|
|
"io"
|
2014-10-25 01:33:23 +00:00
|
|
|
"io/ioutil"
|
2014-10-21 22:02:20 +00:00
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"os/exec"
|
|
|
|
"syscall"
|
|
|
|
|
2014-11-06 20:16:14 +00:00
|
|
|
"github.com/docker/docker-registry/storagedriver"
|
2014-10-21 22:02:20 +00:00
|
|
|
"github.com/docker/libchan"
|
|
|
|
"github.com/docker/libchan/spdy"
|
|
|
|
)
|
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
// StorageDriverExecutablePrefix is the prefix which the IPC storage driver
|
|
|
|
// loader expects driver executables to begin with. For example, the s3 driver
|
|
|
|
// should be named "registry-storagedriver-s3".
|
2014-11-06 18:35:15 +00:00
|
|
|
const StorageDriverExecutablePrefix = "registry-storagedriver-"
|
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
// StorageDriverClient is a storagedriver.StorageDriver implementation using a
|
|
|
|
// managed child process communicating over IPC using libchan with a unix domain
|
|
|
|
// socket
|
2014-10-21 22:02:20 +00:00
|
|
|
type StorageDriverClient struct {
|
|
|
|
subprocess *exec.Cmd
|
2014-11-06 22:06:16 +00:00
|
|
|
exitChan chan error
|
|
|
|
exitErr error
|
|
|
|
stopChan chan struct{}
|
2014-10-21 22:02:20 +00:00
|
|
|
socket *os.File
|
|
|
|
transport *spdy.Transport
|
|
|
|
sender libchan.Sender
|
2014-11-06 20:16:14 +00:00
|
|
|
version storagedriver.Version
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
// NewDriverClient constructs a new out-of-process storage driver using the
|
|
|
|
// driver name and configuration parameters
|
|
|
|
// A user must call Start on this driver client before remote method calls can
|
|
|
|
// be made
|
2014-10-29 01:15:40 +00:00
|
|
|
//
|
|
|
|
// Looks for drivers in the following locations in order:
|
|
|
|
// - Storage drivers directory (to be determined, yet not implemented)
|
|
|
|
// - $GOPATH/bin
|
|
|
|
// - $PATH
|
2014-10-21 22:02:20 +00:00
|
|
|
func NewDriverClient(name string, parameters map[string]string) (*StorageDriverClient, error) {
|
|
|
|
paramsBytes, err := json.Marshal(parameters)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-11-06 18:35:15 +00:00
|
|
|
driverExecName := StorageDriverExecutablePrefix + name
|
|
|
|
driverPath, err := exec.LookPath(driverExecName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
command := exec.Command(driverPath, string(paramsBytes))
|
|
|
|
|
|
|
|
return &StorageDriverClient{
|
|
|
|
subprocess: command,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
// Start starts the designated child process storage driver and binds a socket
|
|
|
|
// to this process for IPC method calls
|
2014-10-21 22:02:20 +00:00
|
|
|
func (driver *StorageDriverClient) Start() error {
|
2014-11-06 22:06:16 +00:00
|
|
|
driver.exitErr = nil
|
|
|
|
driver.exitChan = make(chan error)
|
|
|
|
driver.stopChan = make(chan struct{})
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
fileDescriptors, err := syscall.Socketpair(syscall.AF_LOCAL, syscall.SOCK_STREAM, 0)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
childSocket := os.NewFile(uintptr(fileDescriptors[0]), "childSocket")
|
2014-11-06 20:16:14 +00:00
|
|
|
driver.socket = os.NewFile(uintptr(fileDescriptors[1]), "parentSocket")
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
driver.subprocess.Stdout = os.Stdout
|
|
|
|
driver.subprocess.Stderr = os.Stderr
|
|
|
|
driver.subprocess.ExtraFiles = []*os.File{childSocket}
|
|
|
|
|
|
|
|
if err = driver.subprocess.Start(); err != nil {
|
2014-11-06 20:16:14 +00:00
|
|
|
driver.Stop()
|
2014-10-21 22:02:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
go driver.handleSubprocessExit()
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
if err = childSocket.Close(); err != nil {
|
2014-11-06 20:16:14 +00:00
|
|
|
driver.Stop()
|
2014-10-21 22:02:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-06 20:16:14 +00:00
|
|
|
connection, err := net.FileConn(driver.socket)
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
2014-11-06 20:16:14 +00:00
|
|
|
driver.Stop()
|
2014-10-21 22:02:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-11-06 20:16:14 +00:00
|
|
|
driver.transport, err = spdy.NewClientTransport(connection)
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
2014-11-06 20:16:14 +00:00
|
|
|
driver.Stop()
|
2014-10-21 22:02:20 +00:00
|
|
|
return err
|
|
|
|
}
|
2014-11-06 20:16:14 +00:00
|
|
|
driver.sender, err = driver.transport.NewSendChannel()
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
2014-11-06 20:16:14 +00:00
|
|
|
driver.Stop()
|
2014-10-21 22:02:20 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-06 20:16:14 +00:00
|
|
|
// Check the driver's version to determine compatibility
|
|
|
|
receiver, remoteSender := libchan.Pipe()
|
|
|
|
err = driver.sender.Send(&Request{Type: "Version", ResponseChannel: remoteSender})
|
|
|
|
if err != nil {
|
|
|
|
driver.Stop()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response VersionResponse
|
|
|
|
err = receiver.Receive(&response)
|
|
|
|
if err != nil {
|
|
|
|
driver.Stop()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Error != nil {
|
2014-11-19 01:41:48 +00:00
|
|
|
return response.Error.Unwrap()
|
2014-11-06 20:16:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
driver.version = response.Version
|
|
|
|
|
|
|
|
if driver.version.Major() != storagedriver.CurrentVersion.Major() || driver.version.Minor() > storagedriver.CurrentVersion.Minor() {
|
|
|
|
return IncompatibleVersionError{driver.version}
|
|
|
|
}
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-10-29 19:14:19 +00:00
|
|
|
// Stop stops the child process storage driver
|
|
|
|
// storagedriver.StorageDriver methods called after Stop will fail
|
2014-10-21 22:02:20 +00:00
|
|
|
func (driver *StorageDriverClient) Stop() error {
|
2014-11-06 20:16:14 +00:00
|
|
|
var closeSenderErr, closeTransportErr, closeSocketErr, killErr error
|
|
|
|
|
|
|
|
if driver.sender != nil {
|
|
|
|
closeSenderErr = driver.sender.Close()
|
|
|
|
}
|
|
|
|
if driver.transport != nil {
|
|
|
|
closeTransportErr = driver.transport.Close()
|
|
|
|
}
|
|
|
|
if driver.socket != nil {
|
|
|
|
closeSocketErr = driver.socket.Close()
|
|
|
|
}
|
|
|
|
if driver.subprocess != nil {
|
|
|
|
killErr = driver.subprocess.Process.Kill()
|
|
|
|
}
|
2014-11-06 22:06:16 +00:00
|
|
|
if driver.stopChan != nil {
|
|
|
|
driver.stopChan <- struct{}{}
|
|
|
|
close(driver.stopChan)
|
|
|
|
}
|
2014-10-21 22:02:20 +00:00
|
|
|
|
|
|
|
if closeSenderErr != nil {
|
|
|
|
return closeSenderErr
|
|
|
|
} else if closeTransportErr != nil {
|
|
|
|
return closeTransportErr
|
|
|
|
} else if closeSocketErr != nil {
|
|
|
|
return closeSocketErr
|
|
|
|
}
|
2014-11-06 22:06:16 +00:00
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
return killErr
|
|
|
|
}
|
|
|
|
|
2014-10-29 01:15:40 +00:00
|
|
|
// Implement the storagedriver.StorageDriver interface over IPC
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// GetContent retrieves the content stored at "path" as a []byte.
|
2014-10-21 22:02:20 +00:00
|
|
|
func (driver *StorageDriverClient) GetContent(path string) ([]byte, error) {
|
2014-11-06 22:06:16 +00:00
|
|
|
if err := driver.exited(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
receiver, remoteSender := libchan.Pipe()
|
|
|
|
|
|
|
|
params := map[string]interface{}{"Path": path}
|
|
|
|
err := driver.sender.Send(&Request{Type: "GetContent", Parameters: params, ResponseChannel: remoteSender})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
response := new(ReadStreamResponse)
|
|
|
|
err = driver.receiveResponse(receiver, response)
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Error != nil {
|
2014-11-19 01:41:48 +00:00
|
|
|
return nil, response.Error.Unwrap()
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-10-25 01:33:23 +00:00
|
|
|
defer response.Reader.Close()
|
|
|
|
contents, err := ioutil.ReadAll(response.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return contents, nil
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// PutContent stores the []byte content at a location designated by "path".
|
2014-10-21 22:02:20 +00:00
|
|
|
func (driver *StorageDriverClient) PutContent(path string, contents []byte) error {
|
2014-11-06 22:06:16 +00:00
|
|
|
if err := driver.exited(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-10-21 22:02:20 +00:00
|
|
|
receiver, remoteSender := libchan.Pipe()
|
|
|
|
|
2014-10-31 18:50:02 +00:00
|
|
|
params := map[string]interface{}{"Path": path, "Reader": ioutil.NopCloser(bytes.NewReader(contents))}
|
2014-10-21 22:02:20 +00:00
|
|
|
err := driver.sender.Send(&Request{Type: "PutContent", Parameters: params, ResponseChannel: remoteSender})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
response := new(WriteStreamResponse)
|
|
|
|
err = driver.receiveResponse(receiver, response)
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Error != nil {
|
2014-11-19 01:41:48 +00:00
|
|
|
return response.Error.Unwrap()
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// ReadStream retrieves an io.ReadCloser for the content stored at "path" with a
|
|
|
|
// given byte offset.
|
2014-10-21 22:02:20 +00:00
|
|
|
func (driver *StorageDriverClient) ReadStream(path string, offset uint64) (io.ReadCloser, error) {
|
2014-11-06 22:06:16 +00:00
|
|
|
if err := driver.exited(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
receiver, remoteSender := libchan.Pipe()
|
2014-10-21 22:02:20 +00:00
|
|
|
params := map[string]interface{}{"Path": path, "Offset": offset}
|
|
|
|
err := driver.sender.Send(&Request{Type: "ReadStream", Parameters: params, ResponseChannel: remoteSender})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
response := new(ReadStreamResponse)
|
|
|
|
err = driver.receiveResponse(receiver, response)
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Error != nil {
|
2014-11-19 01:41:48 +00:00
|
|
|
return nil, response.Error.Unwrap()
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.Reader, nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// WriteStream stores the contents of the provided io.ReadCloser at a location
|
|
|
|
// designated by the given path.
|
2014-10-21 22:02:20 +00:00
|
|
|
func (driver *StorageDriverClient) WriteStream(path string, offset, size uint64, reader io.ReadCloser) error {
|
2014-11-06 22:06:16 +00:00
|
|
|
if err := driver.exited(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
receiver, remoteSender := libchan.Pipe()
|
2014-11-20 22:11:49 +00:00
|
|
|
params := map[string]interface{}{"Path": path, "Offset": offset, "Size": size, "Reader": reader}
|
2014-10-21 22:02:20 +00:00
|
|
|
err := driver.sender.Send(&Request{Type: "WriteStream", Parameters: params, ResponseChannel: remoteSender})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
response := new(WriteStreamResponse)
|
|
|
|
err = driver.receiveResponse(receiver, response)
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Error != nil {
|
2014-11-19 01:41:48 +00:00
|
|
|
return response.Error.Unwrap()
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// CurrentSize retrieves the curernt size in bytes of the object at the given
|
|
|
|
// path.
|
2014-11-07 20:58:48 +00:00
|
|
|
func (driver *StorageDriverClient) CurrentSize(path string) (uint64, error) {
|
2014-11-06 22:06:16 +00:00
|
|
|
if err := driver.exited(); err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
receiver, remoteSender := libchan.Pipe()
|
2014-10-21 22:02:20 +00:00
|
|
|
params := map[string]interface{}{"Path": path}
|
2014-11-07 20:58:48 +00:00
|
|
|
err := driver.sender.Send(&Request{Type: "CurrentSize", Parameters: params, ResponseChannel: remoteSender})
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
response := new(CurrentSizeResponse)
|
|
|
|
err = driver.receiveResponse(receiver, response)
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Error != nil {
|
2014-11-19 01:41:48 +00:00
|
|
|
return 0, response.Error.Unwrap()
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.Position, nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// List returns a list of the objects that are direct descendants of the given
|
|
|
|
// path.
|
2014-11-04 17:52:24 +00:00
|
|
|
func (driver *StorageDriverClient) List(path string) ([]string, error) {
|
2014-11-06 22:06:16 +00:00
|
|
|
if err := driver.exited(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
receiver, remoteSender := libchan.Pipe()
|
2014-11-04 17:52:24 +00:00
|
|
|
params := map[string]interface{}{"Path": path}
|
2014-10-21 22:02:20 +00:00
|
|
|
err := driver.sender.Send(&Request{Type: "List", Parameters: params, ResponseChannel: remoteSender})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
response := new(ListResponse)
|
|
|
|
err = driver.receiveResponse(receiver, response)
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Error != nil {
|
2014-11-19 01:41:48 +00:00
|
|
|
return nil, response.Error.Unwrap()
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.Keys, nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Move moves an object stored at sourcePath to destPath, removing the original
|
|
|
|
// object.
|
2014-10-21 22:02:20 +00:00
|
|
|
func (driver *StorageDriverClient) Move(sourcePath string, destPath string) error {
|
2014-11-06 22:06:16 +00:00
|
|
|
if err := driver.exited(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
receiver, remoteSender := libchan.Pipe()
|
2014-10-21 22:02:20 +00:00
|
|
|
params := map[string]interface{}{"SourcePath": sourcePath, "DestPath": destPath}
|
|
|
|
err := driver.sender.Send(&Request{Type: "Move", Parameters: params, ResponseChannel: remoteSender})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
response := new(MoveResponse)
|
|
|
|
err = driver.receiveResponse(receiver, response)
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Error != nil {
|
2014-11-19 01:41:48 +00:00
|
|
|
return response.Error.Unwrap()
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-11-17 23:44:07 +00:00
|
|
|
// Delete recursively deletes all objects stored at "path" and its subpaths.
|
2014-10-21 22:02:20 +00:00
|
|
|
func (driver *StorageDriverClient) Delete(path string) error {
|
2014-11-06 22:06:16 +00:00
|
|
|
if err := driver.exited(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-10-21 22:02:20 +00:00
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
receiver, remoteSender := libchan.Pipe()
|
2014-10-21 22:02:20 +00:00
|
|
|
params := map[string]interface{}{"Path": path}
|
|
|
|
err := driver.sender.Send(&Request{Type: "Delete", Parameters: params, ResponseChannel: remoteSender})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-11-06 22:06:16 +00:00
|
|
|
response := new(DeleteResponse)
|
|
|
|
err = driver.receiveResponse(receiver, response)
|
2014-10-21 22:02:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if response.Error != nil {
|
2014-11-19 01:41:48 +00:00
|
|
|
return response.Error.Unwrap()
|
2014-10-21 22:02:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2014-11-06 22:06:16 +00:00
|
|
|
|
|
|
|
// handleSubprocessExit populates the exit channel until we have explicitly
|
|
|
|
// stopped the storage driver subprocess
|
|
|
|
// Requests can select on driver.exitChan and response receiving and not hang if
|
|
|
|
// the process exits
|
|
|
|
func (driver *StorageDriverClient) handleSubprocessExit() {
|
|
|
|
exitErr := driver.subprocess.Wait()
|
|
|
|
if exitErr == nil {
|
|
|
|
exitErr = fmt.Errorf("Storage driver subprocess already exited cleanly")
|
|
|
|
} else {
|
|
|
|
exitErr = fmt.Errorf("Storage driver subprocess exited with error: %s", exitErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
driver.exitErr = exitErr
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case driver.exitChan <- exitErr:
|
|
|
|
case <-driver.stopChan:
|
|
|
|
close(driver.exitChan)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// receiveResponse populates the response value with the next result from the
|
|
|
|
// given receiver, or returns an error if receiving failed or the driver has
|
|
|
|
// stopped
|
|
|
|
func (driver *StorageDriverClient) receiveResponse(receiver libchan.Receiver, response interface{}) error {
|
|
|
|
receiveChan := make(chan error, 1)
|
|
|
|
go func(receiveChan chan<- error) {
|
|
|
|
defer close(receiveChan)
|
|
|
|
receiveChan <- receiver.Receive(response)
|
|
|
|
}(receiveChan)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
var ok bool
|
|
|
|
select {
|
|
|
|
case err = <-receiveChan:
|
|
|
|
case err, ok = <-driver.exitChan:
|
|
|
|
go func(receiveChan <-chan error) {
|
|
|
|
<-receiveChan
|
|
|
|
}(receiveChan)
|
|
|
|
if !ok {
|
|
|
|
err = driver.exitErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// exited returns an exit error if the driver has exited or nil otherwise
|
|
|
|
func (driver *StorageDriverClient) exited() error {
|
|
|
|
select {
|
|
|
|
case err, ok := <-driver.exitChan:
|
|
|
|
if !ok {
|
|
|
|
return driver.exitErr
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|