-
Notifications
You must be signed in to change notification settings - Fork 95
/
DefaultEndpointRouter.cs
49 lines (44 loc) · 1.91 KB
/
DefaultEndpointRouter.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// 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.Azure.SignalR.Common;
using Microsoft.Owin;
namespace Microsoft.Azure.SignalR.AspNet
{
internal class DefaultEndpointRouter : DefaultMessageRouter, IEndpointRouter
{
/// <summary>
/// Randomly select from the available endpoints
/// </summary>
/// <param name="owinContext">The incoming owin http context</param>
/// <param name="endpoints"></param>
/// <returns></returns>
public ServiceEndpoint GetNegotiateEndpoint(IOwinContext owinContext, IEnumerable<ServiceEndpoint> endpoints)
{
// get primary endpoints snapshot
var availableEndpoints = GetNegotiateEndpoints(endpoints);
return availableEndpoints[StaticRandom.Next(availableEndpoints.Length)];
}
/// <summary>
/// Only primary endpoints will be returned by client /negotiate
/// If no primary endpoint is available, promote one secondary endpoint
/// </summary>
/// <returns>The availbale endpoints</returns>
private ServiceEndpoint[] GetNegotiateEndpoints(IEnumerable<ServiceEndpoint> endpoints)
{
var primary = endpoints.Where(s => s.Online && s.EndpointType == EndpointType.Primary).ToArray();
if (primary.Length > 0)
{
return primary;
}
// All primary endpoints are offline, fallback to the first online secondary endpoint
var secondary = endpoints.Where(s => s.Online && s.EndpointType == EndpointType.Secondary).ToArray();
if (secondary.Length == 0)
{
throw new AzureSignalRNotConnectedException();
}
return secondary;
}
}
}