Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT: Retry Mechanism #1073

Open
Aniket-lodh opened this issue Feb 27, 2025 · 0 comments
Open

FEAT: Retry Mechanism #1073

Aniket-lodh opened this issue Feb 27, 2025 · 0 comments

Comments

@Aniket-lodh
Copy link

Aniket-lodh commented Feb 27, 2025

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"

@Aniket-lodh Aniket-lodh changed the title Feat: Retry Mechanism FEAT: Retry Mechanism Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant