2015-05-14 17:05:39 +00:00
|
|
|
package plugins
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2015-05-16 15:49:23 +00:00
|
|
|
versionMimetype = "application/vnd.docker.plugins.v1+json"
|
2015-05-15 11:07:59 +00:00
|
|
|
defaultTimeOut = 30
|
2015-05-14 17:05:39 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewClient(addr string) *Client {
|
|
|
|
tr := &http.Transport{}
|
|
|
|
protoAndAddr := strings.Split(addr, "://")
|
|
|
|
configureTCPTransport(tr, protoAndAddr[0], protoAndAddr[1])
|
|
|
|
return &Client{&http.Client{Transport: tr}, protoAndAddr[1]}
|
|
|
|
}
|
|
|
|
|
|
|
|
type Client struct {
|
|
|
|
http *http.Client
|
|
|
|
addr string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Call(serviceMethod string, args interface{}, ret interface{}) error {
|
2015-05-19 20:05:25 +00:00
|
|
|
return c.callWithRetry(serviceMethod, args, ret, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) callWithRetry(serviceMethod string, args interface{}, ret interface{}, retry bool) error {
|
2015-05-14 17:05:39 +00:00
|
|
|
var buf bytes.Buffer
|
|
|
|
if err := json.NewEncoder(&buf).Encode(args); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
req, err := http.NewRequest("POST", "/"+serviceMethod, &buf)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
req.Header.Add("Accept", versionMimetype)
|
|
|
|
req.URL.Scheme = "http"
|
|
|
|
req.URL.Host = c.addr
|
|
|
|
|
|
|
|
var retries int
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
for {
|
|
|
|
resp, err := c.http.Do(req)
|
|
|
|
if err != nil {
|
2015-05-19 20:05:25 +00:00
|
|
|
if !retry {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-05-14 17:05:39 +00:00
|
|
|
timeOff := backoff(retries)
|
2015-05-19 20:05:25 +00:00
|
|
|
if abort(start, timeOff) {
|
2015-05-14 17:05:39 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
retries++
|
2015-05-19 20:05:25 +00:00
|
|
|
logrus.Warnf("Unable to connect to plugin: %s, retrying in %v", c.addr, timeOff)
|
2015-05-14 17:05:39 +00:00
|
|
|
time.Sleep(timeOff)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
|
|
remoteErr, err := ioutil.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return fmt.Errorf("Plugin Error: %s", remoteErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.NewDecoder(resp.Body).Decode(&ret)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func backoff(retries int) time.Duration {
|
2015-05-19 20:05:25 +00:00
|
|
|
b, max := 1, defaultTimeOut
|
2015-05-14 17:05:39 +00:00
|
|
|
for b < max && retries > 0 {
|
|
|
|
b *= 2
|
|
|
|
retries--
|
|
|
|
}
|
|
|
|
if b > max {
|
|
|
|
b = max
|
|
|
|
}
|
2015-05-19 20:05:25 +00:00
|
|
|
return time.Duration(b) * time.Second
|
|
|
|
}
|
|
|
|
|
|
|
|
func abort(start time.Time, timeOff time.Duration) bool {
|
|
|
|
return timeOff+time.Since(start) > time.Duration(defaultTimeOut)*time.Second
|
2015-05-14 17:05:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func configureTCPTransport(tr *http.Transport, proto, addr string) {
|
|
|
|
// Why 32? See https://github.com/docker/docker/pull/8035.
|
|
|
|
timeout := 32 * time.Second
|
|
|
|
if proto == "unix" {
|
|
|
|
// No need for compression in local communications.
|
|
|
|
tr.DisableCompression = true
|
|
|
|
tr.Dial = func(_, _ string) (net.Conn, error) {
|
|
|
|
return net.DialTimeout(proto, addr, timeout)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tr.Proxy = http.ProxyFromEnvironment
|
|
|
|
tr.Dial = (&net.Dialer{Timeout: timeout}).Dial
|
|
|
|
}
|
|
|
|
}
|