mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-06-05 23:03:48 +00:00
Fixing the proxy timeout settings which were not passed to the client and and graphql server.
This commit is contained in:
@@ -17,14 +17,6 @@ import (
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
var (
|
||||
httpClient *fasthttp.Client
|
||||
)
|
||||
|
||||
func init() {
|
||||
httpClient = createFasthttpClient(30) // Assuming a default timeout of 30 seconds
|
||||
}
|
||||
|
||||
func createFasthttpClient(timeout int) *fasthttp.Client {
|
||||
return &fasthttp.Client{
|
||||
Name: "graphql_proxy",
|
||||
@@ -40,6 +32,7 @@ func createFasthttpClient(timeout int) *fasthttp.Client {
|
||||
DisableHeaderNamesNormalizing: true,
|
||||
}
|
||||
}
|
||||
|
||||
func proxyTheRequest(c *fiber.Ctx, currentEndpoint string) error {
|
||||
if !checkAllowedURLs(c) {
|
||||
cfg.Logger.Error(&libpack_logger.LogMessage{
|
||||
@@ -64,7 +57,7 @@ func proxyTheRequest(c *fiber.Ctx, currentEndpoint string) error {
|
||||
|
||||
err = retry.Do(
|
||||
func() error {
|
||||
return proxy.DoRedirects(c, proxyURL, 3, httpClient)
|
||||
return proxy.DoRedirects(c, proxyURL, 3, cfg.Client.FastProxyClient)
|
||||
},
|
||||
retry.OnRetry(func(n uint, err error) {
|
||||
cfg.Logger.Warning(&libpack_logger.LogMessage{
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"time"
|
||||
|
||||
"github.com/valyala/fasthttp"
|
||||
)
|
||||
|
||||
@@ -154,3 +158,80 @@ func (suite *Tests) Test_proxyTheRequestWithPayloads() {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *Tests) Test_proxyTheRequestWithTimeouts() {
|
||||
originalTimeout := cfg.Client.ClientTimeout
|
||||
defer func() {
|
||||
cfg.Client.ClientTimeout = originalTimeout
|
||||
cfg.Client.FastProxyClient = createFasthttpClient(cfg.Client.ClientTimeout)
|
||||
}()
|
||||
|
||||
// Create a mock server
|
||||
mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
sleepDuration, _ := time.ParseDuration(r.Header.Get("X-Sleep-Duration"))
|
||||
time.Sleep(sleepDuration)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
w.Write([]byte(`{"data":{"test":"response"}}`))
|
||||
}))
|
||||
defer mockServer.Close()
|
||||
|
||||
tests := []struct {
|
||||
name string
|
||||
clientTimeout int
|
||||
sleepDuration string
|
||||
body string
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "Short timeout, long wait for response",
|
||||
clientTimeout: 1,
|
||||
sleepDuration: "2s",
|
||||
body: `{"query":"query { test }"}`,
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "Short timeout, short wait for response",
|
||||
clientTimeout: 2,
|
||||
sleepDuration: "500ms",
|
||||
body: `{"query":"query { test }"}`,
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "Long timeout, short wait for response",
|
||||
clientTimeout: 10,
|
||||
sleepDuration: "1s",
|
||||
body: `{"query":"query { test }"}`,
|
||||
wantErr: false,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
suite.Run(tt.name, func() {
|
||||
cfg.Client.ClientTimeout = tt.clientTimeout
|
||||
cfg.Client.FastProxyClient = createFasthttpClient(cfg.Client.ClientTimeout)
|
||||
cfg.Server.HostGraphQL = mockServer.URL
|
||||
|
||||
req := &fasthttp.Request{}
|
||||
req.SetBody([]byte(tt.body))
|
||||
req.SetRequestURI("/v1/graphql")
|
||||
req.Header.SetMethod("POST")
|
||||
req.Header.Set("Content-Type", "application/json")
|
||||
req.Header.Set("X-Sleep-Duration", tt.sleepDuration)
|
||||
|
||||
ctx := suite.app.AcquireCtx(&fasthttp.RequestCtx{})
|
||||
ctx.Request().Header.SetMethod("POST")
|
||||
ctx.Request().SetBody(req.Body())
|
||||
ctx.Request().SetRequestURI(string(req.RequestURI())) // Convert []byte to string
|
||||
ctx.Request().Header.SetContentType("application/json")
|
||||
ctx.Request().Header.Set("X-Sleep-Duration", tt.sleepDuration)
|
||||
|
||||
err := proxyTheRequest(ctx, cfg.Server.HostGraphQL)
|
||||
|
||||
if tt.wantErr {
|
||||
assert.NotNil(err, "Expected an error for test: %s", tt.name)
|
||||
} else {
|
||||
assert.Nil(err, "Expected no error for test: %s", tt.name)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
@@ -37,9 +37,9 @@ func StartHTTPProxy() {
|
||||
serverConfig := fiber.Config{
|
||||
DisableStartupMessage: true,
|
||||
AppName: fmt.Sprintf("GraphQL Monitoring Proxy - %s v%s", libpack_config.PKG_NAME, libpack_config.PKG_VERSION),
|
||||
IdleTimeout: time.Duration(cfg.Client.ClientTimeout) * time.Second * 2,
|
||||
ReadTimeout: time.Duration(cfg.Client.ClientTimeout) * time.Second * 2,
|
||||
WriteTimeout: time.Duration(cfg.Client.ClientTimeout) * time.Second * 2,
|
||||
IdleTimeout: time.Duration(cfg.Client.ClientTimeout) * time.Second,
|
||||
ReadTimeout: time.Duration(cfg.Client.ClientTimeout) * time.Second,
|
||||
WriteTimeout: time.Duration(cfg.Client.ClientTimeout) * time.Second,
|
||||
JSONEncoder: json.Marshal,
|
||||
JSONDecoder: json.Unmarshal,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user