Reputation: 4142
I am making use of proxies via C# and was wondering if anyone knew a good way of managing them.
As you know, the status of proxies changes over time and so those proxies that go bad should be retried later or removed. What would be a good way to maintain this list of proxies so that the best proxies get used, while ones that fail don't get used as often or after some timeout etc...
The type of proxy I am referring to is just a WebProxy that can be applied to a HttpWebRequest.
It is also important to minimize the number of requests with bad proxies.
Upvotes: 0
Views: 3456
Reputation: 162
I think that a priority queue could be too much. Moreover, what priority or priorities are you going to use to classify the proxy list?
I think that a simpler design could be better in this case: Two "buckets" or tow FIFO lists, one where you push the latest valid proxies and the other list is where you push the not working proxies.
Then you pop proxy IP addresses from both of them to check again. You can decide which of that proxy lists check more often.
Websites with proxy IP lists like proxy-ip-list.com probably uses an approach like that.
Upvotes: 0
Reputation: 56391
If I understand you, you are talking about the HTTP proxy used to make connections to web servers, not, for example, WCF proxies or RPC proxies.
What's odd is that you're acting as if your program will use more than one. That's a very rare program indeed! Most of us just use the default built into the OS configuration. The idea that a proxy server will suddenly go down seems even rarer. Are you planning to randomly select a proxy for your requests? Are you just trying to find proxy servers?
Regardless, only way you can know if the proxy server doesn't work is to inspect the response.StatusCode. BadGateway
or GatewayTimeout
should do the trick.
So... managing which are good and which are bad. I'd stick to a simple dictionary keyed against the proxy's URI, with the WebProxy object as the value. Assuming I understand what you're trying to do, if you try a proxy URI and it's good, add it to the dictionary. If you find that an existing proxy is bad, delete it from the dictionary.
Upvotes: 1