client.go 4.2 KB

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