client.go 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953
  1. package fasthttp
  2. import (
  3. "bufio"
  4. "crypto/tls"
  5. "errors"
  6. "fmt"
  7. "io"
  8. "net"
  9. "strings"
  10. "sync"
  11. "sync/atomic"
  12. "time"
  13. )
  14. // Do performs the given http request and fills the given http response.
  15. //
  16. // Request must contain at least non-zero RequestURI with full url (including
  17. // scheme and host) or non-zero Host header + RequestURI.
  18. //
  19. // Client determines the server to be requested in the following order:
  20. //
  21. // - from RequestURI if it contains full url with scheme and host;
  22. // - from Host header otherwise.
  23. //
  24. // The function doesn't follow redirects. Use Get* for following redirects.
  25. //
  26. // Response is ignored if resp is nil.
  27. //
  28. // ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections
  29. // to the requested host are busy.
  30. //
  31. // It is recommended obtaining req and resp via AcquireRequest
  32. // and AcquireResponse in performance-critical code.
  33. func Do(req *Request, resp *Response) error {
  34. return defaultClient.Do(req, resp)
  35. }
  36. // DoTimeout performs the given request and waits for response during
  37. // the given timeout duration.
  38. //
  39. // Request must contain at least non-zero RequestURI with full url (including
  40. // scheme and host) or non-zero Host header + RequestURI.
  41. //
  42. // Client determines the server to be requested in the following order:
  43. //
  44. // - from RequestURI if it contains full url with scheme and host;
  45. // - from Host header otherwise.
  46. //
  47. // The function doesn't follow redirects. Use Get* for following redirects.
  48. //
  49. // Response is ignored if resp is nil.
  50. //
  51. // ErrTimeout is returned if the response wasn't returned during
  52. // the given timeout.
  53. //
  54. // ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections
  55. // to the requested host are busy.
  56. //
  57. // It is recommended obtaining req and resp via AcquireRequest
  58. // and AcquireResponse in performance-critical code.
  59. func DoTimeout(req *Request, resp *Response, timeout time.Duration) error {
  60. return defaultClient.DoTimeout(req, resp, timeout)
  61. }
  62. // DoDeadline performs the given request and waits for response until
  63. // the given deadline.
  64. //
  65. // Request must contain at least non-zero RequestURI with full url (including
  66. // scheme and host) or non-zero Host header + RequestURI.
  67. //
  68. // Client determines the server to be requested in the following order:
  69. //
  70. // - from RequestURI if it contains full url with scheme and host;
  71. // - from Host header otherwise.
  72. //
  73. // The function doesn't follow redirects. Use Get* for following redirects.
  74. //
  75. // Response is ignored if resp is nil.
  76. //
  77. // ErrTimeout is returned if the response wasn't returned until
  78. // the given deadline.
  79. //
  80. // ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections
  81. // to the requested host are busy.
  82. //
  83. // It is recommended obtaining req and resp via AcquireRequest
  84. // and AcquireResponse in performance-critical code.
  85. func DoDeadline(req *Request, resp *Response, deadline time.Time) error {
  86. return defaultClient.DoDeadline(req, resp, deadline)
  87. }
  88. // DoRedirects performs the given http request and fills the given http response,
  89. // following up to maxRedirectsCount redirects. When the redirect count exceeds
  90. // maxRedirectsCount, ErrTooManyRedirects is returned.
  91. //
  92. // Request must contain at least non-zero RequestURI with full url (including
  93. // scheme and host) or non-zero Host header + RequestURI.
  94. //
  95. // Client determines the server to be requested in the following order:
  96. //
  97. // - from RequestURI if it contains full url with scheme and host;
  98. // - from Host header otherwise.
  99. //
  100. // Response is ignored if resp is nil.
  101. //
  102. // ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections
  103. // to the requested host are busy.
  104. //
  105. // It is recommended obtaining req and resp via AcquireRequest
  106. // and AcquireResponse in performance-critical code.
  107. func DoRedirects(req *Request, resp *Response, maxRedirectsCount int) error {
  108. _, _, err := doRequestFollowRedirects(req, resp, req.URI().String(), maxRedirectsCount, &defaultClient)
  109. return err
  110. }
  111. // Get returns the status code and body of url.
  112. //
  113. // The contents of dst will be replaced by the body and returned, if the dst
  114. // is too small a new slice will be allocated.
  115. //
  116. // The function follows redirects. Use Do* for manually handling redirects.
  117. func Get(dst []byte, url string) (statusCode int, body []byte, err error) {
  118. return defaultClient.Get(dst, url)
  119. }
  120. // GetTimeout returns the status code and body of url.
  121. //
  122. // The contents of dst will be replaced by the body and returned, if the dst
  123. // is too small a new slice will be allocated.
  124. //
  125. // The function follows redirects. Use Do* for manually handling redirects.
  126. //
  127. // ErrTimeout error is returned if url contents couldn't be fetched
  128. // during the given timeout.
  129. func GetTimeout(dst []byte, url string, timeout time.Duration) (statusCode int, body []byte, err error) {
  130. return defaultClient.GetTimeout(dst, url, timeout)
  131. }
  132. // GetDeadline returns the status code and body of url.
  133. //
  134. // The contents of dst will be replaced by the body and returned, if the dst
  135. // is too small a new slice will be allocated.
  136. //
  137. // The function follows redirects. Use Do* for manually handling redirects.
  138. //
  139. // ErrTimeout error is returned if url contents couldn't be fetched
  140. // until the given deadline.
  141. func GetDeadline(dst []byte, url string, deadline time.Time) (statusCode int, body []byte, err error) {
  142. return defaultClient.GetDeadline(dst, url, deadline)
  143. }
  144. // Post sends POST request to the given url with the given POST arguments.
  145. //
  146. // The contents of dst will be replaced by the body and returned, if the dst
  147. // is too small a new slice will be allocated.
  148. //
  149. // The function follows redirects. Use Do* for manually handling redirects.
  150. //
  151. // Empty POST body is sent if postArgs is nil.
  152. func Post(dst []byte, url string, postArgs *Args) (statusCode int, body []byte, err error) {
  153. return defaultClient.Post(dst, url, postArgs)
  154. }
  155. var defaultClient Client
  156. // Client implements http client.
  157. //
  158. // Copying Client by value is prohibited. Create new instance instead.
  159. //
  160. // It is safe calling Client methods from concurrently running goroutines.
  161. //
  162. // The fields of a Client should not be changed while it is in use.
  163. type Client struct {
  164. noCopy noCopy
  165. // Client name. Used in User-Agent request header.
  166. //
  167. // Default client name is used if not set.
  168. Name string
  169. // NoDefaultUserAgentHeader when set to true, causes the default
  170. // User-Agent header to be excluded from the Request.
  171. NoDefaultUserAgentHeader bool
  172. // Callback for establishing new connections to hosts.
  173. //
  174. // Default Dial is used if not set.
  175. Dial DialFunc
  176. // Attempt to connect to both ipv4 and ipv6 addresses if set to true.
  177. //
  178. // This option is used only if default TCP dialer is used,
  179. // i.e. if Dial is blank.
  180. //
  181. // By default client connects only to ipv4 addresses,
  182. // since unfortunately ipv6 remains broken in many networks worldwide :)
  183. DialDualStack bool
  184. // TLS config for https connections.
  185. //
  186. // Default TLS config is used if not set.
  187. TLSConfig *tls.Config
  188. // Maximum number of connections per each host which may be established.
  189. //
  190. // DefaultMaxConnsPerHost is used if not set.
  191. MaxConnsPerHost int
  192. // Idle keep-alive connections are closed after this duration.
  193. //
  194. // By default idle connections are closed
  195. // after DefaultMaxIdleConnDuration.
  196. MaxIdleConnDuration time.Duration
  197. // Keep-alive connections are closed after this duration.
  198. //
  199. // By default connection duration is unlimited.
  200. MaxConnDuration time.Duration
  201. // Maximum number of attempts for idempotent calls
  202. //
  203. // DefaultMaxIdemponentCallAttempts is used if not set.
  204. MaxIdemponentCallAttempts int
  205. // Per-connection buffer size for responses' reading.
  206. // This also limits the maximum header size.
  207. //
  208. // Default buffer size is used if 0.
  209. ReadBufferSize int
  210. // Per-connection buffer size for requests' writing.
  211. //
  212. // Default buffer size is used if 0.
  213. WriteBufferSize int
  214. // Maximum duration for full response reading (including body).
  215. //
  216. // By default response read timeout is unlimited.
  217. ReadTimeout time.Duration
  218. // Maximum duration for full request writing (including body).
  219. //
  220. // By default request write timeout is unlimited.
  221. WriteTimeout time.Duration
  222. // Maximum response body size.
  223. //
  224. // The client returns ErrBodyTooLarge if this limit is greater than 0
  225. // and response body is greater than the limit.
  226. //
  227. // By default response body size is unlimited.
  228. MaxResponseBodySize int
  229. // Header names are passed as-is without normalization
  230. // if this option is set.
  231. //
  232. // Disabled header names' normalization may be useful only for proxying
  233. // responses to other clients expecting case-sensitive
  234. // header names. See https://github.com/valyala/fasthttp/issues/57
  235. // for details.
  236. //
  237. // By default request and response header names are normalized, i.e.
  238. // The first letter and the first letters following dashes
  239. // are uppercased, while all the other letters are lowercased.
  240. // Examples:
  241. //
  242. // * HOST -> Host
  243. // * content-type -> Content-Type
  244. // * cONTENT-lenGTH -> Content-Length
  245. DisableHeaderNamesNormalizing bool
  246. // Path values are sent as-is without normalization
  247. //
  248. // Disabled path normalization may be useful for proxying incoming requests
  249. // to servers that are expecting paths to be forwarded as-is.
  250. //
  251. // By default path values are normalized, i.e.
  252. // extra slashes are removed, special characters are encoded.
  253. DisablePathNormalizing bool
  254. // Maximum duration for waiting for a free connection.
  255. //
  256. // By default will not waiting, return ErrNoFreeConns immediately
  257. MaxConnWaitTimeout time.Duration
  258. // RetryIf controls whether a retry should be attempted after an error.
  259. //
  260. // By default will use isIdempotent function
  261. RetryIf RetryIfFunc
  262. // Connection pool strategy. Can be either LIFO or FIFO (default).
  263. ConnPoolStrategy ConnPoolStrategyType
  264. // StreamResponseBody enables response body streaming
  265. StreamResponseBody bool
  266. // ConfigureClient configures the fasthttp.HostClient.
  267. ConfigureClient func(hc *HostClient) error
  268. mLock sync.RWMutex
  269. mOnce sync.Once
  270. m map[string]*HostClient
  271. ms map[string]*HostClient
  272. readerPool sync.Pool
  273. writerPool sync.Pool
  274. }
  275. // Get returns the status code and body of url.
  276. //
  277. // The contents of dst will be replaced by the body and returned, if the dst
  278. // is too small a new slice will be allocated.
  279. //
  280. // The function follows redirects. Use Do* for manually handling redirects.
  281. func (c *Client) Get(dst []byte, url string) (statusCode int, body []byte, err error) {
  282. return clientGetURL(dst, url, c)
  283. }
  284. // GetTimeout returns the status code and body of url.
  285. //
  286. // The contents of dst will be replaced by the body and returned, if the dst
  287. // is too small a new slice will be allocated.
  288. //
  289. // The function follows redirects. Use Do* for manually handling redirects.
  290. //
  291. // ErrTimeout error is returned if url contents couldn't be fetched
  292. // during the given timeout.
  293. func (c *Client) GetTimeout(dst []byte, url string, timeout time.Duration) (statusCode int, body []byte, err error) {
  294. return clientGetURLTimeout(dst, url, timeout, c)
  295. }
  296. // GetDeadline returns the status code and body of url.
  297. //
  298. // The contents of dst will be replaced by the body and returned, if the dst
  299. // is too small a new slice will be allocated.
  300. //
  301. // The function follows redirects. Use Do* for manually handling redirects.
  302. //
  303. // ErrTimeout error is returned if url contents couldn't be fetched
  304. // until the given deadline.
  305. func (c *Client) GetDeadline(dst []byte, url string, deadline time.Time) (statusCode int, body []byte, err error) {
  306. return clientGetURLDeadline(dst, url, deadline, c)
  307. }
  308. // Post sends POST request to the given url with the given POST arguments.
  309. //
  310. // The contents of dst will be replaced by the body and returned, if the dst
  311. // is too small a new slice will be allocated.
  312. //
  313. // The function follows redirects. Use Do* for manually handling redirects.
  314. //
  315. // Empty POST body is sent if postArgs is nil.
  316. func (c *Client) Post(dst []byte, url string, postArgs *Args) (statusCode int, body []byte, err error) {
  317. return clientPostURL(dst, url, postArgs, c)
  318. }
  319. // DoTimeout performs the given request and waits for response during
  320. // the given timeout duration.
  321. //
  322. // Request must contain at least non-zero RequestURI with full url (including
  323. // scheme and host) or non-zero Host header + RequestURI.
  324. //
  325. // Client determines the server to be requested in the following order:
  326. //
  327. // - from RequestURI if it contains full url with scheme and host;
  328. // - from Host header otherwise.
  329. //
  330. // The function doesn't follow redirects. Use Get* for following redirects.
  331. //
  332. // Response is ignored if resp is nil.
  333. //
  334. // ErrTimeout is returned if the response wasn't returned during
  335. // the given timeout.
  336. // Immediately returns ErrTimeout if timeout value is negative.
  337. //
  338. // ErrNoFreeConns is returned if all Client.MaxConnsPerHost connections
  339. // to the requested host are busy.
  340. //
  341. // It is recommended obtaining req and resp via AcquireRequest
  342. // and AcquireResponse in performance-critical code.
  343. func (c *Client) DoTimeout(req *Request, resp *Response, timeout time.Duration) error {
  344. req.timeout = timeout
  345. if req.timeout <= 0 {
  346. return ErrTimeout
  347. }
  348. return c.Do(req, resp)
  349. }
  350. // DoDeadline performs the given request and waits for response until
  351. // the given deadline.
  352. //
  353. // Request must contain at least non-zero RequestURI with full url (including
  354. // scheme and host) or non-zero Host header + RequestURI.
  355. //
  356. // Client determines the server to be requested in the following order:
  357. //
  358. // - from RequestURI if it contains full url with scheme and host;
  359. // - from Host header otherwise.
  360. //
  361. // The function doesn't follow redirects. Use Get* for following redirects.
  362. //
  363. // Response is ignored if resp is nil.
  364. //
  365. // ErrTimeout is returned if the response wasn't returned until
  366. // the given deadline.
  367. // Immediately returns ErrTimeout if the deadline has already been reached.
  368. //
  369. // ErrNoFreeConns is returned if all Client.MaxConnsPerHost connections
  370. // to the requested host are busy.
  371. //
  372. // It is recommended obtaining req and resp via AcquireRequest
  373. // and AcquireResponse in performance-critical code.
  374. func (c *Client) DoDeadline(req *Request, resp *Response, deadline time.Time) error {
  375. req.timeout = time.Until(deadline)
  376. if req.timeout <= 0 {
  377. return ErrTimeout
  378. }
  379. return c.Do(req, resp)
  380. }
  381. // DoRedirects performs the given http request and fills the given http response,
  382. // following up to maxRedirectsCount redirects. When the redirect count exceeds
  383. // maxRedirectsCount, ErrTooManyRedirects is returned.
  384. //
  385. // Request must contain at least non-zero RequestURI with full url (including
  386. // scheme and host) or non-zero Host header + RequestURI.
  387. //
  388. // Client determines the server to be requested in the following order:
  389. //
  390. // - from RequestURI if it contains full url with scheme and host;
  391. // - from Host header otherwise.
  392. //
  393. // Response is ignored if resp is nil.
  394. //
  395. // ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections
  396. // to the requested host are busy.
  397. //
  398. // It is recommended obtaining req and resp via AcquireRequest
  399. // and AcquireResponse in performance-critical code.
  400. func (c *Client) DoRedirects(req *Request, resp *Response, maxRedirectsCount int) error {
  401. _, _, err := doRequestFollowRedirects(req, resp, req.URI().String(), maxRedirectsCount, c)
  402. return err
  403. }
  404. // Do performs the given http request and fills the given http response.
  405. //
  406. // Request must contain at least non-zero RequestURI with full url (including
  407. // scheme and host) or non-zero Host header + RequestURI.
  408. //
  409. // Client determines the server to be requested in the following order:
  410. //
  411. // - from RequestURI if it contains full url with scheme and host;
  412. // - from Host header otherwise.
  413. //
  414. // Response is ignored if resp is nil.
  415. //
  416. // The function doesn't follow redirects. Use Get* for following redirects.
  417. //
  418. // ErrNoFreeConns is returned if all Client.MaxConnsPerHost connections
  419. // to the requested host are busy.
  420. //
  421. // It is recommended obtaining req and resp via AcquireRequest
  422. // and AcquireResponse in performance-critical code.
  423. func (c *Client) Do(req *Request, resp *Response) error {
  424. uri := req.URI()
  425. if uri == nil {
  426. return ErrorInvalidURI
  427. }
  428. host := uri.Host()
  429. isTLS := false
  430. if uri.isHTTPS() {
  431. isTLS = true
  432. } else if !uri.isHTTP() {
  433. return fmt.Errorf("unsupported protocol %q. http and https are supported", uri.Scheme())
  434. }
  435. c.mOnce.Do(func() {
  436. c.mLock.Lock()
  437. c.m = make(map[string]*HostClient)
  438. c.ms = make(map[string]*HostClient)
  439. c.mLock.Unlock()
  440. })
  441. startCleaner := false
  442. c.mLock.RLock()
  443. m := c.m
  444. if isTLS {
  445. m = c.ms
  446. }
  447. hc := m[string(host)]
  448. if hc != nil {
  449. atomic.AddInt32(&hc.pendingClientRequests, 1)
  450. defer atomic.AddInt32(&hc.pendingClientRequests, -1)
  451. }
  452. c.mLock.RUnlock()
  453. if hc == nil {
  454. c.mLock.Lock()
  455. hc = m[string(host)]
  456. if hc == nil {
  457. hc = &HostClient{
  458. Addr: AddMissingPort(string(host), isTLS),
  459. Name: c.Name,
  460. NoDefaultUserAgentHeader: c.NoDefaultUserAgentHeader,
  461. Dial: c.Dial,
  462. DialDualStack: c.DialDualStack,
  463. IsTLS: isTLS,
  464. TLSConfig: c.TLSConfig,
  465. MaxConns: c.MaxConnsPerHost,
  466. MaxIdleConnDuration: c.MaxIdleConnDuration,
  467. MaxConnDuration: c.MaxConnDuration,
  468. MaxIdemponentCallAttempts: c.MaxIdemponentCallAttempts,
  469. ReadBufferSize: c.ReadBufferSize,
  470. WriteBufferSize: c.WriteBufferSize,
  471. ReadTimeout: c.ReadTimeout,
  472. WriteTimeout: c.WriteTimeout,
  473. MaxResponseBodySize: c.MaxResponseBodySize,
  474. DisableHeaderNamesNormalizing: c.DisableHeaderNamesNormalizing,
  475. DisablePathNormalizing: c.DisablePathNormalizing,
  476. MaxConnWaitTimeout: c.MaxConnWaitTimeout,
  477. RetryIf: c.RetryIf,
  478. ConnPoolStrategy: c.ConnPoolStrategy,
  479. StreamResponseBody: c.StreamResponseBody,
  480. clientReaderPool: &c.readerPool,
  481. clientWriterPool: &c.writerPool,
  482. }
  483. if c.ConfigureClient != nil {
  484. if err := c.ConfigureClient(hc); err != nil {
  485. c.mLock.Unlock()
  486. return err
  487. }
  488. }
  489. m[string(host)] = hc
  490. if len(m) == 1 {
  491. startCleaner = true
  492. }
  493. }
  494. atomic.AddInt32(&hc.pendingClientRequests, 1)
  495. defer atomic.AddInt32(&hc.pendingClientRequests, -1)
  496. c.mLock.Unlock()
  497. }
  498. if startCleaner {
  499. go c.mCleaner(m)
  500. }
  501. return hc.Do(req, resp)
  502. }
  503. // CloseIdleConnections closes any connections which were previously
  504. // connected from previous requests but are now sitting idle in a
  505. // "keep-alive" state. It does not interrupt any connections currently
  506. // in use.
  507. func (c *Client) CloseIdleConnections() {
  508. c.mLock.RLock()
  509. for _, v := range c.m {
  510. v.CloseIdleConnections()
  511. }
  512. for _, v := range c.ms {
  513. v.CloseIdleConnections()
  514. }
  515. c.mLock.RUnlock()
  516. }
  517. func (c *Client) mCleaner(m map[string]*HostClient) {
  518. mustStop := false
  519. sleep := c.MaxIdleConnDuration
  520. if sleep < time.Second {
  521. sleep = time.Second
  522. } else if sleep > 10*time.Second {
  523. sleep = 10 * time.Second
  524. }
  525. for {
  526. time.Sleep(sleep)
  527. c.mLock.Lock()
  528. for k, v := range m {
  529. v.connsLock.Lock()
  530. /* #nosec G601 */
  531. if v.connsCount == 0 && atomic.LoadInt32(&v.pendingClientRequests) == 0 {
  532. delete(m, k)
  533. }
  534. v.connsLock.Unlock()
  535. }
  536. if len(m) == 0 {
  537. mustStop = true
  538. }
  539. c.mLock.Unlock()
  540. if mustStop {
  541. break
  542. }
  543. }
  544. }
  545. // DefaultMaxConnsPerHost is the maximum number of concurrent connections
  546. // http client may establish per host by default (i.e. if
  547. // Client.MaxConnsPerHost isn't set).
  548. const DefaultMaxConnsPerHost = 512
  549. // DefaultMaxIdleConnDuration is the default duration before idle keep-alive
  550. // connection is closed.
  551. const DefaultMaxIdleConnDuration = 10 * time.Second
  552. // DefaultMaxIdemponentCallAttempts is the default idempotent calls attempts count.
  553. const DefaultMaxIdemponentCallAttempts = 5
  554. // DialFunc must establish connection to addr.
  555. //
  556. // There is no need in establishing TLS (SSL) connection for https.
  557. // The client automatically converts connection to TLS
  558. // if HostClient.IsTLS is set.
  559. //
  560. // TCP address passed to DialFunc always contains host and port.
  561. // Example TCP addr values:
  562. //
  563. // - foobar.com:80
  564. // - foobar.com:443
  565. // - foobar.com:8080
  566. type DialFunc func(addr string) (net.Conn, error)
  567. // RetryIfFunc signature of retry if function
  568. //
  569. // Request argument passed to RetryIfFunc, if there are any request errors.
  570. type RetryIfFunc func(request *Request) bool
  571. // RoundTripper wraps every request/response.
  572. type RoundTripper interface {
  573. RoundTrip(hc *HostClient, req *Request, resp *Response) (retry bool, err error)
  574. }
  575. // ConnPoolStrategyType define strategy of connection pool enqueue/dequeue
  576. type ConnPoolStrategyType int
  577. const (
  578. FIFO ConnPoolStrategyType = iota
  579. LIFO
  580. )
  581. // HostClient balances http requests among hosts listed in Addr.
  582. //
  583. // HostClient may be used for balancing load among multiple upstream hosts.
  584. // While multiple addresses passed to HostClient.Addr may be used for balancing
  585. // load among them, it would be better using LBClient instead, since HostClient
  586. // may unevenly balance load among upstream hosts.
  587. //
  588. // It is forbidden copying HostClient instances. Create new instances instead.
  589. //
  590. // It is safe calling HostClient methods from concurrently running goroutines.
  591. type HostClient struct {
  592. noCopy noCopy
  593. // Comma-separated list of upstream HTTP server host addresses,
  594. // which are passed to Dial in a round-robin manner.
  595. //
  596. // Each address may contain port if default dialer is used.
  597. // For example,
  598. //
  599. // - foobar.com:80
  600. // - foobar.com:443
  601. // - foobar.com:8080
  602. Addr string
  603. // Client name. Used in User-Agent request header.
  604. Name string
  605. // NoDefaultUserAgentHeader when set to true, causes the default
  606. // User-Agent header to be excluded from the Request.
  607. NoDefaultUserAgentHeader bool
  608. // Callback for establishing new connection to the host.
  609. //
  610. // Default Dial is used if not set.
  611. Dial DialFunc
  612. // Attempt to connect to both ipv4 and ipv6 host addresses
  613. // if set to true.
  614. //
  615. // This option is used only if default TCP dialer is used,
  616. // i.e. if Dial is blank.
  617. //
  618. // By default client connects only to ipv4 addresses,
  619. // since unfortunately ipv6 remains broken in many networks worldwide :)
  620. DialDualStack bool
  621. // Whether to use TLS (aka SSL or HTTPS) for host connections.
  622. IsTLS bool
  623. // Optional TLS config.
  624. TLSConfig *tls.Config
  625. // Maximum number of connections which may be established to all hosts
  626. // listed in Addr.
  627. //
  628. // You can change this value while the HostClient is being used
  629. // with HostClient.SetMaxConns(value)
  630. //
  631. // DefaultMaxConnsPerHost is used if not set.
  632. MaxConns int
  633. // Keep-alive connections are closed after this duration.
  634. //
  635. // By default connection duration is unlimited.
  636. MaxConnDuration time.Duration
  637. // Idle keep-alive connections are closed after this duration.
  638. //
  639. // By default idle connections are closed
  640. // after DefaultMaxIdleConnDuration.
  641. MaxIdleConnDuration time.Duration
  642. // Maximum number of attempts for idempotent calls
  643. //
  644. // DefaultMaxIdemponentCallAttempts is used if not set.
  645. MaxIdemponentCallAttempts int
  646. // Per-connection buffer size for responses' reading.
  647. // This also limits the maximum header size.
  648. //
  649. // Default buffer size is used if 0.
  650. ReadBufferSize int
  651. // Per-connection buffer size for requests' writing.
  652. //
  653. // Default buffer size is used if 0.
  654. WriteBufferSize int
  655. // Maximum duration for full response reading (including body).
  656. //
  657. // By default response read timeout is unlimited.
  658. ReadTimeout time.Duration
  659. // Maximum duration for full request writing (including body).
  660. //
  661. // By default request write timeout is unlimited.
  662. WriteTimeout time.Duration
  663. // Maximum response body size.
  664. //
  665. // The client returns ErrBodyTooLarge if this limit is greater than 0
  666. // and response body is greater than the limit.
  667. //
  668. // By default response body size is unlimited.
  669. MaxResponseBodySize int
  670. // Header names are passed as-is without normalization
  671. // if this option is set.
  672. //
  673. // Disabled header names' normalization may be useful only for proxying
  674. // responses to other clients expecting case-sensitive
  675. // header names. See https://github.com/valyala/fasthttp/issues/57
  676. // for details.
  677. //
  678. // By default request and response header names are normalized, i.e.
  679. // The first letter and the first letters following dashes
  680. // are uppercased, while all the other letters are lowercased.
  681. // Examples:
  682. //
  683. // * HOST -> Host
  684. // * content-type -> Content-Type
  685. // * cONTENT-lenGTH -> Content-Length
  686. DisableHeaderNamesNormalizing bool
  687. // Path values are sent as-is without normalization
  688. //
  689. // Disabled path normalization may be useful for proxying incoming requests
  690. // to servers that are expecting paths to be forwarded as-is.
  691. //
  692. // By default path values are normalized, i.e.
  693. // extra slashes are removed, special characters are encoded.
  694. DisablePathNormalizing bool
  695. // Will not log potentially sensitive content in error logs
  696. //
  697. // This option is useful for servers that handle sensitive data
  698. // in the request/response.
  699. //
  700. // Client logs full errors by default.
  701. SecureErrorLogMessage bool
  702. // Maximum duration for waiting for a free connection.
  703. //
  704. // By default will not waiting, return ErrNoFreeConns immediately
  705. MaxConnWaitTimeout time.Duration
  706. // RetryIf controls whether a retry should be attempted after an error.
  707. //
  708. // By default will use isIdempotent function
  709. RetryIf RetryIfFunc
  710. // Transport defines a transport-like mechanism that wraps every request/response.
  711. Transport RoundTripper
  712. // Connection pool strategy. Can be either LIFO or FIFO (default).
  713. ConnPoolStrategy ConnPoolStrategyType
  714. // StreamResponseBody enables response body streaming
  715. StreamResponseBody bool
  716. lastUseTime uint32
  717. connsLock sync.Mutex
  718. connsCount int
  719. conns []*clientConn
  720. connsWait *wantConnQueue
  721. addrsLock sync.Mutex
  722. addrs []string
  723. addrIdx uint32
  724. tlsConfigMap map[string]*tls.Config
  725. tlsConfigMapLock sync.Mutex
  726. readerPool sync.Pool
  727. writerPool sync.Pool
  728. clientReaderPool *sync.Pool
  729. clientWriterPool *sync.Pool
  730. pendingRequests int32
  731. // pendingClientRequests counts the number of requests that a Client is currently running using this HostClient.
  732. // It will be incremented earlier than pendingRequests and will be used by Client to see if the HostClient is still in use.
  733. pendingClientRequests int32
  734. connsCleanerRun bool
  735. }
  736. type clientConn struct {
  737. c net.Conn
  738. createdTime time.Time
  739. lastUseTime time.Time
  740. }
  741. var startTimeUnix = time.Now().Unix()
  742. // LastUseTime returns time the client was last used
  743. func (c *HostClient) LastUseTime() time.Time {
  744. n := atomic.LoadUint32(&c.lastUseTime)
  745. return time.Unix(startTimeUnix+int64(n), 0)
  746. }
  747. // Get returns the status code and body of url.
  748. //
  749. // The contents of dst will be replaced by the body and returned, if the dst
  750. // is too small a new slice will be allocated.
  751. //
  752. // The function follows redirects. Use Do* for manually handling redirects.
  753. func (c *HostClient) Get(dst []byte, url string) (statusCode int, body []byte, err error) {
  754. return clientGetURL(dst, url, c)
  755. }
  756. // GetTimeout returns the status code and body of url.
  757. //
  758. // The contents of dst will be replaced by the body and returned, if the dst
  759. // is too small a new slice will be allocated.
  760. //
  761. // The function follows redirects. Use Do* for manually handling redirects.
  762. //
  763. // ErrTimeout error is returned if url contents couldn't be fetched
  764. // during the given timeout.
  765. func (c *HostClient) GetTimeout(dst []byte, url string, timeout time.Duration) (statusCode int, body []byte, err error) {
  766. return clientGetURLTimeout(dst, url, timeout, c)
  767. }
  768. // GetDeadline returns the status code and body of url.
  769. //
  770. // The contents of dst will be replaced by the body and returned, if the dst
  771. // is too small a new slice will be allocated.
  772. //
  773. // The function follows redirects. Use Do* for manually handling redirects.
  774. //
  775. // ErrTimeout error is returned if url contents couldn't be fetched
  776. // until the given deadline.
  777. func (c *HostClient) GetDeadline(dst []byte, url string, deadline time.Time) (statusCode int, body []byte, err error) {
  778. return clientGetURLDeadline(dst, url, deadline, c)
  779. }
  780. // Post sends POST request to the given url with the given POST arguments.
  781. //
  782. // The contents of dst will be replaced by the body and returned, if the dst
  783. // is too small a new slice will be allocated.
  784. //
  785. // The function follows redirects. Use Do* for manually handling redirects.
  786. //
  787. // Empty POST body is sent if postArgs is nil.
  788. func (c *HostClient) Post(dst []byte, url string, postArgs *Args) (statusCode int, body []byte, err error) {
  789. return clientPostURL(dst, url, postArgs, c)
  790. }
  791. type clientDoer interface {
  792. Do(req *Request, resp *Response) error
  793. }
  794. func clientGetURL(dst []byte, url string, c clientDoer) (statusCode int, body []byte, err error) {
  795. req := AcquireRequest()
  796. statusCode, body, err = doRequestFollowRedirectsBuffer(req, dst, url, c)
  797. ReleaseRequest(req)
  798. return statusCode, body, err
  799. }
  800. func clientGetURLTimeout(dst []byte, url string, timeout time.Duration, c clientDoer) (statusCode int, body []byte, err error) {
  801. deadline := time.Now().Add(timeout)
  802. return clientGetURLDeadline(dst, url, deadline, c)
  803. }
  804. type clientURLResponse struct {
  805. statusCode int
  806. body []byte
  807. err error
  808. }
  809. func clientGetURLDeadline(dst []byte, url string, deadline time.Time, c clientDoer) (statusCode int, body []byte, err error) {
  810. timeout := time.Until(deadline)
  811. if timeout <= 0 {
  812. return 0, dst, ErrTimeout
  813. }
  814. var ch chan clientURLResponse
  815. chv := clientURLResponseChPool.Get()
  816. if chv == nil {
  817. chv = make(chan clientURLResponse, 1)
  818. }
  819. ch = chv.(chan clientURLResponse)
  820. // Note that the request continues execution on ErrTimeout until
  821. // client-specific ReadTimeout exceeds. This helps limiting load
  822. // on slow hosts by MaxConns* concurrent requests.
  823. //
  824. // Without this 'hack' the load on slow host could exceed MaxConns*
  825. // concurrent requests, since timed out requests on client side
  826. // usually continue execution on the host.
  827. var mu sync.Mutex
  828. var timedout, responded bool
  829. go func() {
  830. req := AcquireRequest()
  831. statusCodeCopy, bodyCopy, errCopy := doRequestFollowRedirectsBuffer(req, dst, url, c)
  832. mu.Lock()
  833. if !timedout {
  834. ch <- clientURLResponse{
  835. statusCode: statusCodeCopy,
  836. body: bodyCopy,
  837. err: errCopy,
  838. }
  839. responded = true
  840. }
  841. mu.Unlock()
  842. ReleaseRequest(req)
  843. }()
  844. tc := AcquireTimer(timeout)
  845. select {
  846. case resp := <-ch:
  847. statusCode = resp.statusCode
  848. body = resp.body
  849. err = resp.err
  850. case <-tc.C:
  851. mu.Lock()
  852. if responded {
  853. resp := <-ch
  854. statusCode = resp.statusCode
  855. body = resp.body
  856. err = resp.err
  857. } else {
  858. timedout = true
  859. err = ErrTimeout
  860. body = dst
  861. }
  862. mu.Unlock()
  863. }
  864. ReleaseTimer(tc)
  865. clientURLResponseChPool.Put(chv)
  866. return statusCode, body, err
  867. }
  868. var clientURLResponseChPool sync.Pool
  869. func clientPostURL(dst []byte, url string, postArgs *Args, c clientDoer) (statusCode int, body []byte, err error) {
  870. req := AcquireRequest()
  871. defer ReleaseRequest(req)
  872. req.Header.SetMethod(MethodPost)
  873. req.Header.SetContentTypeBytes(strPostArgsContentType)
  874. if postArgs != nil {
  875. if _, err := postArgs.WriteTo(req.BodyWriter()); err != nil {
  876. return 0, nil, err
  877. }
  878. }
  879. statusCode, body, err = doRequestFollowRedirectsBuffer(req, dst, url, c)
  880. return statusCode, body, err
  881. }
  882. var (
  883. // ErrMissingLocation is returned by clients when the Location header is missing on
  884. // an HTTP response with a redirect status code.
  885. ErrMissingLocation = errors.New("missing Location header for http redirect")
  886. // ErrTooManyRedirects is returned by clients when the number of redirects followed
  887. // exceed the max count.
  888. ErrTooManyRedirects = errors.New("too many redirects detected when doing the request")
  889. // HostClients are only able to follow redirects to the same protocol.
  890. ErrHostClientRedirectToDifferentScheme = errors.New("HostClient can't follow redirects to a different protocol, please use Client instead")
  891. )
  892. const defaultMaxRedirectsCount = 16
  893. func doRequestFollowRedirectsBuffer(req *Request, dst []byte, url string, c clientDoer) (statusCode int, body []byte, err error) {
  894. resp := AcquireResponse()
  895. bodyBuf := resp.bodyBuffer()
  896. resp.keepBodyBuffer = true
  897. oldBody := bodyBuf.B
  898. bodyBuf.B = dst
  899. statusCode, _, err = doRequestFollowRedirects(req, resp, url, defaultMaxRedirectsCount, c)
  900. body = bodyBuf.B
  901. bodyBuf.B = oldBody
  902. resp.keepBodyBuffer = false
  903. ReleaseResponse(resp)
  904. return statusCode, body, err
  905. }
  906. func doRequestFollowRedirects(req *Request, resp *Response, url string, maxRedirectsCount int, c clientDoer) (statusCode int, body []byte, err error) {
  907. redirectsCount := 0
  908. for {
  909. req.SetRequestURI(url)
  910. if err := req.parseURI(); err != nil {
  911. return 0, nil, err
  912. }
  913. if err = c.Do(req, resp); err != nil {
  914. break
  915. }
  916. statusCode = resp.Header.StatusCode()
  917. if !StatusCodeIsRedirect(statusCode) {
  918. break
  919. }
  920. redirectsCount++
  921. if redirectsCount > maxRedirectsCount {
  922. err = ErrTooManyRedirects
  923. break
  924. }
  925. location := resp.Header.peek(strLocation)
  926. if len(location) == 0 {
  927. err = ErrMissingLocation
  928. break
  929. }
  930. url = getRedirectURL(url, location)
  931. }
  932. return statusCode, body, err
  933. }
  934. func getRedirectURL(baseURL string, location []byte) string {
  935. u := AcquireURI()
  936. u.Update(baseURL)
  937. u.UpdateBytes(location)
  938. redirectURL := u.String()
  939. ReleaseURI(u)
  940. return redirectURL
  941. }
  942. // StatusCodeIsRedirect returns true if the status code indicates a redirect.
  943. func StatusCodeIsRedirect(statusCode int) bool {
  944. return statusCode == StatusMovedPermanently ||
  945. statusCode == StatusFound ||
  946. statusCode == StatusSeeOther ||
  947. statusCode == StatusTemporaryRedirect ||
  948. statusCode == StatusPermanentRedirect
  949. }
  950. var (
  951. requestPool sync.Pool
  952. responsePool sync.Pool
  953. )
  954. // AcquireRequest returns an empty Request instance from request pool.
  955. //
  956. // The returned Request instance may be passed to ReleaseRequest when it is
  957. // no longer needed. This allows Request recycling, reduces GC pressure
  958. // and usually improves performance.
  959. func AcquireRequest() *Request {
  960. v := requestPool.Get()
  961. if v == nil {
  962. return &Request{}
  963. }
  964. return v.(*Request)
  965. }
  966. // ReleaseRequest returns req acquired via AcquireRequest to request pool.
  967. //
  968. // It is forbidden accessing req and/or its' members after returning
  969. // it to request pool.
  970. func ReleaseRequest(req *Request) {
  971. req.Reset()
  972. requestPool.Put(req)
  973. }
  974. // AcquireResponse returns an empty Response instance from response pool.
  975. //
  976. // The returned Response instance may be passed to ReleaseResponse when it is
  977. // no longer needed. This allows Response recycling, reduces GC pressure
  978. // and usually improves performance.
  979. func AcquireResponse() *Response {
  980. v := responsePool.Get()
  981. if v == nil {
  982. return &Response{}
  983. }
  984. return v.(*Response)
  985. }
  986. // ReleaseResponse return resp acquired via AcquireResponse to response pool.
  987. //
  988. // It is forbidden accessing resp and/or its' members after returning
  989. // it to response pool.
  990. func ReleaseResponse(resp *Response) {
  991. resp.Reset()
  992. responsePool.Put(resp)
  993. }
  994. // DoTimeout performs the given request and waits for response during
  995. // the given timeout duration.
  996. //
  997. // Request must contain at least non-zero RequestURI with full url (including
  998. // scheme and host) or non-zero Host header + RequestURI.
  999. //
  1000. // The function doesn't follow redirects. Use Get* for following redirects.
  1001. //
  1002. // Response is ignored if resp is nil.
  1003. //
  1004. // ErrTimeout is returned if the response wasn't returned during
  1005. // the given timeout.
  1006. // Immediately returns ErrTimeout if timeout value is negative.
  1007. //
  1008. // ErrNoFreeConns is returned if all HostClient.MaxConns connections
  1009. // to the host are busy.
  1010. //
  1011. // It is recommended obtaining req and resp via AcquireRequest
  1012. // and AcquireResponse in performance-critical code.
  1013. func (c *HostClient) DoTimeout(req *Request, resp *Response, timeout time.Duration) error {
  1014. req.timeout = timeout
  1015. if req.timeout <= 0 {
  1016. return ErrTimeout
  1017. }
  1018. return c.Do(req, resp)
  1019. }
  1020. // DoDeadline performs the given request and waits for response until
  1021. // the given deadline.
  1022. //
  1023. // Request must contain at least non-zero RequestURI with full url (including
  1024. // scheme and host) or non-zero Host header + RequestURI.
  1025. //
  1026. // The function doesn't follow redirects. Use Get* for following redirects.
  1027. //
  1028. // Response is ignored if resp is nil.
  1029. //
  1030. // ErrTimeout is returned if the response wasn't returned until
  1031. // the given deadline.
  1032. // Immediately returns ErrTimeout if the deadline has already been reached.
  1033. //
  1034. // ErrNoFreeConns is returned if all HostClient.MaxConns connections
  1035. // to the host are busy.
  1036. //
  1037. // It is recommended obtaining req and resp via AcquireRequest
  1038. // and AcquireResponse in performance-critical code.
  1039. func (c *HostClient) DoDeadline(req *Request, resp *Response, deadline time.Time) error {
  1040. req.timeout = time.Until(deadline)
  1041. if req.timeout <= 0 {
  1042. return ErrTimeout
  1043. }
  1044. return c.Do(req, resp)
  1045. }
  1046. // DoRedirects performs the given http request and fills the given http response,
  1047. // following up to maxRedirectsCount redirects. When the redirect count exceeds
  1048. // maxRedirectsCount, ErrTooManyRedirects is returned.
  1049. //
  1050. // Request must contain at least non-zero RequestURI with full url (including
  1051. // scheme and host) or non-zero Host header + RequestURI.
  1052. //
  1053. // Client determines the server to be requested in the following order:
  1054. //
  1055. // - from RequestURI if it contains full url with scheme and host;
  1056. // - from Host header otherwise.
  1057. //
  1058. // Response is ignored if resp is nil.
  1059. //
  1060. // ErrNoFreeConns is returned if all DefaultMaxConnsPerHost connections
  1061. // to the requested host are busy.
  1062. //
  1063. // It is recommended obtaining req and resp via AcquireRequest
  1064. // and AcquireResponse in performance-critical code.
  1065. func (c *HostClient) DoRedirects(req *Request, resp *Response, maxRedirectsCount int) error {
  1066. _, _, err := doRequestFollowRedirects(req, resp, req.URI().String(), maxRedirectsCount, c)
  1067. return err
  1068. }
  1069. // Do performs the given http request and sets the corresponding response.
  1070. //
  1071. // Request must contain at least non-zero RequestURI with full url (including
  1072. // scheme and host) or non-zero Host header + RequestURI.
  1073. //
  1074. // The function doesn't follow redirects. Use Get* for following redirects.
  1075. //
  1076. // Response is ignored if resp is nil.
  1077. //
  1078. // ErrNoFreeConns is returned if all HostClient.MaxConns connections
  1079. // to the host are busy.
  1080. //
  1081. // It is recommended obtaining req and resp via AcquireRequest
  1082. // and AcquireResponse in performance-critical code.
  1083. func (c *HostClient) Do(req *Request, resp *Response) error {
  1084. var err error
  1085. var retry bool
  1086. maxAttempts := c.MaxIdemponentCallAttempts
  1087. if maxAttempts <= 0 {
  1088. maxAttempts = DefaultMaxIdemponentCallAttempts
  1089. }
  1090. isRequestRetryable := isIdempotent
  1091. if c.RetryIf != nil {
  1092. isRequestRetryable = c.RetryIf
  1093. }
  1094. attempts := 0
  1095. hasBodyStream := req.IsBodyStream()
  1096. // If a request has a timeout we store the timeout
  1097. // and calculate a deadline so we can keep updating the
  1098. // timeout on each retry.
  1099. deadline := time.Time{}
  1100. timeout := req.timeout
  1101. if timeout > 0 {
  1102. deadline = time.Now().Add(timeout)
  1103. }
  1104. atomic.AddInt32(&c.pendingRequests, 1)
  1105. for {
  1106. // If the original timeout was set, we need to update
  1107. // the one set on the request to reflect the remaining time.
  1108. if timeout > 0 {
  1109. req.timeout = time.Until(deadline)
  1110. if req.timeout <= 0 {
  1111. err = ErrTimeout
  1112. break
  1113. }
  1114. }
  1115. retry, err = c.do(req, resp)
  1116. if err == nil || !retry {
  1117. break
  1118. }
  1119. if hasBodyStream {
  1120. break
  1121. }
  1122. if !isRequestRetryable(req) {
  1123. // Retry non-idempotent requests if the server closes
  1124. // the connection before sending the response.
  1125. //
  1126. // This case is possible if the server closes the idle
  1127. // keep-alive connection on timeout.
  1128. //
  1129. // Apache and nginx usually do this.
  1130. if err != io.EOF {
  1131. break
  1132. }
  1133. }
  1134. attempts++
  1135. if attempts >= maxAttempts {
  1136. break
  1137. }
  1138. }
  1139. atomic.AddInt32(&c.pendingRequests, -1)
  1140. // Restore the original timeout.
  1141. req.timeout = timeout
  1142. if err == io.EOF {
  1143. err = ErrConnectionClosed
  1144. }
  1145. return err
  1146. }
  1147. // PendingRequests returns the current number of requests the client
  1148. // is executing.
  1149. //
  1150. // This function may be used for balancing load among multiple HostClient
  1151. // instances.
  1152. func (c *HostClient) PendingRequests() int {
  1153. return int(atomic.LoadInt32(&c.pendingRequests))
  1154. }
  1155. func isIdempotent(req *Request) bool {
  1156. return req.Header.IsGet() || req.Header.IsHead() || req.Header.IsPut()
  1157. }
  1158. func (c *HostClient) do(req *Request, resp *Response) (bool, error) {
  1159. if resp == nil {
  1160. resp = AcquireResponse()
  1161. defer ReleaseResponse(resp)
  1162. }
  1163. ok, err := c.doNonNilReqResp(req, resp)
  1164. return ok, err
  1165. }
  1166. func (c *HostClient) doNonNilReqResp(req *Request, resp *Response) (bool, error) {
  1167. if req == nil {
  1168. // for debugging purposes
  1169. panic("BUG: req cannot be nil")
  1170. }
  1171. if resp == nil {
  1172. // for debugging purposes
  1173. panic("BUG: resp cannot be nil")
  1174. }
  1175. // Secure header error logs configuration
  1176. resp.secureErrorLogMessage = c.SecureErrorLogMessage
  1177. resp.Header.secureErrorLogMessage = c.SecureErrorLogMessage
  1178. req.secureErrorLogMessage = c.SecureErrorLogMessage
  1179. req.Header.secureErrorLogMessage = c.SecureErrorLogMessage
  1180. if c.IsTLS != req.URI().isHTTPS() {
  1181. return false, ErrHostClientRedirectToDifferentScheme
  1182. }
  1183. atomic.StoreUint32(&c.lastUseTime, uint32(time.Now().Unix()-startTimeUnix))
  1184. // Free up resources occupied by response before sending the request,
  1185. // so the GC may reclaim these resources (e.g. response body).
  1186. // backing up SkipBody in case it was set explicitly
  1187. customSkipBody := resp.SkipBody
  1188. customStreamBody := resp.StreamBody || c.StreamResponseBody
  1189. resp.Reset()
  1190. resp.SkipBody = customSkipBody
  1191. resp.StreamBody = customStreamBody
  1192. req.URI().DisablePathNormalizing = c.DisablePathNormalizing
  1193. userAgentOld := req.Header.UserAgent()
  1194. if len(userAgentOld) == 0 {
  1195. userAgent := c.Name
  1196. if userAgent == "" && !c.NoDefaultUserAgentHeader {
  1197. userAgent = defaultUserAgent
  1198. }
  1199. if userAgent != "" {
  1200. req.Header.userAgent = append(req.Header.userAgent[:0], userAgent...)
  1201. }
  1202. }
  1203. return c.transport().RoundTrip(c, req, resp)
  1204. }
  1205. func (c *HostClient) transport() RoundTripper {
  1206. if c.Transport == nil {
  1207. return DefaultTransport
  1208. }
  1209. return c.Transport
  1210. }
  1211. var (
  1212. // ErrNoFreeConns is returned when no free connections available
  1213. // to the given host.
  1214. //
  1215. // Increase the allowed number of connections per host if you
  1216. // see this error.
  1217. ErrNoFreeConns = errors.New("no free connections available to host")
  1218. // ErrConnectionClosed may be returned from client methods if the server
  1219. // closes connection before returning the first response byte.
  1220. //
  1221. // If you see this error, then either fix the server by returning
  1222. // 'Connection: close' response header before closing the connection
  1223. // or add 'Connection: close' request header before sending requests
  1224. // to broken server.
  1225. ErrConnectionClosed = errors.New("the server closed connection before returning the first response byte. " +
  1226. "Make sure the server returns 'Connection: close' response header before closing the connection")
  1227. // ErrConnPoolStrategyNotImpl is returned when HostClient.ConnPoolStrategy is not implement yet.
  1228. // If you see this error, then you need to check your HostClient configuration.
  1229. ErrConnPoolStrategyNotImpl = errors.New("connection pool strategy is not implement")
  1230. )
  1231. type timeoutError struct{}
  1232. func (e *timeoutError) Error() string {
  1233. return "timeout"
  1234. }
  1235. // Only implement the Timeout() function of the net.Error interface.
  1236. // This allows for checks like:
  1237. //
  1238. // if x, ok := err.(interface{ Timeout() bool }); ok && x.Timeout() {
  1239. func (e *timeoutError) Timeout() bool {
  1240. return true
  1241. }
  1242. // ErrTimeout is returned from timed out calls.
  1243. var ErrTimeout = &timeoutError{}
  1244. // SetMaxConns sets up the maximum number of connections which may be established to all hosts listed in Addr.
  1245. func (c *HostClient) SetMaxConns(newMaxConns int) {
  1246. c.connsLock.Lock()
  1247. c.MaxConns = newMaxConns
  1248. c.connsLock.Unlock()
  1249. }
  1250. func (c *HostClient) acquireConn(reqTimeout time.Duration, connectionClose bool) (cc *clientConn, err error) {
  1251. createConn := false
  1252. startCleaner := false
  1253. var n int
  1254. c.connsLock.Lock()
  1255. n = len(c.conns)
  1256. if n == 0 {
  1257. maxConns := c.MaxConns
  1258. if maxConns <= 0 {
  1259. maxConns = DefaultMaxConnsPerHost
  1260. }
  1261. if c.connsCount < maxConns {
  1262. c.connsCount++
  1263. createConn = true
  1264. if !c.connsCleanerRun && !connectionClose {
  1265. startCleaner = true
  1266. c.connsCleanerRun = true
  1267. }
  1268. }
  1269. } else {
  1270. switch c.ConnPoolStrategy {
  1271. case LIFO:
  1272. n--
  1273. cc = c.conns[n]
  1274. c.conns[n] = nil
  1275. c.conns = c.conns[:n]
  1276. case FIFO:
  1277. cc = c.conns[0]
  1278. copy(c.conns, c.conns[1:])
  1279. c.conns[n-1] = nil
  1280. c.conns = c.conns[:n-1]
  1281. default:
  1282. c.connsLock.Unlock()
  1283. return nil, ErrConnPoolStrategyNotImpl
  1284. }
  1285. }
  1286. c.connsLock.Unlock()
  1287. if cc != nil {
  1288. return cc, nil
  1289. }
  1290. if !createConn {
  1291. if c.MaxConnWaitTimeout <= 0 {
  1292. return nil, ErrNoFreeConns
  1293. }
  1294. // reqTimeout c.MaxConnWaitTimeout wait duration
  1295. // d1 d2 min(d1, d2)
  1296. // 0(not set) d2 d2
  1297. // d1 0(don't wait) 0(don't wait)
  1298. // 0(not set) d2 d2
  1299. timeout := c.MaxConnWaitTimeout
  1300. timeoutOverridden := false
  1301. // reqTimeout == 0 means not set
  1302. if reqTimeout > 0 && reqTimeout < timeout {
  1303. timeout = reqTimeout
  1304. timeoutOverridden = true
  1305. }
  1306. // wait for a free connection
  1307. tc := AcquireTimer(timeout)
  1308. defer ReleaseTimer(tc)
  1309. w := &wantConn{
  1310. ready: make(chan struct{}, 1),
  1311. }
  1312. defer func() {
  1313. if err != nil {
  1314. w.cancel(c, err)
  1315. }
  1316. }()
  1317. c.queueForIdle(w)
  1318. select {
  1319. case <-w.ready:
  1320. return w.conn, w.err
  1321. case <-tc.C:
  1322. if timeoutOverridden {
  1323. return nil, ErrTimeout
  1324. }
  1325. return nil, ErrNoFreeConns
  1326. }
  1327. }
  1328. if startCleaner {
  1329. go c.connsCleaner()
  1330. }
  1331. conn, err := c.dialHostHard(reqTimeout)
  1332. if err != nil {
  1333. c.decConnsCount()
  1334. return nil, err
  1335. }
  1336. cc = acquireClientConn(conn)
  1337. return cc, nil
  1338. }
  1339. func (c *HostClient) queueForIdle(w *wantConn) {
  1340. c.connsLock.Lock()
  1341. defer c.connsLock.Unlock()
  1342. if c.connsWait == nil {
  1343. c.connsWait = &wantConnQueue{}
  1344. }
  1345. c.connsWait.clearFront()
  1346. c.connsWait.pushBack(w)
  1347. }
  1348. func (c *HostClient) dialConnFor(w *wantConn) {
  1349. conn, err := c.dialHostHard(0)
  1350. if err != nil {
  1351. w.tryDeliver(nil, err)
  1352. c.decConnsCount()
  1353. return
  1354. }
  1355. cc := acquireClientConn(conn)
  1356. if !w.tryDeliver(cc, nil) {
  1357. // not delivered, return idle connection
  1358. c.releaseConn(cc)
  1359. }
  1360. }
  1361. // CloseIdleConnections closes any connections which were previously
  1362. // connected from previous requests but are now sitting idle in a
  1363. // "keep-alive" state. It does not interrupt any connections currently
  1364. // in use.
  1365. func (c *HostClient) CloseIdleConnections() {
  1366. c.connsLock.Lock()
  1367. scratch := append([]*clientConn{}, c.conns...)
  1368. for i := range c.conns {
  1369. c.conns[i] = nil
  1370. }
  1371. c.conns = c.conns[:0]
  1372. c.connsLock.Unlock()
  1373. for _, cc := range scratch {
  1374. c.closeConn(cc)
  1375. }
  1376. }
  1377. func (c *HostClient) connsCleaner() {
  1378. var (
  1379. scratch []*clientConn
  1380. maxIdleConnDuration = c.MaxIdleConnDuration
  1381. )
  1382. if maxIdleConnDuration <= 0 {
  1383. maxIdleConnDuration = DefaultMaxIdleConnDuration
  1384. }
  1385. for {
  1386. currentTime := time.Now()
  1387. // Determine idle connections to be closed.
  1388. c.connsLock.Lock()
  1389. conns := c.conns
  1390. n := len(conns)
  1391. i := 0
  1392. for i < n && currentTime.Sub(conns[i].lastUseTime) > maxIdleConnDuration {
  1393. i++
  1394. }
  1395. sleepFor := maxIdleConnDuration
  1396. if i < n {
  1397. // + 1 so we actually sleep past the expiration time and not up to it.
  1398. // Otherwise the > check above would still fail.
  1399. sleepFor = maxIdleConnDuration - currentTime.Sub(conns[i].lastUseTime) + 1
  1400. }
  1401. scratch = append(scratch[:0], conns[:i]...)
  1402. if i > 0 {
  1403. m := copy(conns, conns[i:])
  1404. for i = m; i < n; i++ {
  1405. conns[i] = nil
  1406. }
  1407. c.conns = conns[:m]
  1408. }
  1409. c.connsLock.Unlock()
  1410. // Close idle connections.
  1411. for i, cc := range scratch {
  1412. c.closeConn(cc)
  1413. scratch[i] = nil
  1414. }
  1415. // Determine whether to stop the connsCleaner.
  1416. c.connsLock.Lock()
  1417. mustStop := c.connsCount == 0
  1418. if mustStop {
  1419. c.connsCleanerRun = false
  1420. }
  1421. c.connsLock.Unlock()
  1422. if mustStop {
  1423. break
  1424. }
  1425. time.Sleep(sleepFor)
  1426. }
  1427. }
  1428. func (c *HostClient) closeConn(cc *clientConn) {
  1429. c.decConnsCount()
  1430. cc.c.Close()
  1431. releaseClientConn(cc)
  1432. }
  1433. func (c *HostClient) decConnsCount() {
  1434. if c.MaxConnWaitTimeout <= 0 {
  1435. c.connsLock.Lock()
  1436. c.connsCount--
  1437. c.connsLock.Unlock()
  1438. return
  1439. }
  1440. c.connsLock.Lock()
  1441. defer c.connsLock.Unlock()
  1442. dialed := false
  1443. if q := c.connsWait; q != nil && q.len() > 0 {
  1444. for q.len() > 0 {
  1445. w := q.popFront()
  1446. if w.waiting() {
  1447. go c.dialConnFor(w)
  1448. dialed = true
  1449. break
  1450. }
  1451. }
  1452. }
  1453. if !dialed {
  1454. c.connsCount--
  1455. }
  1456. }
  1457. // ConnsCount returns connection count of HostClient
  1458. func (c *HostClient) ConnsCount() int {
  1459. c.connsLock.Lock()
  1460. defer c.connsLock.Unlock()
  1461. return c.connsCount
  1462. }
  1463. func acquireClientConn(conn net.Conn) *clientConn {
  1464. v := clientConnPool.Get()
  1465. if v == nil {
  1466. v = &clientConn{}
  1467. }
  1468. cc := v.(*clientConn)
  1469. cc.c = conn
  1470. cc.createdTime = time.Now()
  1471. return cc
  1472. }
  1473. func releaseClientConn(cc *clientConn) {
  1474. // Reset all fields.
  1475. *cc = clientConn{}
  1476. clientConnPool.Put(cc)
  1477. }
  1478. var clientConnPool sync.Pool
  1479. func (c *HostClient) releaseConn(cc *clientConn) {
  1480. cc.lastUseTime = time.Now()
  1481. if c.MaxConnWaitTimeout <= 0 {
  1482. c.connsLock.Lock()
  1483. c.conns = append(c.conns, cc)
  1484. c.connsLock.Unlock()
  1485. return
  1486. }
  1487. // try to deliver an idle connection to a *wantConn
  1488. c.connsLock.Lock()
  1489. defer c.connsLock.Unlock()
  1490. delivered := false
  1491. if q := c.connsWait; q != nil && q.len() > 0 {
  1492. for q.len() > 0 {
  1493. w := q.popFront()
  1494. if w.waiting() {
  1495. delivered = w.tryDeliver(cc, nil)
  1496. break
  1497. }
  1498. }
  1499. }
  1500. if !delivered {
  1501. c.conns = append(c.conns, cc)
  1502. }
  1503. }
  1504. func (c *HostClient) acquireWriter(conn net.Conn) *bufio.Writer {
  1505. var v interface{}
  1506. if c.clientWriterPool != nil {
  1507. v = c.clientWriterPool.Get()
  1508. if v == nil {
  1509. n := c.WriteBufferSize
  1510. if n <= 0 {
  1511. n = defaultWriteBufferSize
  1512. }
  1513. return bufio.NewWriterSize(conn, n)
  1514. }
  1515. } else {
  1516. v = c.writerPool.Get()
  1517. if v == nil {
  1518. n := c.WriteBufferSize
  1519. if n <= 0 {
  1520. n = defaultWriteBufferSize
  1521. }
  1522. return bufio.NewWriterSize(conn, n)
  1523. }
  1524. }
  1525. bw := v.(*bufio.Writer)
  1526. bw.Reset(conn)
  1527. return bw
  1528. }
  1529. func (c *HostClient) releaseWriter(bw *bufio.Writer) {
  1530. if c.clientWriterPool != nil {
  1531. c.clientWriterPool.Put(bw)
  1532. } else {
  1533. c.writerPool.Put(bw)
  1534. }
  1535. }
  1536. func (c *HostClient) acquireReader(conn net.Conn) *bufio.Reader {
  1537. var v interface{}
  1538. if c.clientReaderPool != nil {
  1539. v = c.clientReaderPool.Get()
  1540. if v == nil {
  1541. n := c.ReadBufferSize
  1542. if n <= 0 {
  1543. n = defaultReadBufferSize
  1544. }
  1545. return bufio.NewReaderSize(conn, n)
  1546. }
  1547. } else {
  1548. v = c.readerPool.Get()
  1549. if v == nil {
  1550. n := c.ReadBufferSize
  1551. if n <= 0 {
  1552. n = defaultReadBufferSize
  1553. }
  1554. return bufio.NewReaderSize(conn, n)
  1555. }
  1556. }
  1557. br := v.(*bufio.Reader)
  1558. br.Reset(conn)
  1559. return br
  1560. }
  1561. func (c *HostClient) releaseReader(br *bufio.Reader) {
  1562. if c.clientReaderPool != nil {
  1563. c.clientReaderPool.Put(br)
  1564. } else {
  1565. c.readerPool.Put(br)
  1566. }
  1567. }
  1568. func newClientTLSConfig(c *tls.Config, addr string) *tls.Config {
  1569. if c == nil {
  1570. c = &tls.Config{}
  1571. } else {
  1572. c = c.Clone()
  1573. }
  1574. if len(c.ServerName) == 0 {
  1575. serverName := tlsServerName(addr)
  1576. if serverName == "*" {
  1577. c.InsecureSkipVerify = true
  1578. } else {
  1579. c.ServerName = serverName
  1580. }
  1581. }
  1582. return c
  1583. }
  1584. func tlsServerName(addr string) string {
  1585. if !strings.Contains(addr, ":") {
  1586. return addr
  1587. }
  1588. host, _, err := net.SplitHostPort(addr)
  1589. if err != nil {
  1590. return "*"
  1591. }
  1592. return host
  1593. }
  1594. func (c *HostClient) nextAddr() string {
  1595. c.addrsLock.Lock()
  1596. if c.addrs == nil {
  1597. c.addrs = strings.Split(c.Addr, ",")
  1598. }
  1599. addr := c.addrs[0]
  1600. if len(c.addrs) > 1 {
  1601. addr = c.addrs[c.addrIdx%uint32(len(c.addrs))]
  1602. c.addrIdx++
  1603. }
  1604. c.addrsLock.Unlock()
  1605. return addr
  1606. }
  1607. func (c *HostClient) dialHostHard(dialTimeout time.Duration) (conn net.Conn, err error) {
  1608. // use dialTimeout to control the timeout of each dial. It does not work if dialTimeout is 0 or dial has been set.
  1609. // attempt to dial all the available hosts before giving up.
  1610. c.addrsLock.Lock()
  1611. n := len(c.addrs)
  1612. c.addrsLock.Unlock()
  1613. if n == 0 {
  1614. // It looks like c.addrs isn't initialized yet.
  1615. n = 1
  1616. }
  1617. dial := c.Dial
  1618. if dialTimeout != 0 && dial == nil {
  1619. dial = func(addr string) (net.Conn, error) {
  1620. return DialTimeout(addr, dialTimeout)
  1621. }
  1622. }
  1623. timeout := c.ReadTimeout + c.WriteTimeout
  1624. if timeout <= 0 {
  1625. timeout = DefaultDialTimeout
  1626. }
  1627. deadline := time.Now().Add(timeout)
  1628. for n > 0 {
  1629. addr := c.nextAddr()
  1630. tlsConfig := c.cachedTLSConfig(addr)
  1631. conn, err = dialAddr(addr, dial, c.DialDualStack, c.IsTLS, tlsConfig, c.WriteTimeout)
  1632. if err == nil {
  1633. return conn, nil
  1634. }
  1635. if time.Since(deadline) >= 0 {
  1636. break
  1637. }
  1638. n--
  1639. }
  1640. return nil, err
  1641. }
  1642. func (c *HostClient) cachedTLSConfig(addr string) *tls.Config {
  1643. if !c.IsTLS {
  1644. return nil
  1645. }
  1646. c.tlsConfigMapLock.Lock()
  1647. if c.tlsConfigMap == nil {
  1648. c.tlsConfigMap = make(map[string]*tls.Config)
  1649. }
  1650. cfg := c.tlsConfigMap[addr]
  1651. if cfg == nil {
  1652. cfg = newClientTLSConfig(c.TLSConfig, addr)
  1653. c.tlsConfigMap[addr] = cfg
  1654. }
  1655. c.tlsConfigMapLock.Unlock()
  1656. return cfg
  1657. }
  1658. // ErrTLSHandshakeTimeout indicates there is a timeout from tls handshake.
  1659. var ErrTLSHandshakeTimeout = errors.New("tls handshake timed out")
  1660. func tlsClientHandshake(rawConn net.Conn, tlsConfig *tls.Config, deadline time.Time) (_ net.Conn, retErr error) {
  1661. defer func() {
  1662. if retErr != nil {
  1663. rawConn.Close()
  1664. }
  1665. }()
  1666. conn := tls.Client(rawConn, tlsConfig)
  1667. err := conn.SetDeadline(deadline)
  1668. if err != nil {
  1669. return nil, err
  1670. }
  1671. err = conn.Handshake()
  1672. if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
  1673. return nil, ErrTLSHandshakeTimeout
  1674. }
  1675. if err != nil {
  1676. return nil, err
  1677. }
  1678. err = conn.SetDeadline(time.Time{})
  1679. if err != nil {
  1680. return nil, err
  1681. }
  1682. return conn, nil
  1683. }
  1684. func dialAddr(addr string, dial DialFunc, dialDualStack, isTLS bool, tlsConfig *tls.Config, timeout time.Duration) (net.Conn, error) {
  1685. deadline := time.Now().Add(timeout)
  1686. if dial == nil {
  1687. if dialDualStack {
  1688. dial = DialDualStack
  1689. } else {
  1690. dial = Dial
  1691. }
  1692. addr = AddMissingPort(addr, isTLS)
  1693. }
  1694. conn, err := dial(addr)
  1695. if err != nil {
  1696. return nil, err
  1697. }
  1698. if conn == nil {
  1699. return nil, errors.New("dialling unsuccessful. Please report this bug!")
  1700. }
  1701. // We assume that any conn that has the Handshake() method is a TLS conn already.
  1702. // This doesn't cover just tls.Conn but also other TLS implementations.
  1703. _, isTLSAlready := conn.(interface{ Handshake() error })
  1704. if isTLS && !isTLSAlready {
  1705. if timeout == 0 {
  1706. return tls.Client(conn, tlsConfig), nil
  1707. }
  1708. return tlsClientHandshake(conn, tlsConfig, deadline)
  1709. }
  1710. return conn, nil
  1711. }
  1712. // AddMissingPort adds a port to a host if it is missing.
  1713. // A literal IPv6 address in hostport must be enclosed in square
  1714. // brackets, as in "[::1]:80", "[::1%lo0]:80".
  1715. func AddMissingPort(addr string, isTLS bool) string {
  1716. addrLen := len(addr)
  1717. if addrLen == 0 {
  1718. return addr
  1719. }
  1720. isIP6 := addr[0] == '['
  1721. if isIP6 {
  1722. // if the IPv6 has opening bracket but closing bracket is the last char then it doesn't have a port
  1723. isIP6WithoutPort := addr[addrLen-1] == ']'
  1724. if !isIP6WithoutPort {
  1725. return addr
  1726. }
  1727. } else { // IPv4
  1728. columnPos := strings.LastIndexByte(addr, ':')
  1729. if columnPos > 0 {
  1730. return addr
  1731. }
  1732. }
  1733. port := ":80"
  1734. if isTLS {
  1735. port = ":443"
  1736. }
  1737. return addr + port
  1738. }
  1739. // A wantConn records state about a wanted connection
  1740. // (that is, an active call to getConn).
  1741. // The conn may be gotten by dialing or by finding an idle connection,
  1742. // or a cancellation may make the conn no longer wanted.
  1743. // These three options are racing against each other and use
  1744. // wantConn to coordinate and agree about the winning outcome.
  1745. //
  1746. // inspired by net/http/transport.go
  1747. type wantConn struct {
  1748. ready chan struct{}
  1749. mu sync.Mutex // protects conn, err, close(ready)
  1750. conn *clientConn
  1751. err error
  1752. }
  1753. // waiting reports whether w is still waiting for an answer (connection or error).
  1754. func (w *wantConn) waiting() bool {
  1755. select {
  1756. case <-w.ready:
  1757. return false
  1758. default:
  1759. return true
  1760. }
  1761. }
  1762. // tryDeliver attempts to deliver conn, err to w and reports whether it succeeded.
  1763. func (w *wantConn) tryDeliver(conn *clientConn, err error) bool {
  1764. w.mu.Lock()
  1765. defer w.mu.Unlock()
  1766. if w.conn != nil || w.err != nil {
  1767. return false
  1768. }
  1769. w.conn = conn
  1770. w.err = err
  1771. if w.conn == nil && w.err == nil {
  1772. panic("fasthttp: internal error: misuse of tryDeliver")
  1773. }
  1774. close(w.ready)
  1775. return true
  1776. }
  1777. // cancel marks w as no longer wanting a result (for example, due to cancellation).
  1778. // If a connection has been delivered already, cancel returns it with c.releaseConn.
  1779. func (w *wantConn) cancel(c *HostClient, err error) {
  1780. w.mu.Lock()
  1781. if w.conn == nil && w.err == nil {
  1782. close(w.ready) // catch misbehavior in future delivery
  1783. }
  1784. conn := w.conn
  1785. w.conn = nil
  1786. w.err = err
  1787. w.mu.Unlock()
  1788. if conn != nil {
  1789. c.releaseConn(conn)
  1790. }
  1791. }
  1792. // A wantConnQueue is a queue of wantConns.
  1793. //
  1794. // inspired by net/http/transport.go
  1795. type wantConnQueue struct {
  1796. // This is a queue, not a dequeue.
  1797. // It is split into two stages - head[headPos:] and tail.
  1798. // popFront is trivial (headPos++) on the first stage, and
  1799. // pushBack is trivial (append) on the second stage.
  1800. // If the first stage is empty, popFront can swap the
  1801. // first and second stages to remedy the situation.
  1802. //
  1803. // This two-stage split is analogous to the use of two lists
  1804. // in Okasaki's purely functional queue but without the
  1805. // overhead of reversing the list when swapping stages.
  1806. head []*wantConn
  1807. headPos int
  1808. tail []*wantConn
  1809. }
  1810. // len returns the number of items in the queue.
  1811. func (q *wantConnQueue) len() int {
  1812. return len(q.head) - q.headPos + len(q.tail)
  1813. }
  1814. // pushBack adds w to the back of the queue.
  1815. func (q *wantConnQueue) pushBack(w *wantConn) {
  1816. q.tail = append(q.tail, w)
  1817. }
  1818. // popFront removes and returns the wantConn at the front of the queue.
  1819. func (q *wantConnQueue) popFront() *wantConn {
  1820. if q.headPos >= len(q.head) {
  1821. if len(q.tail) == 0 {
  1822. return nil
  1823. }
  1824. // Pick up tail as new head, clear tail.
  1825. q.head, q.headPos, q.tail = q.tail, 0, q.head[:0]
  1826. }
  1827. w := q.head[q.headPos]
  1828. q.head[q.headPos] = nil
  1829. q.headPos++
  1830. return w
  1831. }
  1832. // peekFront returns the wantConn at the front of the queue without removing it.
  1833. func (q *wantConnQueue) peekFront() *wantConn {
  1834. if q.headPos < len(q.head) {
  1835. return q.head[q.headPos]
  1836. }
  1837. if len(q.tail) > 0 {
  1838. return q.tail[0]
  1839. }
  1840. return nil
  1841. }
  1842. // clearFront pops any wantConns that are no longer waiting from the head of the
  1843. // queue, reporting whether any were popped.
  1844. func (q *wantConnQueue) clearFront() (cleaned bool) {
  1845. for {
  1846. w := q.peekFront()
  1847. if w == nil || w.waiting() {
  1848. return cleaned
  1849. }
  1850. q.popFront()
  1851. cleaned = true
  1852. }
  1853. }
  1854. // PipelineClient pipelines requests over a limited set of concurrent
  1855. // connections to the given Addr.
  1856. //
  1857. // This client may be used in highly loaded HTTP-based RPC systems for reducing
  1858. // context switches and network level overhead.
  1859. // See https://en.wikipedia.org/wiki/HTTP_pipelining for details.
  1860. //
  1861. // It is forbidden copying PipelineClient instances. Create new instances
  1862. // instead.
  1863. //
  1864. // It is safe calling PipelineClient methods from concurrently running
  1865. // goroutines.
  1866. type PipelineClient struct {
  1867. noCopy noCopy
  1868. // Address of the host to connect to.
  1869. Addr string
  1870. // PipelineClient name. Used in User-Agent request header.
  1871. Name string
  1872. // NoDefaultUserAgentHeader when set to true, causes the default
  1873. // User-Agent header to be excluded from the Request.
  1874. NoDefaultUserAgentHeader bool
  1875. // The maximum number of concurrent connections to the Addr.
  1876. //
  1877. // A single connection is used by default.
  1878. MaxConns int
  1879. // The maximum number of pending pipelined requests over
  1880. // a single connection to Addr.
  1881. //
  1882. // DefaultMaxPendingRequests is used by default.
  1883. MaxPendingRequests int
  1884. // The maximum delay before sending pipelined requests as a batch
  1885. // to the server.
  1886. //
  1887. // By default requests are sent immediately to the server.
  1888. MaxBatchDelay time.Duration
  1889. // Callback for connection establishing to the host.
  1890. //
  1891. // Default Dial is used if not set.
  1892. Dial DialFunc
  1893. // Attempt to connect to both ipv4 and ipv6 host addresses
  1894. // if set to true.
  1895. //
  1896. // This option is used only if default TCP dialer is used,
  1897. // i.e. if Dial is blank.
  1898. //
  1899. // By default client connects only to ipv4 addresses,
  1900. // since unfortunately ipv6 remains broken in many networks worldwide :)
  1901. DialDualStack bool
  1902. // Response header names are passed as-is without normalization
  1903. // if this option is set.
  1904. //
  1905. // Disabled header names' normalization may be useful only for proxying
  1906. // responses to other clients expecting case-sensitive
  1907. // header names. See https://github.com/valyala/fasthttp/issues/57
  1908. // for details.
  1909. //
  1910. // By default request and response header names are normalized, i.e.
  1911. // The first letter and the first letters following dashes
  1912. // are uppercased, while all the other letters are lowercased.
  1913. // Examples:
  1914. //
  1915. // * HOST -> Host
  1916. // * content-type -> Content-Type
  1917. // * cONTENT-lenGTH -> Content-Length
  1918. DisableHeaderNamesNormalizing bool
  1919. // Path values are sent as-is without normalization
  1920. //
  1921. // Disabled path normalization may be useful for proxying incoming requests
  1922. // to servers that are expecting paths to be forwarded as-is.
  1923. //
  1924. // By default path values are normalized, i.e.
  1925. // extra slashes are removed, special characters are encoded.
  1926. DisablePathNormalizing bool
  1927. // Whether to use TLS (aka SSL or HTTPS) for host connections.
  1928. IsTLS bool
  1929. // Optional TLS config.
  1930. TLSConfig *tls.Config
  1931. // Idle connection to the host is closed after this duration.
  1932. //
  1933. // By default idle connection is closed after
  1934. // DefaultMaxIdleConnDuration.
  1935. MaxIdleConnDuration time.Duration
  1936. // Buffer size for responses' reading.
  1937. // This also limits the maximum header size.
  1938. //
  1939. // Default buffer size is used if 0.
  1940. ReadBufferSize int
  1941. // Buffer size for requests' writing.
  1942. //
  1943. // Default buffer size is used if 0.
  1944. WriteBufferSize int
  1945. // Maximum duration for full response reading (including body).
  1946. //
  1947. // By default response read timeout is unlimited.
  1948. ReadTimeout time.Duration
  1949. // Maximum duration for full request writing (including body).
  1950. //
  1951. // By default request write timeout is unlimited.
  1952. WriteTimeout time.Duration
  1953. // Logger for logging client errors.
  1954. //
  1955. // By default standard logger from log package is used.
  1956. Logger Logger
  1957. connClients []*pipelineConnClient
  1958. connClientsLock sync.Mutex
  1959. }
  1960. type pipelineConnClient struct {
  1961. noCopy noCopy
  1962. Addr string
  1963. Name string
  1964. NoDefaultUserAgentHeader bool
  1965. MaxPendingRequests int
  1966. MaxBatchDelay time.Duration
  1967. Dial DialFunc
  1968. DialDualStack bool
  1969. DisableHeaderNamesNormalizing bool
  1970. DisablePathNormalizing bool
  1971. IsTLS bool
  1972. TLSConfig *tls.Config
  1973. MaxIdleConnDuration time.Duration
  1974. ReadBufferSize int
  1975. WriteBufferSize int
  1976. ReadTimeout time.Duration
  1977. WriteTimeout time.Duration
  1978. Logger Logger
  1979. workPool sync.Pool
  1980. chLock sync.Mutex
  1981. chW chan *pipelineWork
  1982. chR chan *pipelineWork
  1983. tlsConfigLock sync.Mutex
  1984. tlsConfig *tls.Config
  1985. }
  1986. type pipelineWork struct {
  1987. reqCopy Request
  1988. respCopy Response
  1989. req *Request
  1990. resp *Response
  1991. t *time.Timer
  1992. deadline time.Time
  1993. err error
  1994. done chan struct{}
  1995. }
  1996. // DoTimeout performs the given request and waits for response during
  1997. // the given timeout duration.
  1998. //
  1999. // Request must contain at least non-zero RequestURI with full url (including
  2000. // scheme and host) or non-zero Host header + RequestURI.
  2001. //
  2002. // The function doesn't follow redirects.
  2003. //
  2004. // Response is ignored if resp is nil.
  2005. //
  2006. // ErrTimeout is returned if the response wasn't returned during
  2007. // the given timeout.
  2008. //
  2009. // It is recommended obtaining req and resp via AcquireRequest
  2010. // and AcquireResponse in performance-critical code.
  2011. func (c *PipelineClient) DoTimeout(req *Request, resp *Response, timeout time.Duration) error {
  2012. return c.DoDeadline(req, resp, time.Now().Add(timeout))
  2013. }
  2014. // DoDeadline performs the given request and waits for response until
  2015. // the given deadline.
  2016. //
  2017. // Request must contain at least non-zero RequestURI with full url (including
  2018. // scheme and host) or non-zero Host header + RequestURI.
  2019. //
  2020. // The function doesn't follow redirects.
  2021. //
  2022. // Response is ignored if resp is nil.
  2023. //
  2024. // ErrTimeout is returned if the response wasn't returned until
  2025. // the given deadline.
  2026. //
  2027. // It is recommended obtaining req and resp via AcquireRequest
  2028. // and AcquireResponse in performance-critical code.
  2029. func (c *PipelineClient) DoDeadline(req *Request, resp *Response, deadline time.Time) error {
  2030. return c.getConnClient().DoDeadline(req, resp, deadline)
  2031. }
  2032. func (c *pipelineConnClient) DoDeadline(req *Request, resp *Response, deadline time.Time) error {
  2033. c.init()
  2034. timeout := time.Until(deadline)
  2035. if timeout <= 0 {
  2036. return ErrTimeout
  2037. }
  2038. if c.DisablePathNormalizing {
  2039. req.URI().DisablePathNormalizing = true
  2040. }
  2041. userAgentOld := req.Header.UserAgent()
  2042. if len(userAgentOld) == 0 {
  2043. userAgent := c.Name
  2044. if userAgent == "" && !c.NoDefaultUserAgentHeader {
  2045. userAgent = defaultUserAgent
  2046. }
  2047. if userAgent != "" {
  2048. req.Header.userAgent = append(req.Header.userAgent[:0], userAgent...)
  2049. }
  2050. }
  2051. w := c.acquirePipelineWork(timeout)
  2052. w.respCopy.Header.disableNormalizing = c.DisableHeaderNamesNormalizing
  2053. w.req = &w.reqCopy
  2054. w.resp = &w.respCopy
  2055. // Make a copy of the request in order to avoid data races on timeouts
  2056. req.copyToSkipBody(&w.reqCopy)
  2057. swapRequestBody(req, &w.reqCopy)
  2058. // Put the request to outgoing queue
  2059. select {
  2060. case c.chW <- w:
  2061. // Fast path: len(c.ch) < cap(c.ch)
  2062. default:
  2063. // Slow path
  2064. select {
  2065. case c.chW <- w:
  2066. case <-w.t.C:
  2067. c.releasePipelineWork(w)
  2068. return ErrTimeout
  2069. }
  2070. }
  2071. // Wait for the response
  2072. var err error
  2073. select {
  2074. case <-w.done:
  2075. if resp != nil {
  2076. w.respCopy.copyToSkipBody(resp)
  2077. swapResponseBody(resp, &w.respCopy)
  2078. }
  2079. err = w.err
  2080. c.releasePipelineWork(w)
  2081. case <-w.t.C:
  2082. err = ErrTimeout
  2083. }
  2084. return err
  2085. }
  2086. func (c *pipelineConnClient) acquirePipelineWork(timeout time.Duration) (w *pipelineWork) {
  2087. v := c.workPool.Get()
  2088. if v != nil {
  2089. w = v.(*pipelineWork)
  2090. } else {
  2091. w = &pipelineWork{
  2092. done: make(chan struct{}, 1),
  2093. }
  2094. }
  2095. if timeout > 0 {
  2096. if w.t == nil {
  2097. w.t = time.NewTimer(timeout)
  2098. } else {
  2099. w.t.Reset(timeout)
  2100. }
  2101. w.deadline = time.Now().Add(timeout)
  2102. } else {
  2103. w.deadline = zeroTime
  2104. }
  2105. return w
  2106. }
  2107. func (c *pipelineConnClient) releasePipelineWork(w *pipelineWork) {
  2108. if w.t != nil {
  2109. w.t.Stop()
  2110. }
  2111. w.reqCopy.Reset()
  2112. w.respCopy.Reset()
  2113. w.req = nil
  2114. w.resp = nil
  2115. w.err = nil
  2116. c.workPool.Put(w)
  2117. }
  2118. // Do performs the given http request and sets the corresponding response.
  2119. //
  2120. // Request must contain at least non-zero RequestURI with full url (including
  2121. // scheme and host) or non-zero Host header + RequestURI.
  2122. //
  2123. // The function doesn't follow redirects. Use Get* for following redirects.
  2124. //
  2125. // Response is ignored if resp is nil.
  2126. //
  2127. // It is recommended obtaining req and resp via AcquireRequest
  2128. // and AcquireResponse in performance-critical code.
  2129. func (c *PipelineClient) Do(req *Request, resp *Response) error {
  2130. return c.getConnClient().Do(req, resp)
  2131. }
  2132. func (c *pipelineConnClient) Do(req *Request, resp *Response) error {
  2133. c.init()
  2134. if c.DisablePathNormalizing {
  2135. req.URI().DisablePathNormalizing = true
  2136. }
  2137. userAgentOld := req.Header.UserAgent()
  2138. if len(userAgentOld) == 0 {
  2139. userAgent := c.Name
  2140. if userAgent == "" && !c.NoDefaultUserAgentHeader {
  2141. userAgent = defaultUserAgent
  2142. }
  2143. if userAgent != "" {
  2144. req.Header.userAgent = append(req.Header.userAgent[:0], userAgent...)
  2145. }
  2146. }
  2147. w := c.acquirePipelineWork(0)
  2148. w.req = req
  2149. if resp != nil {
  2150. resp.Header.disableNormalizing = c.DisableHeaderNamesNormalizing
  2151. w.resp = resp
  2152. } else {
  2153. w.resp = &w.respCopy
  2154. }
  2155. // Put the request to outgoing queue
  2156. select {
  2157. case c.chW <- w:
  2158. default:
  2159. // Try substituting the oldest w with the current one.
  2160. select {
  2161. case wOld := <-c.chW:
  2162. wOld.err = ErrPipelineOverflow
  2163. wOld.done <- struct{}{}
  2164. default:
  2165. }
  2166. select {
  2167. case c.chW <- w:
  2168. default:
  2169. c.releasePipelineWork(w)
  2170. return ErrPipelineOverflow
  2171. }
  2172. }
  2173. // Wait for the response
  2174. <-w.done
  2175. err := w.err
  2176. c.releasePipelineWork(w)
  2177. return err
  2178. }
  2179. func (c *PipelineClient) getConnClient() *pipelineConnClient {
  2180. c.connClientsLock.Lock()
  2181. cc := c.getConnClientUnlocked()
  2182. c.connClientsLock.Unlock()
  2183. return cc
  2184. }
  2185. func (c *PipelineClient) getConnClientUnlocked() *pipelineConnClient {
  2186. if len(c.connClients) == 0 {
  2187. return c.newConnClient()
  2188. }
  2189. // Return the client with the minimum number of pending requests.
  2190. minCC := c.connClients[0]
  2191. minReqs := minCC.PendingRequests()
  2192. if minReqs == 0 {
  2193. return minCC
  2194. }
  2195. for i := 1; i < len(c.connClients); i++ {
  2196. cc := c.connClients[i]
  2197. reqs := cc.PendingRequests()
  2198. if reqs == 0 {
  2199. return cc
  2200. }
  2201. if reqs < minReqs {
  2202. minCC = cc
  2203. minReqs = reqs
  2204. }
  2205. }
  2206. maxConns := c.MaxConns
  2207. if maxConns <= 0 {
  2208. maxConns = 1
  2209. }
  2210. if len(c.connClients) < maxConns {
  2211. return c.newConnClient()
  2212. }
  2213. return minCC
  2214. }
  2215. func (c *PipelineClient) newConnClient() *pipelineConnClient {
  2216. cc := &pipelineConnClient{
  2217. Addr: c.Addr,
  2218. Name: c.Name,
  2219. NoDefaultUserAgentHeader: c.NoDefaultUserAgentHeader,
  2220. MaxPendingRequests: c.MaxPendingRequests,
  2221. MaxBatchDelay: c.MaxBatchDelay,
  2222. Dial: c.Dial,
  2223. DialDualStack: c.DialDualStack,
  2224. DisableHeaderNamesNormalizing: c.DisableHeaderNamesNormalizing,
  2225. DisablePathNormalizing: c.DisablePathNormalizing,
  2226. IsTLS: c.IsTLS,
  2227. TLSConfig: c.TLSConfig,
  2228. MaxIdleConnDuration: c.MaxIdleConnDuration,
  2229. ReadBufferSize: c.ReadBufferSize,
  2230. WriteBufferSize: c.WriteBufferSize,
  2231. ReadTimeout: c.ReadTimeout,
  2232. WriteTimeout: c.WriteTimeout,
  2233. Logger: c.Logger,
  2234. }
  2235. c.connClients = append(c.connClients, cc)
  2236. return cc
  2237. }
  2238. // ErrPipelineOverflow may be returned from PipelineClient.Do*
  2239. // if the requests' queue is overflown.
  2240. var ErrPipelineOverflow = errors.New("pipelined requests' queue has been overflown. Increase MaxConns and/or MaxPendingRequests")
  2241. // DefaultMaxPendingRequests is the default value
  2242. // for PipelineClient.MaxPendingRequests.
  2243. const DefaultMaxPendingRequests = 1024
  2244. func (c *pipelineConnClient) init() {
  2245. c.chLock.Lock()
  2246. if c.chR == nil {
  2247. maxPendingRequests := c.MaxPendingRequests
  2248. if maxPendingRequests <= 0 {
  2249. maxPendingRequests = DefaultMaxPendingRequests
  2250. }
  2251. c.chR = make(chan *pipelineWork, maxPendingRequests)
  2252. if c.chW == nil {
  2253. c.chW = make(chan *pipelineWork, maxPendingRequests)
  2254. }
  2255. go func() {
  2256. // Keep restarting the worker if it fails (connection errors for example).
  2257. for {
  2258. if err := c.worker(); err != nil {
  2259. c.logger().Printf("error in PipelineClient(%q): %v", c.Addr, err)
  2260. if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
  2261. // Throttle client reconnections on timeout errors
  2262. time.Sleep(time.Second)
  2263. }
  2264. } else {
  2265. c.chLock.Lock()
  2266. stop := len(c.chR) == 0 && len(c.chW) == 0
  2267. if !stop {
  2268. c.chR = nil
  2269. c.chW = nil
  2270. }
  2271. c.chLock.Unlock()
  2272. if stop {
  2273. break
  2274. }
  2275. }
  2276. }
  2277. }()
  2278. }
  2279. c.chLock.Unlock()
  2280. }
  2281. func (c *pipelineConnClient) worker() error {
  2282. tlsConfig := c.cachedTLSConfig()
  2283. conn, err := dialAddr(c.Addr, c.Dial, c.DialDualStack, c.IsTLS, tlsConfig, c.WriteTimeout)
  2284. if err != nil {
  2285. return err
  2286. }
  2287. // Start reader and writer
  2288. stopW := make(chan struct{})
  2289. doneW := make(chan error)
  2290. go func() {
  2291. doneW <- c.writer(conn, stopW)
  2292. }()
  2293. stopR := make(chan struct{})
  2294. doneR := make(chan error)
  2295. go func() {
  2296. doneR <- c.reader(conn, stopR)
  2297. }()
  2298. // Wait until reader and writer are stopped
  2299. select {
  2300. case err = <-doneW:
  2301. conn.Close()
  2302. close(stopR)
  2303. <-doneR
  2304. case err = <-doneR:
  2305. conn.Close()
  2306. close(stopW)
  2307. <-doneW
  2308. }
  2309. // Notify pending readers
  2310. for len(c.chR) > 0 {
  2311. w := <-c.chR
  2312. w.err = errPipelineConnStopped
  2313. w.done <- struct{}{}
  2314. }
  2315. return err
  2316. }
  2317. func (c *pipelineConnClient) cachedTLSConfig() *tls.Config {
  2318. if !c.IsTLS {
  2319. return nil
  2320. }
  2321. c.tlsConfigLock.Lock()
  2322. cfg := c.tlsConfig
  2323. if cfg == nil {
  2324. cfg = newClientTLSConfig(c.TLSConfig, c.Addr)
  2325. c.tlsConfig = cfg
  2326. }
  2327. c.tlsConfigLock.Unlock()
  2328. return cfg
  2329. }
  2330. func (c *pipelineConnClient) writer(conn net.Conn, stopCh <-chan struct{}) error {
  2331. writeBufferSize := c.WriteBufferSize
  2332. if writeBufferSize <= 0 {
  2333. writeBufferSize = defaultWriteBufferSize
  2334. }
  2335. bw := bufio.NewWriterSize(conn, writeBufferSize)
  2336. defer bw.Flush()
  2337. chR := c.chR
  2338. chW := c.chW
  2339. writeTimeout := c.WriteTimeout
  2340. maxIdleConnDuration := c.MaxIdleConnDuration
  2341. if maxIdleConnDuration <= 0 {
  2342. maxIdleConnDuration = DefaultMaxIdleConnDuration
  2343. }
  2344. maxBatchDelay := c.MaxBatchDelay
  2345. var (
  2346. stopTimer = time.NewTimer(time.Hour)
  2347. flushTimer = time.NewTimer(time.Hour)
  2348. flushTimerCh <-chan time.Time
  2349. instantTimerCh = make(chan time.Time)
  2350. w *pipelineWork
  2351. err error
  2352. )
  2353. close(instantTimerCh)
  2354. for {
  2355. againChW:
  2356. select {
  2357. case w = <-chW:
  2358. // Fast path: len(chW) > 0
  2359. default:
  2360. // Slow path
  2361. stopTimer.Reset(maxIdleConnDuration)
  2362. select {
  2363. case w = <-chW:
  2364. case <-stopTimer.C:
  2365. return nil
  2366. case <-stopCh:
  2367. return nil
  2368. case <-flushTimerCh:
  2369. if err = bw.Flush(); err != nil {
  2370. return err
  2371. }
  2372. flushTimerCh = nil
  2373. goto againChW
  2374. }
  2375. }
  2376. if !w.deadline.IsZero() && time.Since(w.deadline) >= 0 {
  2377. w.err = ErrTimeout
  2378. w.done <- struct{}{}
  2379. continue
  2380. }
  2381. w.resp.parseNetConn(conn)
  2382. if writeTimeout > 0 {
  2383. // Set Deadline every time, since golang has fixed the performance issue
  2384. // See https://github.com/golang/go/issues/15133#issuecomment-271571395 for details
  2385. currentTime := time.Now()
  2386. if err = conn.SetWriteDeadline(currentTime.Add(writeTimeout)); err != nil {
  2387. w.err = err
  2388. w.done <- struct{}{}
  2389. return err
  2390. }
  2391. }
  2392. if err = w.req.Write(bw); err != nil {
  2393. w.err = err
  2394. w.done <- struct{}{}
  2395. return err
  2396. }
  2397. if flushTimerCh == nil && (len(chW) == 0 || len(chR) == cap(chR)) {
  2398. if maxBatchDelay > 0 {
  2399. flushTimer.Reset(maxBatchDelay)
  2400. flushTimerCh = flushTimer.C
  2401. } else {
  2402. flushTimerCh = instantTimerCh
  2403. }
  2404. }
  2405. againChR:
  2406. select {
  2407. case chR <- w:
  2408. // Fast path: len(chR) < cap(chR)
  2409. default:
  2410. // Slow path
  2411. select {
  2412. case chR <- w:
  2413. case <-stopCh:
  2414. w.err = errPipelineConnStopped
  2415. w.done <- struct{}{}
  2416. return nil
  2417. case <-flushTimerCh:
  2418. if err = bw.Flush(); err != nil {
  2419. w.err = err
  2420. w.done <- struct{}{}
  2421. return err
  2422. }
  2423. flushTimerCh = nil
  2424. goto againChR
  2425. }
  2426. }
  2427. }
  2428. }
  2429. func (c *pipelineConnClient) reader(conn net.Conn, stopCh <-chan struct{}) error {
  2430. readBufferSize := c.ReadBufferSize
  2431. if readBufferSize <= 0 {
  2432. readBufferSize = defaultReadBufferSize
  2433. }
  2434. br := bufio.NewReaderSize(conn, readBufferSize)
  2435. chR := c.chR
  2436. readTimeout := c.ReadTimeout
  2437. var (
  2438. w *pipelineWork
  2439. err error
  2440. )
  2441. for {
  2442. select {
  2443. case w = <-chR:
  2444. // Fast path: len(chR) > 0
  2445. default:
  2446. // Slow path
  2447. select {
  2448. case w = <-chR:
  2449. case <-stopCh:
  2450. return nil
  2451. }
  2452. }
  2453. if readTimeout > 0 {
  2454. // Set Deadline every time, since golang has fixed the performance issue
  2455. // See https://github.com/golang/go/issues/15133#issuecomment-271571395 for details
  2456. currentTime := time.Now()
  2457. if err = conn.SetReadDeadline(currentTime.Add(readTimeout)); err != nil {
  2458. w.err = err
  2459. w.done <- struct{}{}
  2460. return err
  2461. }
  2462. }
  2463. if err = w.resp.Read(br); err != nil {
  2464. w.err = err
  2465. w.done <- struct{}{}
  2466. return err
  2467. }
  2468. w.done <- struct{}{}
  2469. }
  2470. }
  2471. func (c *pipelineConnClient) logger() Logger {
  2472. if c.Logger != nil {
  2473. return c.Logger
  2474. }
  2475. return defaultLogger
  2476. }
  2477. // PendingRequests returns the current number of pending requests pipelined
  2478. // to the server.
  2479. //
  2480. // This number may exceed MaxPendingRequests*MaxConns by up to two times, since
  2481. // each connection to the server may keep up to MaxPendingRequests requests
  2482. // in the queue before sending them to the server.
  2483. //
  2484. // This function may be used for balancing load among multiple PipelineClient
  2485. // instances.
  2486. func (c *PipelineClient) PendingRequests() int {
  2487. c.connClientsLock.Lock()
  2488. n := 0
  2489. for _, cc := range c.connClients {
  2490. n += cc.PendingRequests()
  2491. }
  2492. c.connClientsLock.Unlock()
  2493. return n
  2494. }
  2495. func (c *pipelineConnClient) PendingRequests() int {
  2496. c.init()
  2497. c.chLock.Lock()
  2498. n := len(c.chR) + len(c.chW)
  2499. c.chLock.Unlock()
  2500. return n
  2501. }
  2502. var errPipelineConnStopped = errors.New("pipeline connection has been stopped")
  2503. var DefaultTransport RoundTripper = &transport{}
  2504. type transport struct{}
  2505. func (t *transport) RoundTrip(hc *HostClient, req *Request, resp *Response) (retry bool, err error) {
  2506. customSkipBody := resp.SkipBody
  2507. customStreamBody := resp.StreamBody
  2508. var deadline time.Time
  2509. if req.timeout > 0 {
  2510. deadline = time.Now().Add(req.timeout)
  2511. }
  2512. cc, err := hc.acquireConn(req.timeout, req.ConnectionClose())
  2513. if err != nil {
  2514. return false, err
  2515. }
  2516. conn := cc.c
  2517. resp.parseNetConn(conn)
  2518. writeDeadline := deadline
  2519. if hc.WriteTimeout > 0 {
  2520. tmpWriteDeadline := time.Now().Add(hc.WriteTimeout)
  2521. if writeDeadline.IsZero() || tmpWriteDeadline.Before(writeDeadline) {
  2522. writeDeadline = tmpWriteDeadline
  2523. }
  2524. }
  2525. if err = conn.SetWriteDeadline(writeDeadline); err != nil {
  2526. hc.closeConn(cc)
  2527. return true, err
  2528. }
  2529. resetConnection := false
  2530. if hc.MaxConnDuration > 0 && time.Since(cc.createdTime) > hc.MaxConnDuration && !req.ConnectionClose() {
  2531. req.SetConnectionClose()
  2532. resetConnection = true
  2533. }
  2534. bw := hc.acquireWriter(conn)
  2535. err = req.Write(bw)
  2536. if resetConnection {
  2537. req.Header.ResetConnectionClose()
  2538. }
  2539. if err == nil {
  2540. err = bw.Flush()
  2541. }
  2542. hc.releaseWriter(bw)
  2543. // Return ErrTimeout on any timeout.
  2544. if x, ok := err.(interface{ Timeout() bool }); ok && x.Timeout() {
  2545. err = ErrTimeout
  2546. }
  2547. isConnRST := isConnectionReset(err)
  2548. if err != nil && !isConnRST {
  2549. hc.closeConn(cc)
  2550. return true, err
  2551. }
  2552. readDeadline := deadline
  2553. if hc.ReadTimeout > 0 {
  2554. tmpReadDeadline := time.Now().Add(hc.ReadTimeout)
  2555. if readDeadline.IsZero() || tmpReadDeadline.Before(readDeadline) {
  2556. readDeadline = tmpReadDeadline
  2557. }
  2558. }
  2559. if err = conn.SetReadDeadline(readDeadline); err != nil {
  2560. hc.closeConn(cc)
  2561. return true, err
  2562. }
  2563. if customSkipBody || req.Header.IsHead() {
  2564. resp.SkipBody = true
  2565. }
  2566. if hc.DisableHeaderNamesNormalizing {
  2567. resp.Header.DisableNormalizing()
  2568. }
  2569. br := hc.acquireReader(conn)
  2570. err = resp.ReadLimitBody(br, hc.MaxResponseBodySize)
  2571. if err != nil {
  2572. hc.releaseReader(br)
  2573. hc.closeConn(cc)
  2574. // Don't retry in case of ErrBodyTooLarge since we will just get the same again.
  2575. needRetry := err != ErrBodyTooLarge
  2576. return needRetry, err
  2577. }
  2578. closeConn := resetConnection || req.ConnectionClose() || resp.ConnectionClose() || isConnRST
  2579. if customStreamBody && resp.bodyStream != nil {
  2580. rbs := resp.bodyStream
  2581. resp.bodyStream = newCloseReader(rbs, func() error {
  2582. hc.releaseReader(br)
  2583. if r, ok := rbs.(*requestStream); ok {
  2584. releaseRequestStream(r)
  2585. }
  2586. if closeConn || resp.ConnectionClose() {
  2587. hc.closeConn(cc)
  2588. } else {
  2589. hc.releaseConn(cc)
  2590. }
  2591. return nil
  2592. })
  2593. return false, nil
  2594. } else {
  2595. hc.releaseReader(br)
  2596. }
  2597. if closeConn {
  2598. hc.closeConn(cc)
  2599. } else {
  2600. hc.releaseConn(cc)
  2601. }
  2602. return false, nil
  2603. }