Http-proxy-middleware: Retries [Feature]

Created on 24 Jun 2017  路  6Comments  路  Source: chimurai/http-proxy-middleware

Expected behavior

Ability to have the proxy automatically retry on events like 'ECONNREFUSED'.

Proposed api

I imagine it would look something like this:

{
  target: 'localhost:9090',
  retry: {
    timeout: 1000,
    limit: 10
  }
}

Where the proxy would attempt the request again on failure every 1 second for at least 10 times.

enhancement need votes

Most helpful comment

For the case when the server is in a separate process, I created a connection to monitor the state of the server like this:

new WebpackDevServer({
    ...,
    setup: function (app) {
        var net = require('net');
        var opts = { port: 8082 };
        var socket = net.connect(opts);
        var connected = false;
        socket.on('connect', function () {
            console.log('[PROXY] connected');
            connected = true;
        }); 
        socket.on('error', function (err) {
            console.log('[PROXY] Error: ' + err.message);
        }); 
        socket.on('close', function (err) {
            console.log('[PROXY] closed');
            connected = false;
            setTimeout(function () {
                console.log('[PROXY] connecting...');
                socket.connect(opts);
            }, 1000);
        }); 
        app.use(function (req, res, next) {
            if (connected) {
                next();
            } else {
                socket.once('connect', function () {
                    next();
                }); 
            }   
        }); 
    }   
}

All 6 comments

Sounds like a feature for the core library http-proxy; which http-proxy-middleware uses.

Might consider implementing it, if there is need for it from the community.

@chimurai as an alternative for my specific use case it would also be work to be able to pause a proxied request. For example:

  target: 'localhost:9090',
  beforeRequest: (req, res, next) => {
    if (thing.isReady) return next()
    else thing.once('ready', next)
  })
}

Is this currently possible?

Mind you I am using webpack-dev-server so doing it manually in express is not available.


EDIT:

I was actually able to achieve this using the setup option on webpack dev server. 馃檭

@DylanPiercey, any chance you could provide an example? I am trying to achieve the same thing. Thanks.

@SystemParadox for my scenario I didn't actually need retries, just the ability to wait for a different server to start. Basically I did this specifically with webpack dev server:

new DevServer({
  ...,
  setup (app) {
    app.use((req, res, next) => {
      if (spawnedServer.listening) next()
      else spawnedServer.once('listening', next)
    })
  }
})

May not help for your use case.

Ah I see. Yes my server is a different process (spawned with node-dev) so I can't find out if it's listening so easily.

Thanks for the snippet though, might still come in handy.

For the case when the server is in a separate process, I created a connection to monitor the state of the server like this:

new WebpackDevServer({
    ...,
    setup: function (app) {
        var net = require('net');
        var opts = { port: 8082 };
        var socket = net.connect(opts);
        var connected = false;
        socket.on('connect', function () {
            console.log('[PROXY] connected');
            connected = true;
        }); 
        socket.on('error', function (err) {
            console.log('[PROXY] Error: ' + err.message);
        }); 
        socket.on('close', function (err) {
            console.log('[PROXY] closed');
            connected = false;
            setTimeout(function () {
                console.log('[PROXY] connecting...');
                socket.connect(opts);
            }, 1000);
        }); 
        app.use(function (req, res, next) {
            if (connected) {
                next();
            } else {
                socket.once('connect', function () {
                    next();
                }); 
            }   
        }); 
    }   
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

kimmobrunfeldt picture kimmobrunfeldt  路  5Comments

udayms picture udayms  路  8Comments

ghostd picture ghostd  路  7Comments

pwlerke picture pwlerke  路  6Comments

ubreddy picture ubreddy  路  5Comments