Skip to content
代码片段 群组 项目
提交 5d55ea12 编辑于 作者: Jan Provaznik's avatar Jan Provaznik
浏览文件

Introduce default client for send-url injector

The http client has been always reused for send-url injector.

However, it changed recently when an ability to customize the
timeouts has been introduced. This MR caches http client and re-use it
for requests with same params.
上级 457909e1
No related branches found
No related tags found
无相关合并请求
......@@ -6,6 +6,8 @@ import (
"net/http"
"os"
"strings"
"sync"
"time"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
......@@ -33,6 +35,14 @@ type entryParams struct {
Method string
}
type cacheKey struct {
requestTimeout time.Duration
responseTimeout time.Duration
allowRedirects bool
}
var httpClients sync.Map
var SendURL = &entry{"send-url:"}
var rangeHeaderKeys = []string{
......@@ -129,9 +139,7 @@ func (e *entry) Inject(w http.ResponseWriter, r *http.Request, sendData string)
}
// execute new request
var resp *http.Response
resp, err = newClient(params).Do(newReq)
resp, err := cachedClient(params).Do(newReq)
if err != nil {
status := http.StatusInternalServerError
......@@ -174,7 +182,17 @@ func (e *entry) Inject(w http.ResponseWriter, r *http.Request, sendData string)
sendURLRequestsSucceeded.Inc()
}
func newClient(params entryParams) *http.Client {
func cachedClient(params entryParams) *http.Client {
key := cacheKey{
requestTimeout: params.DialTimeout.Duration,
responseTimeout: params.ResponseHeaderTimeout.Duration,
allowRedirects: params.AllowRedirects,
}
cachedClient, found := httpClients.Load(key)
if found {
return cachedClient.(*http.Client)
}
var options []transport.Option
if params.DialTimeout.Duration != 0 {
......@@ -187,11 +205,12 @@ func newClient(params entryParams) *http.Client {
client := &http.Client{
Transport: transport.NewRestrictedTransport(options...),
}
if !params.AllowRedirects {
client.CheckRedirect = httpClientNoRedirect
}
httpClients.Store(key, client)
return client
}
......
......@@ -292,3 +292,22 @@ func TestErrorWithCustomStatusCode(t *testing.T) {
require.Equal(t, http.StatusTeapot, response.Code)
}
func TestHttpClientReuse(t *testing.T) {
expectedKey := cacheKey{
requestTimeout: 0,
responseTimeout: 0,
allowRedirects: false,
}
httpClients.Delete(expectedKey)
response := testEntryServer(t, "/get/request", nil, false)
require.Equal(t, http.StatusOK, response.Code)
_, found := httpClients.Load(expectedKey)
require.Equal(t, true, found)
storedClient := &http.Client{}
httpClients.Store(expectedKey, storedClient)
require.Equal(t, cachedClient(entryParams{}), storedClient)
require.NotEqual(t, cachedClient(entryParams{AllowRedirects: true}), storedClient)
}
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册