Skip to content

FEAT: Retry Mechanism #1073

Open
Open
@Aniket-lodh

Description

@Aniket-lodh

Retry Mechanism for Failed Proxy Requests

We need a retry mechanism to handle temporary service unavailability and connection failures during proxy requests, something like 'ECONNREFUSED'

Current Behavior

  • Single attempt to connect to microservices
  • Returns 503 on ECONNREFUSED
  • No retry logic for failed requests

Proposed Changes

  1. Add retry configuration:

    • Maximum 1-3 retry attempts (configurable)
    • manual delay b/w each failed retry (1s, 2s, 4s) (configurable)
    • 10s timeout per request (configurable)
    • A handler to decide on what basis the retry mechanism will work.
  2. Handle specific error cases:

    • ECONNREFUSED
    • ECONNRESET
    • ETIMEDOUT
  3. Add response statuses:

    • 503 Service Unavailable (temporary)
    • 504 Gateway Timeout
    • 500 Internal Server Error (other cases)

Code Example

const service_proxy_middleware = (target_url) => {
  const proxy = createProxyMiddleware({
    target: "http://localhost:3002/api/v0/endpoint",
    changeOrigin: true,
    logger: console,
    selfHandleResponse: true,
    retry: {
      attempts: 3,
      delay: 1000,
      factor: 2,
      retryCondition: (err) => {
        return (
          err.code === "ECONNREFUSED" ||
          err.code === "ECONNRESET" ||
          err.code === "ETIMEDOUT"
        );
      },
    },
    on: {
      proxyReq: fixRequestBody,
      proxyRes: responseInterceptor(save_to_cache),
      error: (err, req, res) => {
        if (err.code === "ECONNREFUSED") {
          res.status(503).json({
            status: "error",
            message:
              "Service temporarily unavailable / is restarting, please try again",
            code: "SERVICE_UNAVAILABLE",
          });
        } else {
          res.status(500).json({
            status: "error",
            message: "Internal server error",
            code: "PROXY_ERROR",
          });
        }
      },
    },
  });
  return proxy;
};

Related issue: #176

Additional context (optional)

"http-proxy-middleware": "^3.0.3",
"node": ">=22.0.0",
"express": "^4.21.2"

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions