Skip to content

Commit

Permalink
merge hotfix
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm360 committed Sep 23, 2022
2 parents 68cdaaf + 21ac77c commit 6fd13f2
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/BeehiveManager.Domain/BeehiveManager.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Etherna.DomainEvents" Version="1.3.1" />
<PackageReference Include="Etherna.DomainEvents" Version="1.4.0" />
<PackageReference Include="MongODM.Core" Version="0.23.1" />
<PackageReference Include="Nethereum.Accounts" Version="4.7.0" />
<PackageReference Include="Nethereum.Accounts" Version="4.8.0" />
</ItemGroup>

</Project>
4 changes: 2 additions & 2 deletions src/BeehiveManager.Services/BeehiveManager.Services.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bee.Net" Version="0.2.0-alpha.120" />
<PackageReference Include="Etherna.DomainEvents.AspNetCore" Version="1.3.1" />
<PackageReference Include="Bee.Net" Version="0.2.0-alpha.127" />
<PackageReference Include="Etherna.DomainEvents.AspNetCore" Version="1.4.0" />
<PackageReference Include="MongODM.Hangfire" Version="0.23.1" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" />
</ItemGroup>
Expand Down
70 changes: 70 additions & 0 deletions src/BeehiveManager/Areas/Api/Controllers/LoadBalancerController.cs
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;
}
}
}
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);
}
}
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");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.3.1" />
<PackageReference Include="Moq" Version="4.18.2" />
<PackageReference Include="xunit" Version="2.4.2" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.5">
Expand Down

0 comments on commit 6fd13f2

Please sign in to comment.