-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move EndpointRoutingMode to EndpointRouterDecorator
- Loading branch information
Showing
6 changed files
with
97 additions
and
61 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
src/Microsoft.Azure.SignalR/EndpointRouters/LeastConnectionEndpointRouter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.Azure.SignalR | ||
{ | ||
internal class LeastConnectionEndpointRouter : DefaultEndpointRouter | ||
{ | ||
/// <summary> | ||
/// Select an endpoint for negotiate request | ||
/// </summary> | ||
/// <param name="context">The http context of the incoming request</param> | ||
/// <param name="endpoints">All the available endpoints</param> | ||
public override ServiceEndpoint GetNegotiateEndpoint(HttpContext context, IEnumerable<ServiceEndpoint> endpoints) | ||
{ | ||
// get primary endpoints snapshot | ||
var availableEndpoints = GetNegotiateEndpoints(endpoints); | ||
return GetEndpointWithLeastConnection(availableEndpoints); | ||
} | ||
|
||
/// <summary> | ||
/// Choose endpoint with least connection count | ||
/// </summary> | ||
private ServiceEndpoint GetEndpointWithLeastConnection(ServiceEndpoint[] availableEndpoints) | ||
{ | ||
//first check if weight is available or necessary | ||
if (availableEndpoints.Any(endpoint => endpoint.EndpointMetrics.ConnectionCapacity == 0) || | ||
availableEndpoints.Length == 1) | ||
{ | ||
return availableEndpoints[StaticRandom.Next(availableEndpoints.Length)]; | ||
} | ||
|
||
var leastConnectionCount = int.MaxValue; | ||
var index = 0; | ||
for (var i = 0; i < availableEndpoints.Length; i++) | ||
{ | ||
var endpointMetrics = availableEndpoints[i].EndpointMetrics; | ||
var connectionCount = endpointMetrics.ClientConnectionCount + endpointMetrics.ServerConnectionCount; | ||
if (connectionCount < leastConnectionCount) | ||
{ | ||
leastConnectionCount = connectionCount; | ||
index = i; | ||
} | ||
} | ||
|
||
return availableEndpoints[index]; | ||
} | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
src/Microsoft.Azure.SignalR/EndpointRouters/RandomEndpointRouter.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System.Collections.Generic; | ||
using Microsoft.AspNetCore.Http; | ||
|
||
namespace Microsoft.Azure.SignalR | ||
{ | ||
internal class RandomEndpointRouter : DefaultEndpointRouter | ||
{ | ||
/// <summary> | ||
/// Select an endpoint for negotiate request according to the mode | ||
/// </summary> | ||
/// <param name="context">The http context of the incoming request</param> | ||
/// <param name="endpoints">All the available endpoints</param> | ||
public override ServiceEndpoint GetNegotiateEndpoint(HttpContext context, IEnumerable<ServiceEndpoint> endpoints) | ||
{ | ||
// get primary endpoints snapshot | ||
var availableEndpoints = GetNegotiateEndpoints(endpoints); | ||
return availableEndpoints[StaticRandom.Next(availableEndpoints.Length)]; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters