I'd use
squid to set up your proxy environment. It's tried and true and has lots of options which may help you now or later when you want additional/different behavior. Look at
retry_on_error and
cache_peer in squid's configuration to set up the behavior you describe.