options.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright 2020-2021 InfluxData, Inc. All rights reserved.
  2. // Use of this source code is governed by MIT
  3. // license that can be found in the LICENSE file.
  4. package http
  5. import (
  6. "crypto/tls"
  7. "net"
  8. "net/http"
  9. "time"
  10. )
  11. // Options holds http configuration properties for communicating with InfluxDB server
  12. type Options struct {
  13. // HTTP client. Default is http.DefaultClient.
  14. httpClient *http.Client
  15. // doer is an http Doer - if set it overrides httpClient
  16. doer Doer
  17. // Flag whether http client was created internally
  18. ownClient bool
  19. // TLS configuration for secure connection. Default nil
  20. tlsConfig *tls.Config
  21. // HTTP request timeout in sec. Default 20
  22. httpRequestTimeout uint
  23. // Application name in the User-Agent HTTP header string
  24. appName string
  25. }
  26. // HTTPClient returns the http.Client that is configured to be used
  27. // for HTTP requests. It will return the one that has been set using
  28. // SetHTTPClient or it will construct a default client using the
  29. // other configured options.
  30. // HTTPClient panics if SetHTTPDoer was called.
  31. func (o *Options) HTTPClient() *http.Client {
  32. if o.doer != nil {
  33. panic("HTTPClient called after SetHTTPDoer")
  34. }
  35. if o.httpClient == nil {
  36. o.httpClient = &http.Client{
  37. Timeout: time.Second * time.Duration(o.HTTPRequestTimeout()),
  38. Transport: &http.Transport{
  39. DialContext: (&net.Dialer{
  40. Timeout: 5 * time.Second,
  41. }).DialContext,
  42. TLSHandshakeTimeout: 5 * time.Second,
  43. TLSClientConfig: o.TLSConfig(),
  44. MaxIdleConns: 100,
  45. MaxIdleConnsPerHost: 100,
  46. IdleConnTimeout: 90 * time.Second,
  47. },
  48. }
  49. o.ownClient = true
  50. }
  51. return o.httpClient
  52. }
  53. // SetHTTPClient will configure the http.Client that is used
  54. // for HTTP requests. If set to nil, an HTTPClient will be
  55. // generated.
  56. //
  57. // Setting the HTTPClient will cause the other HTTP options
  58. // to be ignored.
  59. // In case of UsersAPI.SignIn() is used, HTTPClient.Jar will be used for storing session cookie.
  60. func (o *Options) SetHTTPClient(c *http.Client) *Options {
  61. o.httpClient = c
  62. o.ownClient = false
  63. return o
  64. }
  65. // OwnHTTPClient returns true of HTTP client was created internally. False if it was set externally.
  66. func (o *Options) OwnHTTPClient() bool {
  67. return o.ownClient
  68. }
  69. // Doer allows proving custom Do for HTTP operations
  70. type Doer interface {
  71. Do(*http.Request) (*http.Response, error)
  72. }
  73. // SetHTTPDoer will configure the http.Client that is used
  74. // for HTTP requests. If set to nil, this has no effect.
  75. //
  76. // Setting the HTTPDoer will cause the other HTTP options
  77. // to be ignored.
  78. func (o *Options) SetHTTPDoer(d Doer) *Options {
  79. if d != nil {
  80. o.doer = d
  81. o.ownClient = false
  82. }
  83. return o
  84. }
  85. // HTTPDoer returns actual Doer if set, or http.Client
  86. func (o *Options) HTTPDoer() Doer {
  87. if o.doer != nil {
  88. return o.doer
  89. }
  90. return o.HTTPClient()
  91. }
  92. // TLSConfig returns tls.Config
  93. func (o *Options) TLSConfig() *tls.Config {
  94. return o.tlsConfig
  95. }
  96. // SetTLSConfig sets TLS configuration for secure connection
  97. func (o *Options) SetTLSConfig(tlsConfig *tls.Config) *Options {
  98. o.tlsConfig = tlsConfig
  99. return o
  100. }
  101. // HTTPRequestTimeout returns HTTP request timeout
  102. func (o *Options) HTTPRequestTimeout() uint {
  103. return o.httpRequestTimeout
  104. }
  105. // SetHTTPRequestTimeout sets HTTP request timeout in sec
  106. func (o *Options) SetHTTPRequestTimeout(httpRequestTimeout uint) *Options {
  107. o.httpRequestTimeout = httpRequestTimeout
  108. return o
  109. }
  110. // ApplicationName returns application name used in the User-Agent HTTP header
  111. func (o *Options) ApplicationName() string {
  112. return o.appName
  113. }
  114. // SetApplicationName sets an application name to the User-Agent HTTP header
  115. func (o *Options) SetApplicationName(appName string) *Options {
  116. o.appName = appName
  117. return o
  118. }
  119. // DefaultOptions returns Options object with default values
  120. func DefaultOptions() *Options {
  121. return &Options{httpRequestTimeout: 20}
  122. }