mirror of
https://github.com/lukaszraczylo/graphql-monitoring-proxy.git
synced 2026-10-22 06:38:13 +00:00
Reuse http client and add retry to the proxied requests.
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/avast/retry-go/v4"
|
||||||
fiber "github.com/gofiber/fiber/v2"
|
fiber "github.com/gofiber/fiber/v2"
|
||||||
"github.com/gofiber/fiber/v2/middleware/proxy"
|
"github.com/gofiber/fiber/v2/middleware/proxy"
|
||||||
libpack_monitoring "github.com/lukaszraczylo/graphql-monitoring-proxy/monitoring"
|
libpack_monitoring "github.com/lukaszraczylo/graphql-monitoring-proxy/monitoring"
|
||||||
@@ -40,12 +41,31 @@ func proxyTheRequest(c *fiber.Ctx) error {
|
|||||||
c.Request().Header.Del(fiber.HeaderAcceptEncoding)
|
c.Request().Header.Del(fiber.HeaderAcceptEncoding)
|
||||||
|
|
||||||
cfg.Logger.Debug("Proxying the request", map[string]interface{}{"path": c.Path(), "body": string(c.Request().Body()), "headers": c.GetReqHeaders(), "request_uuid": c.Locals("request_uuid")})
|
cfg.Logger.Debug("Proxying the request", map[string]interface{}{"path": c.Path(), "body": string(c.Request().Body()), "headers": c.GetReqHeaders(), "request_uuid": c.Locals("request_uuid")})
|
||||||
|
|
||||||
|
err := retry.Do(
|
||||||
|
func() error {
|
||||||
err := proxy.DoRedirects(c, cfg.Server.HostGraphQL+c.Path(), 3, cfg.Client.FastProxyClient)
|
err := proxy.DoRedirects(c, cfg.Server.HostGraphQL+c.Path(), 3, cfg.Client.FastProxyClient)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
cfg.Logger.Error("Can't proxy the request", map[string]interface{}{"error": err.Error()})
|
cfg.Logger.Error("Can't proxy the request", map[string]interface{}{"error": err.Error()})
|
||||||
cfg.Monitoring.Increment(libpack_monitoring.MetricsFailed, nil)
|
cfg.Monitoring.Increment(libpack_monitoring.MetricsFailed, nil)
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
retry.OnRetry(func(n uint, err error) {
|
||||||
|
cfg.Logger.Warning("Retrying the request", map[string]interface{}{"path": c.Path(), "error": err.Error()})
|
||||||
|
}),
|
||||||
|
retry.Attempts(uint(3)),
|
||||||
|
retry.DelayType(retry.BackOffDelay),
|
||||||
|
retry.Delay(time.Duration(250*time.Millisecond)),
|
||||||
|
retry.LastErrorOnly(true),
|
||||||
|
)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
cfg.Logger.Warning("Can't proxy the request", map[string]interface{}{"error": err.Error()})
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
cfg.Logger.Debug("Received proxied response", map[string]interface{}{"path": c.Path(), "response_body": string(c.Response().Body()), "response_code": c.Response().StatusCode(), "headers": c.GetRespHeaders(), "request_uuid": c.Locals("request_uuid")})
|
cfg.Logger.Debug("Received proxied response", map[string]interface{}{"path": c.Path(), "response_body": string(c.Response().Body()), "response_code": c.Response().StatusCode(), "headers": c.GetRespHeaders(), "request_uuid": c.Locals("request_uuid")})
|
||||||
|
|
||||||
if c.Response().StatusCode() != 200 {
|
if c.Response().StatusCode() != 200 {
|
||||||
|
|||||||
Reference in New Issue
Block a user