client.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. package plugins
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "fmt"
  6. "io"
  7. "io/ioutil"
  8. "net/http"
  9. "strings"
  10. "time"
  11. "github.com/Sirupsen/logrus"
  12. "github.com/docker/go-connections/sockets"
  13. "github.com/docker/go-connections/tlsconfig"
  14. )
  15. const (
  16. versionMimetype = "application/vnd.docker.plugins.v1.2+json"
  17. defaultTimeOut = 30
  18. )
  19. // NewClient creates a new plugin client (http).
  20. func NewClient(addr string, tlsConfig tlsconfig.Options) (*Client, error) {
  21. tr := &http.Transport{}
  22. c, err := tlsconfig.Client(tlsConfig)
  23. if err != nil {
  24. return nil, err
  25. }
  26. tr.TLSClientConfig = c
  27. protoAndAddr := strings.Split(addr, "://")
  28. sockets.ConfigureTCPTransport(tr, protoAndAddr[0], protoAndAddr[1])
  29. scheme := protoAndAddr[0]
  30. if scheme != "https" {
  31. scheme = "http"
  32. }
  33. return &Client{&http.Client{Transport: tr}, scheme, protoAndAddr[1]}, nil
  34. }
  35. // Client represents a plugin client.
  36. type Client struct {
  37. http *http.Client // http client to use
  38. scheme string // scheme protocol of the plugin
  39. addr string // http address of the plugin
  40. }
  41. // Call calls the specified method with the specified arguments for the plugin.
  42. // It will retry for 30 seconds if a failure occurs when calling.
  43. func (c *Client) Call(serviceMethod string, args interface{}, ret interface{}) error {
  44. var buf bytes.Buffer
  45. if args != nil {
  46. if err := json.NewEncoder(&buf).Encode(args); err != nil {
  47. return err
  48. }
  49. }
  50. body, err := c.callWithRetry(serviceMethod, &buf, true)
  51. if err != nil {
  52. return err
  53. }
  54. defer body.Close()
  55. if ret != nil {
  56. if err := json.NewDecoder(body).Decode(&ret); err != nil {
  57. logrus.Errorf("%s: error reading plugin resp: %v", serviceMethod, err)
  58. return err
  59. }
  60. }
  61. return nil
  62. }
  63. // Stream calls the specified method with the specified arguments for the plugin and returns the response body
  64. func (c *Client) Stream(serviceMethod string, args interface{}) (io.ReadCloser, error) {
  65. var buf bytes.Buffer
  66. if err := json.NewEncoder(&buf).Encode(args); err != nil {
  67. return nil, err
  68. }
  69. return c.callWithRetry(serviceMethod, &buf, true)
  70. }
  71. // SendFile calls the specified method, and passes through the IO stream
  72. func (c *Client) SendFile(serviceMethod string, data io.Reader, ret interface{}) error {
  73. body, err := c.callWithRetry(serviceMethod, data, true)
  74. if err != nil {
  75. return err
  76. }
  77. if err := json.NewDecoder(body).Decode(&ret); err != nil {
  78. logrus.Errorf("%s: error reading plugin resp: %v", serviceMethod, err)
  79. return err
  80. }
  81. return nil
  82. }
  83. func (c *Client) callWithRetry(serviceMethod string, data io.Reader, retry bool) (io.ReadCloser, error) {
  84. req, err := http.NewRequest("POST", "/"+serviceMethod, data)
  85. if err != nil {
  86. return nil, err
  87. }
  88. req.Header.Add("Accept", versionMimetype)
  89. req.URL.Scheme = c.scheme
  90. req.URL.Host = c.addr
  91. var retries int
  92. start := time.Now()
  93. for {
  94. resp, err := c.http.Do(req)
  95. if err != nil {
  96. if !retry {
  97. return nil, err
  98. }
  99. timeOff := backoff(retries)
  100. if abort(start, timeOff) {
  101. return nil, err
  102. }
  103. retries++
  104. logrus.Warnf("Unable to connect to plugin: %s, retrying in %v", c.addr, timeOff)
  105. time.Sleep(timeOff)
  106. continue
  107. }
  108. if resp.StatusCode != http.StatusOK {
  109. b, err := ioutil.ReadAll(resp.Body)
  110. if err != nil {
  111. return nil, fmt.Errorf("%s: %s", serviceMethod, err)
  112. }
  113. // Plugins' Response(s) should have an Err field indicating what went
  114. // wrong. Try to unmarshal into ResponseErr. Otherwise fallback to just
  115. // return the string(body)
  116. type responseErr struct {
  117. Err string
  118. }
  119. remoteErr := responseErr{}
  120. if err := json.Unmarshal(b, &remoteErr); err == nil {
  121. if remoteErr.Err != "" {
  122. return nil, fmt.Errorf("%s: %s", serviceMethod, remoteErr.Err)
  123. }
  124. }
  125. // old way...
  126. return nil, fmt.Errorf("%s: %s", serviceMethod, string(b))
  127. }
  128. return resp.Body, nil
  129. }
  130. }
  131. func backoff(retries int) time.Duration {
  132. b, max := 1, defaultTimeOut
  133. for b < max && retries > 0 {
  134. b *= 2
  135. retries--
  136. }
  137. if b > max {
  138. b = max
  139. }
  140. return time.Duration(b) * time.Second
  141. }
  142. func abort(start time.Time, timeOff time.Duration) bool {
  143. return timeOff+time.Since(start) >= time.Duration(defaultTimeOut)*time.Second
  144. }