Skip to content

Commit

Permalink
Improved error handling on server start/stop and updated RestClient t…
Browse files Browse the repository at this point in the history
…o handle timeouts
  • Loading branch information
Scott Offen committed Aug 24, 2016
1 parent 7ab3fee commit 391ecef
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 12 deletions.
3 changes: 2 additions & 1 deletion src/Grapevine/Client/RestClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,9 @@ public IRestResponse Execute(IRestRequest restRequest)
}
catch (WebException e)
{
if (e.Status == WebExceptionStatus.Timeout) throw;
var elapsed = stopwatch.ElapsedMilliseconds;
var httpresponse = (HttpWebResponse)e.Response;
var httpresponse = (HttpWebResponse) e.Response;
response = new RestResponse(httpresponse)
{
ElapsedTime = elapsed,
Expand Down
2 changes: 2 additions & 0 deletions src/Grapevine/Server/Exceptions/UnableToStartHostException.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace Grapevine.Server.Exceptions
/// </summary>
public class UnableToStartHostException : Exception
{
public UnableToStartHostException(string message) : base(message) { }

public UnableToStartHostException(string message, Exception inner) : base(message, inner) { }
}
}
34 changes: 23 additions & 11 deletions src/Grapevine/Server/RestServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public class RestServer : DynamicProperties, IRestServer
private string _protocol = "http";
private int _connections;
protected bool IsStopping;
protected bool IsStarting;
protected readonly HttpListener Listener;
protected readonly Thread Listening;
protected readonly ConcurrentQueue<HttpListenerContext> Queue;
Expand Down Expand Up @@ -160,19 +161,21 @@ public bool UseHttps

public void Start()
{
if (IsListening) return;
if (IsListening || IsStarting) return;
if (IsStopping) throw new UnableToStartHostException("Cannot start server until server has finished stopping");
IsStarting = true;

try
{
if (Router.RoutingTable.Count == 0) Router.RegisterAssembly();

OnBeforeStart?.Invoke();

IsStopping = false;
Listener.Prefixes.Add(ListenerPrefix);
Listener.Start();
Listening.Start();

Workers = new Thread[_connections * Environment.ProcessorCount];
Workers = new Thread[_connections*Environment.ProcessorCount];
for (var i = 0; i < Workers.Length; i++)
{
Workers[i] = new Thread(Worker);
Expand All @@ -183,20 +186,24 @@ public void Start()
}
catch (Exception e)
{
var cshe = new UnableToStartHostException($"An error occured when trying to start the {GetType().FullName}", e);
Logger.Error(cshe);
if (EnableThrowingExceptions) throw cshe;
throw new UnableToStartHostException($"An error occured when trying to start the {GetType().FullName}", e);
}
finally
{
IsStarting = false;
}
}

public void Stop()
{
if (!IsListening) return;
if (!IsListening || IsStopping) return;
if (IsStarting) throw new UnableToStartHostException("Cannot stop server until server has finished starting");
IsStopping = true;

try
{
OnBeforeStop?.Invoke();

IsStopping = true;
StopEvent.Set();
Listening.Join();
foreach (var worker in Workers) worker.Join();
Expand All @@ -206,9 +213,11 @@ public void Stop()
}
catch (Exception e)
{
var cshe = new UnableToStopHostException($"An error occured while trying to stop {GetType().FullName}", e);
Logger.Error(cshe);
if (EnableThrowingExceptions) throw cshe;
throw new UnableToStopHostException($"An error occured while trying to stop {GetType().FullName}", e);
}
finally
{
IsStopping = false;
}
}

Expand Down Expand Up @@ -298,14 +307,17 @@ private void Worker()
}
catch (RouteNotFoundException)
{
if (EnableThrowingExceptions) throw;
context.Response.SendResponse(HttpStatusCode.NotFound);
}
catch (NotImplementedException)
{
if (EnableThrowingExceptions) throw;
context.Response.SendResponse(HttpStatusCode.NotImplemented);
}
catch (Exception e)
{
if (EnableThrowingExceptions) throw;
Logger.Error(e);
context.Response.SendResponse(HttpStatusCode.InternalServerError, e);
}
Expand Down

0 comments on commit 391ecef

Please sign in to comment.