http.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package rdhttp
  2. import (
  3. "cockData/server/log"
  4. "fmt"
  5. "github.com/valyala/fasthttp"
  6. "strings"
  7. "sync"
  8. "time"
  9. )
  10. var client *fasthttp.Client
  11. func InitHttp() {
  12. readTimeout, _ := time.ParseDuration("300ms")
  13. writeTimeout, _ := time.ParseDuration("300ms")
  14. maxIdleConnDuration, _ := time.ParseDuration("8s")
  15. maxConnWaitTimeout, _ := time.ParseDuration("8s")
  16. client = &fasthttp.Client{
  17. ReadTimeout: readTimeout,
  18. WriteTimeout: writeTimeout,
  19. MaxIdleConnDuration: maxIdleConnDuration,
  20. MaxConnWaitTimeout: maxConnWaitTimeout,
  21. MaxIdemponentCallAttempts: 3,
  22. NoDefaultUserAgentHeader: true, // Don't send: User-Agent: fasthttp
  23. DisableHeaderNamesNormalizing: true, // If you set the case on your headers correctly you can enable this
  24. DisablePathNormalizing: true,
  25. }
  26. curProxy.UnSet = true
  27. lock = sync.RWMutex{} // 初始化读写锁
  28. log.Info("Http连接池初始化完毕...")
  29. }
  30. func SendGetRequest(url string, headers map[string]string) (*fasthttp.Response, error) {
  31. req := fasthttp.AcquireRequest()
  32. req.SetRequestURI(url)
  33. req.Header.SetMethod(fasthttp.MethodGet)
  34. for key, value := range headers {
  35. req.Header.Add(key, value)
  36. }
  37. resp := fasthttp.AcquireResponse()
  38. err := client.Do(req, resp)
  39. fasthttp.ReleaseRequest(req)
  40. if err == nil {
  41. return resp, nil
  42. } else {
  43. log.Warnf("发送Get请求失败,地址%s,错误%v", url, err)
  44. return resp, err
  45. }
  46. }
  47. func SendPostRequest(url string, headers map[string]string, body []byte) (*fasthttp.Response, error) {
  48. req := fasthttp.AcquireRequest()
  49. defer fasthttp.ReleaseRequest(req)
  50. req.SetRequestURI(url)
  51. req.Header.SetMethod(fasthttp.MethodPost)
  52. for key, value := range headers {
  53. req.Header.Add(key, value)
  54. }
  55. req.SetBody(body)
  56. resp := fasthttp.AcquireResponse()
  57. err := client.Do(req, resp)
  58. if err == nil {
  59. log.Debugf("请求地址 %s 成功", url)
  60. return resp, nil
  61. } else {
  62. log.Warnf("Post地址%s,错误%v", url, err)
  63. return resp, err
  64. }
  65. }
  66. func SendGetRequestAndFixHost(url string, host string, headers map[string]string) (*fasthttp.Response, error) {
  67. req := fasthttp.AcquireRequest()
  68. req.SetRequestURI(url)
  69. req.Header.SetMethod(fasthttp.MethodGet)
  70. for key, value := range headers {
  71. req.Header.Add(key, value)
  72. }
  73. resp := fasthttp.AcquireResponse()
  74. err := client.Do(req, resp)
  75. fasthttp.ReleaseRequest(req)
  76. if err == nil {
  77. return resp, nil
  78. } else {
  79. log.Warnf("发送Get请求失败,地址%s,错误%v", url, err)
  80. return resp, err
  81. }
  82. }
  83. func GetQueryUrl(url string, params map[string]string) string {
  84. if len(params) <= 0 {
  85. return url
  86. }
  87. paramsList := make([]string, 0)
  88. for key, value := range params {
  89. str := fmt.Sprintf("%s=%s", key, value)
  90. paramsList = append(paramsList, str)
  91. }
  92. paramsStr := strings.Join(paramsList, "&")
  93. return fmt.Sprintf("%s?%s", url, paramsStr)
  94. }