github.com/ojongerius/docker@v1.11.2/pkg/plugins/client.go (about)

     1  package plugins
     2  
     3  import (
     4  	"bytes"
     5  	"encoding/json"
     6  	"io"
     7  	"io/ioutil"
     8  	"net/http"
     9  	"net/url"
    10  	"time"
    11  
    12  	"github.com/Sirupsen/logrus"
    13  	"github.com/docker/docker/pkg/plugins/transport"
    14  	"github.com/docker/go-connections/sockets"
    15  	"github.com/docker/go-connections/tlsconfig"
    16  )
    17  
    18  const (
    19  	defaultTimeOut = 30
    20  )
    21  
    22  // NewClient creates a new plugin client (http).
    23  func NewClient(addr string, tlsConfig tlsconfig.Options) (*Client, error) {
    24  	tr := &http.Transport{}
    25  
    26  	c, err := tlsconfig.Client(tlsConfig)
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	tr.TLSClientConfig = c
    31  
    32  	u, err := url.Parse(addr)
    33  	if err != nil {
    34  		return nil, err
    35  	}
    36  	socket := u.Host
    37  	if socket == "" {
    38  		// valid local socket addresses have the host empty.
    39  		socket = u.Path
    40  	}
    41  	if err := sockets.ConfigureTransport(tr, u.Scheme, socket); err != nil {
    42  		return nil, err
    43  	}
    44  	scheme := httpScheme(u)
    45  
    46  	clientTransport := transport.NewHTTPTransport(tr, scheme, socket)
    47  	return NewClientWithTransport(clientTransport), nil
    48  }
    49  
    50  // NewClientWithTransport creates a new plugin client with a given transport.
    51  func NewClientWithTransport(tr transport.Transport) *Client {
    52  	return &Client{
    53  		http: &http.Client{
    54  			Transport: tr,
    55  		},
    56  		requestFactory: tr,
    57  	}
    58  }
    59  
    60  // Client represents a plugin client.
    61  type Client struct {
    62  	http           *http.Client // http client to use
    63  	requestFactory transport.RequestFactory
    64  }
    65  
    66  // Call calls the specified method with the specified arguments for the plugin.
    67  // It will retry for 30 seconds if a failure occurs when calling.
    68  func (c *Client) Call(serviceMethod string, args interface{}, ret interface{}) error {
    69  	var buf bytes.Buffer
    70  	if args != nil {
    71  		if err := json.NewEncoder(&buf).Encode(args); err != nil {
    72  			return err
    73  		}
    74  	}
    75  	body, err := c.callWithRetry(serviceMethod, &buf, true)
    76  	if err != nil {
    77  		return err
    78  	}
    79  	defer body.Close()
    80  	if ret != nil {
    81  		if err := json.NewDecoder(body).Decode(&ret); err != nil {
    82  			logrus.Errorf("%s: error reading plugin resp: %v", serviceMethod, err)
    83  			return err
    84  		}
    85  	}
    86  	return nil
    87  }
    88  
    89  // Stream calls the specified method with the specified arguments for the plugin and returns the response body
    90  func (c *Client) Stream(serviceMethod string, args interface{}) (io.ReadCloser, error) {
    91  	var buf bytes.Buffer
    92  	if err := json.NewEncoder(&buf).Encode(args); err != nil {
    93  		return nil, err
    94  	}
    95  	return c.callWithRetry(serviceMethod, &buf, true)
    96  }
    97  
    98  // SendFile calls the specified method, and passes through the IO stream
    99  func (c *Client) SendFile(serviceMethod string, data io.Reader, ret interface{}) error {
   100  	body, err := c.callWithRetry(serviceMethod, data, true)
   101  	if err != nil {
   102  		return err
   103  	}
   104  	defer body.Close()
   105  	if err := json.NewDecoder(body).Decode(&ret); err != nil {
   106  		logrus.Errorf("%s: error reading plugin resp: %v", serviceMethod, err)
   107  		return err
   108  	}
   109  	return nil
   110  }
   111  
   112  func (c *Client) callWithRetry(serviceMethod string, data io.Reader, retry bool) (io.ReadCloser, error) {
   113  	req, err := c.requestFactory.NewRequest(serviceMethod, data)
   114  	if err != nil {
   115  		return nil, err
   116  	}
   117  
   118  	var retries int
   119  	start := time.Now()
   120  
   121  	for {
   122  		resp, err := c.http.Do(req)
   123  		if err != nil {
   124  			if !retry {
   125  				return nil, err
   126  			}
   127  
   128  			timeOff := backoff(retries)
   129  			if abort(start, timeOff) {
   130  				return nil, err
   131  			}
   132  			retries++
   133  			logrus.Warnf("Unable to connect to plugin: %s:%s, retrying in %v", req.URL.Host, req.URL.Path, timeOff)
   134  			time.Sleep(timeOff)
   135  			continue
   136  		}
   137  
   138  		if resp.StatusCode != http.StatusOK {
   139  			b, err := ioutil.ReadAll(resp.Body)
   140  			resp.Body.Close()
   141  			if err != nil {
   142  				return nil, &statusError{resp.StatusCode, serviceMethod, err.Error()}
   143  			}
   144  
   145  			// Plugins' Response(s) should have an Err field indicating what went
   146  			// wrong. Try to unmarshal into ResponseErr. Otherwise fallback to just
   147  			// return the string(body)
   148  			type responseErr struct {
   149  				Err string
   150  			}
   151  			remoteErr := responseErr{}
   152  			if err := json.Unmarshal(b, &remoteErr); err == nil {
   153  				if remoteErr.Err != "" {
   154  					return nil, &statusError{resp.StatusCode, serviceMethod, remoteErr.Err}
   155  				}
   156  			}
   157  			// old way...
   158  			return nil, &statusError{resp.StatusCode, serviceMethod, string(b)}
   159  		}
   160  		return resp.Body, nil
   161  	}
   162  }
   163  
   164  func backoff(retries int) time.Duration {
   165  	b, max := 1, defaultTimeOut
   166  	for b < max && retries > 0 {
   167  		b *= 2
   168  		retries--
   169  	}
   170  	if b > max {
   171  		b = max
   172  	}
   173  	return time.Duration(b) * time.Second
   174  }
   175  
   176  func abort(start time.Time, timeOff time.Duration) bool {
   177  	return timeOff+time.Since(start) >= time.Duration(defaultTimeOut)*time.Second
   178  }
   179  
   180  func httpScheme(u *url.URL) string {
   181  	scheme := u.Scheme
   182  	if scheme != "https" {
   183  		scheme = "http"
   184  	}
   185  	return scheme
   186  }