-
Notifications
You must be signed in to change notification settings - Fork 0
/
http.go
41 lines (35 loc) · 1.15 KB
/
http.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package http_client
import (
"github.com/bulutcan99/weekly-task-scheduler/internal/infrastructure/env"
"sync"
"time"
"github.com/valyala/fasthttp"
)
var MaxConnPerHost = &env.Env.MaxConnsPerHost
var MaxConnWaitTimeout = &env.Env.MaxConnWaitTimeout
var ReadTimeout = &env.Env.ReadTimeout
var MaxIdempotentCallAttempts = &env.Env.MaxIdempotentCallAttempts
var doOnce sync.Once
var NetClient fasthttp.Client
func Init() {
doOnce.Do(func() {
NetClient = fasthttp.Client{
MaxConnsPerHost: *MaxConnPerHost,
MaxConnWaitTimeout: time.Duration(*MaxConnWaitTimeout) * time.Second,
ReadTimeout: time.Duration(*ReadTimeout) * time.Second,
MaxIdemponentCallAttempts: *MaxIdempotentCallAttempts,
MaxIdleConnDuration: 5 * time.Second,
MaxConnDuration: 5 * time.Second,
}
})
}
func SendGetRequest(url string) (int, []byte, error) {
req := fasthttp.AcquireRequest()
req.SetRequestURI(url)
req.Header.SetMethod(fasthttp.MethodGet)
res := fasthttp.AcquireResponse()
err := NetClient.Do(req, res)
defer fasthttp.ReleaseRequest(req)
defer fasthttp.ReleaseResponse(res)
return res.StatusCode(), res.Body(), err
}