-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
166 additions
and
5 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
70 changes: 70 additions & 0 deletions
70
src/BeehiveManager/Areas/Api/Controllers/LoadBalancerController.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,70 @@ | ||
// Copyright 2021-present Etherna Sagl | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Etherna.BeehiveManager.Areas.Api.DtoModels; | ||
using Etherna.BeehiveManager.Areas.Api.Services; | ||
using Etherna.BeehiveManager.Attributes; | ||
using Microsoft.AspNetCore.Http; | ||
using Microsoft.AspNetCore.Mvc; | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Globalization; | ||
using System.Threading.Tasks; | ||
|
||
namespace Etherna.BeehiveManager.Areas.Api.Controllers | ||
{ | ||
[ApiController] | ||
[ApiVersion("0.3")] | ||
[Route("api/v{api-version:apiVersion}/[controller]")] | ||
public class LoadBalancerController : ControllerBase | ||
{ | ||
// Fields. | ||
private readonly ILoadBalancerControllerService service; | ||
|
||
// Constructor. | ||
public LoadBalancerController(ILoadBalancerControllerService service) | ||
{ | ||
this.service = service; | ||
} | ||
|
||
// Get. | ||
|
||
/// <summary> | ||
/// Select best node for download a specific content | ||
/// </summary> | ||
/// <param name="hash">Reference hash of the content</param> | ||
/// <response code="200">Selected Bee node</response> | ||
[HttpGet("download/{hash}")] | ||
[SimpleExceptionFilter] | ||
[ProducesResponseType(StatusCodes.Status200OK)] | ||
[ProducesResponseType(StatusCodes.Status400BadRequest)] | ||
public async Task<BeeNodeDto> SelectDownloadNodeAsync( | ||
[Required] string hash) | ||
{ | ||
var beeNode = await service.SelectDownloadNodeAsync(hash); | ||
|
||
// Copy response in headers (Nginx optimization). | ||
HttpContext.Response.Headers.Add("bee-node-id", beeNode.Id); | ||
HttpContext.Response.Headers.Add("bee-node-debug-port", beeNode.DebugPort.ToString(CultureInfo.InvariantCulture)); | ||
HttpContext.Response.Headers.Add("bee-node-ethereum-address", beeNode.EthereumAddress); | ||
HttpContext.Response.Headers.Add("bee-node-gateway-port", beeNode.GatewayPort.ToString(CultureInfo.InvariantCulture)); | ||
HttpContext.Response.Headers.Add("bee-node-hostname", beeNode.Hostname.ToString(CultureInfo.InvariantCulture)); | ||
HttpContext.Response.Headers.Add("bee-node-scheme", beeNode.ConnectionScheme); | ||
HttpContext.Response.Headers.Add("bee-node-overlay-address", beeNode.OverlayAddress); | ||
HttpContext.Response.Headers.Add("bee-node-pss-public-key", beeNode.PssPublicKey); | ||
HttpContext.Response.Headers.Add("bee-node-public-key", beeNode.PublicKey); | ||
|
||
return beeNode; | ||
} | ||
} | ||
} |
24 changes: 24 additions & 0 deletions
24
src/BeehiveManager/Areas/Api/Services/ILoadBalancerControllerService.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,24 @@ | ||
// Copyright 2021-present Etherna Sagl | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Etherna.BeehiveManager.Areas.Api.DtoModels; | ||
using System.Threading.Tasks; | ||
|
||
namespace Etherna.BeehiveManager.Areas.Api.Services | ||
{ | ||
public interface ILoadBalancerControllerService | ||
{ | ||
Task<BeeNodeDto> SelectDownloadNodeAsync(string hash); | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
src/BeehiveManager/Areas/Api/Services/LoadBalancerControllerService.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,67 @@ | ||
// Copyright 2021-present Etherna Sagl | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
using Etherna.BeehiveManager.Areas.Api.DtoModels; | ||
using Etherna.BeehiveManager.Domain; | ||
using Etherna.BeehiveManager.Services.Utilities; | ||
using MoreLinq; | ||
using System; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
|
||
namespace Etherna.BeehiveManager.Areas.Api.Services | ||
{ | ||
public class LoadBalancerControllerService : ILoadBalancerControllerService | ||
{ | ||
// Fields. | ||
private readonly IBeeNodeLiveManager beeNodeLiveManager; | ||
private readonly IBeehiveDbContext dbContext; | ||
|
||
// Constructor. | ||
public LoadBalancerControllerService( | ||
IBeeNodeLiveManager beeNodeLiveManager, | ||
IBeehiveDbContext dbContext) | ||
{ | ||
this.beeNodeLiveManager = beeNodeLiveManager; | ||
this.dbContext = dbContext; | ||
} | ||
|
||
// Methods. | ||
public async Task<BeeNodeDto> SelectDownloadNodeAsync(string hash) | ||
{ | ||
// Try to find a pinning node. | ||
var beeNodeLiveInstances = beeNodeLiveManager.GetBeeNodeLiveInstancesByPinnedContent(hash, true); | ||
if (beeNodeLiveInstances.Any()) | ||
{ | ||
//select a random one | ||
var instance = beeNodeLiveInstances.RandomSubset(1).First(); | ||
var node = await dbContext.BeeNodes.FindOneAsync(instance.Id); | ||
return new BeeNodeDto(node); | ||
} | ||
|
||
// If there isn't any pinning node, select an alive node. | ||
beeNodeLiveInstances = beeNodeLiveManager.HealthyNodes; | ||
if (beeNodeLiveInstances.Any()) | ||
{ | ||
//select a random one | ||
var instance = beeNodeLiveInstances.RandomSubset(1).First(); | ||
var node = await dbContext.BeeNodes.FindOneAsync(instance.Id); | ||
return new BeeNodeDto(node); | ||
} | ||
|
||
// Throw exception because doesn't exist any available node. | ||
throw new InvalidOperationException("Can't select a valid node"); | ||
} | ||
} | ||
} |
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