-
Notifications
You must be signed in to change notification settings - Fork 5
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
1 parent
f5a4687
commit f9b4056
Showing
9 changed files
with
311 additions
and
126 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
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 |
---|---|---|
@@ -1,46 +1,29 @@ | ||
using Xenopool.Server.Database.Repository; | ||
using Xenopool.Server.Database.Tables; | ||
using Xenopool.Server.Database.Tables; | ||
|
||
namespace Xenopool.Server.Pool; | ||
|
||
public sealed class PoolClient : IDisposable | ||
public sealed class PoolClient | ||
{ | ||
public sealed class Worker | ||
{ | ||
private readonly PoolAccount _poolAccount; | ||
private readonly string _workerId; | ||
private readonly PoolAccount _poolAccount; | ||
private readonly string _workerId; | ||
|
||
private DateTime _lastRequestTime = DateTime.Now; | ||
|
||
public Worker(PoolAccount poolAccount, string workerId) | ||
{ | ||
_poolAccount = poolAccount; | ||
_workerId = workerId; | ||
} | ||
private DateTime _lastRequestTime = DateTime.Now; | ||
|
||
public bool IsExpire() | ||
{ | ||
return (DateTime.Now - _lastRequestTime).TotalMinutes > 1; | ||
} | ||
private ulong _sharePoints = 0; | ||
|
||
public void Ping() | ||
{ | ||
_lastRequestTime = DateTime.Now; | ||
} | ||
public PoolClient(PoolAccount poolAccount, string workerId) | ||
{ | ||
_poolAccount = poolAccount; | ||
_workerId = workerId; | ||
} | ||
|
||
public PoolAccount Account { get; } | ||
|
||
private readonly PoolAccountRepository _poolAccountRepository; | ||
|
||
public PoolClient(PoolAccountRepository poolAccountRepository, string walletAddress) | ||
public bool IsExpire() | ||
{ | ||
_poolAccountRepository = poolAccountRepository; | ||
Account = poolAccountRepository.GetAccount(walletAddress) ?? poolAccountRepository.CreateAccount(walletAddress); | ||
return (DateTime.Now - _lastRequestTime).TotalMinutes > 1; | ||
} | ||
|
||
public void Dispose() | ||
public void Ping() | ||
{ | ||
_poolAccountRepository.Dispose(); | ||
_lastRequestTime = DateTime.Now; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,99 @@ | ||
using Grpc.Core; | ||
using Microsoft.EntityFrameworkCore; | ||
using Xenolib.Algorithms.Xenophyte.Centralized.Networking.Pool; | ||
using Xenolib.Utilities.Buffer; | ||
using Xenopool.Server.Database; | ||
using Xenopool.Server.Database.Repository; | ||
|
||
namespace Xenopool.Server.Pool; | ||
|
||
public sealed class PoolClientManager : IDisposable | ||
{ | ||
private readonly PoolAccountRepository _poolAccountRepository; | ||
private readonly Timer _poolClientValidityCheck; | ||
|
||
private readonly SemaphoreSlim _semaphoreSlim = new(1, 1); | ||
private readonly Dictionary<string, PoolClient> _poolClients = new(); | ||
|
||
public PoolClientManager(IDbContextFactory<SqliteDatabaseContext> contextFactory) | ||
{ | ||
_poolAccountRepository = new PoolAccountRepository(contextFactory.CreateDbContext()); | ||
_poolClientValidityCheck = new Timer(CheckClientValidity, null, TimeSpan.FromMinutes(1), TimeSpan.FromMinutes(1)); | ||
} | ||
|
||
public async Task<LoginResponse> AddClientAsync(LoginRequest request, ServerCallContext context) | ||
{ | ||
try | ||
{ | ||
await _semaphoreSlim.WaitAsync(context.CancellationToken); | ||
|
||
var account = await _poolAccountRepository.GetAccountAsync(request.WalletAddress, context.CancellationToken) ?? _poolAccountRepository.CreateAccount(request.WalletAddress); | ||
|
||
if (account.IsBanned) | ||
{ | ||
return new LoginResponse { Status = false, Reason = account.BanReason ?? string.Empty }; | ||
} | ||
|
||
string guid; | ||
|
||
do | ||
{ | ||
guid = Guid.NewGuid().ToString(); | ||
} while (!_poolClients.TryAdd(guid, new PoolClient(account, request.WorkerId))); | ||
|
||
return new LoginResponse { Status = true, Token = guid }; | ||
} | ||
finally | ||
{ | ||
if (_semaphoreSlim.CurrentCount == 0) _semaphoreSlim.Release(); | ||
} | ||
} | ||
|
||
public async Task<PoolClient?> GetClientAsync(string token, CancellationToken cancellationToken = default) | ||
{ | ||
try | ||
{ | ||
await _semaphoreSlim.WaitAsync(cancellationToken); | ||
return _poolClients.TryGetValue(token, out var poolClient) ? poolClient : null; | ||
} | ||
finally | ||
{ | ||
if (_semaphoreSlim.CurrentCount == 0) _semaphoreSlim.Release(); | ||
} | ||
} | ||
|
||
private void CheckClientValidity(object? _) | ||
{ | ||
try | ||
{ | ||
_semaphoreSlim.Wait(); | ||
|
||
using var expiredPoolClients = ArrayPoolOwner<string>.Rent(_poolClients.Count); | ||
var expiredPoolClientCount = 0; | ||
|
||
foreach (var poolClient in _poolClients) | ||
{ | ||
if (poolClient.Value.IsExpire()) | ||
{ | ||
expiredPoolClients.Span[expiredPoolClientCount++] = poolClient.Key; | ||
} | ||
} | ||
|
||
for (var i = 0; i < expiredPoolClientCount; i++) | ||
{ | ||
_poolClients.Remove(expiredPoolClients.Span[i]); | ||
} | ||
} | ||
finally | ||
{ | ||
if (_semaphoreSlim.CurrentCount == 0) _semaphoreSlim.Release(); | ||
} | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_poolAccountRepository.Dispose(); | ||
_poolClientValidityCheck.Dispose(); | ||
_semaphoreSlim.Dispose(); | ||
} | ||
} |
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
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
Oops, something went wrong.