private bool ProcessRequest(IHttpRequest aRequest, IHttpResponse aResponse, IHttpSession aSession) { session.HandleSession(aRequest, aResponse); IServerDispatcherStrategy serverDispatcher = ChooseStrategy(session); int index = serverDispatcher.GetNextServer(); if (index == -1) { return(false); } Logger.WriteLine("Forwarding {0} {1} to server {2}.", aRequest.Method, aRequest.UriPath, _servers[index]); HttpWebRequest request = PrepareServerRequest(aRequest, index); try { HttpWebResponse response = (HttpWebResponse)request.GetResponse(); PrepareClientResponse(aResponse, response); SetResponseCookies(response, aResponse, index, aRequest.Uri.Host); HandleResponse(response, aResponse, aRequest, _servers[index]); } catch (WebException e) when(e.Status == WebExceptionStatus.Timeout) { _servers.RemoveAt(index); } catch (WebException exception) { if (exception.Response != null) { HttpWebResponse response = (HttpWebResponse)exception.Response; PrepareClientResponse(aResponse, response); SetResponseCookies(response, aResponse, index, aRequest.Uri.Host); HandleResponse(response, aResponse, aRequest, _servers[index]); } else { aResponse.Status = HttpStatusCode.InternalServerError; StreamWriter writer = new StreamWriter(aResponse.Body); writer.Write(exception.Message); writer.Flush(); } } catch (Exception ex) { Logger.WriteLine("Exception! - {0}: {1}", ex.GetType().Name, ex.Message); aResponse.Status = HttpStatusCode.InternalServerError; StreamWriter writer = new StreamWriter(aResponse.Body); writer.Write($"{ex.GetType().Name}: {ex.Message}"); writer.Flush(); } return(true); }
private IServerDispatcherStrategy ChooseStrategy(Session aSession) { IServerDispatcherStrategy roundrobin = RoundrobinStrategy.GetInstance(_servers); switch (this.Settings["Algorithm"]) { case "roundrobin": return(roundrobin); case "sameserver": return(new SameServerStrategy(roundrobin, aSession)); default: return(roundrobin); } }
public SameServerStrategy(IServerDispatcherStrategy newClientStrategy, Session aSession) { _newClientStrategy = newClientStrategy; _session = aSession; }