jeudi 27 août 2020

How to implement retry pattern using c#?

I have a list of Proxy Servers. Using these Proxy Servers, I want to hit URL to get data programmatically. I want to apply retry pattern here. For example, If I cannot get data from Proxy Server 1, I would like to hit URL using Proxy Server 2 and so on. What should I write in ProxyHelper class GetData method as per good design practices.

Sample Code :

Interface IProxyServer {

    IResponse GetData(String URL);

}

public class ProxyServerA implements IProxyServer {

    public IResponse GetData(String URL)
    {
        //some code
    }

}

public class ProxyServerB implements IProxyServer {

    public IResponse GetData(String URL) {
        //some code
    }

}

public class ProxyHelper {

    public List<IProxyServer> _proxyservers = new List<IProxyServer>(); 
    
    public IResponse GetData(String URL)
    {
    
    // Following the best practices, what should be the code here which will implement retry mechanism
    // and call other available proxy server in case the request is failed.
    
    }
    
    public void Add(IProxyServer proxyserver)
    {
        _proxyservers.Add(proxyserver);
    }

}

void main() {

    ProxyHelper proxyHelper = new ProxyHelper();

    String URL = "www.google.com";
    
    proxyserver.Add(new ProxyServerA());
    
    proxyserver.Add(new ProxyServerB());

    var response = ProxyHelper.GetData(URL);

}

Aucun commentaire:

Enregistrer un commentaire