-
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.
Merge pull request #37 from swisschain/LM-3196-api-trading-blocking
LM-3196 implemented API trading blocking
- Loading branch information
Showing
21 changed files
with
257 additions
and
45 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
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
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 |
---|---|---|
@@ -0,0 +1,8 @@ | ||
namespace HftApi.Common.Configuration | ||
{ | ||
public class RabbitMqConnection | ||
{ | ||
public string ConnectionString { get; set; } | ||
public string ExchangeName { get; set; } | ||
} | ||
} |
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,4 +1,4 @@ | ||
using Lykke.Service.Kyc.Client; | ||
using Lykke.Service.Kyc.Client; | ||
|
||
namespace HftApi.Common.Configuration | ||
{ | ||
|
4 changes: 2 additions & 2 deletions
4
src/HftApi/Extensions/UserExxtensions.cs → src/HftApi/Extensions/UserExtensions.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
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
102 changes: 102 additions & 0 deletions
102
src/HftApi/RabbitSubscribers/ClientSettingsUpdatesSubscriber.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,102 @@ | ||
using System; | ||
using System.Threading.Tasks; | ||
using Autofac; | ||
using Common.Log; | ||
using HftApi.RabbitSubscribers.Messages; | ||
using Lykke.Common.Log; | ||
using Lykke.HftApi.Domain.Services; | ||
using Lykke.RabbitMqBroker; | ||
using Lykke.RabbitMqBroker.Subscriber; | ||
using Lykke.Service.HftInternalService.Client; | ||
|
||
namespace HftApi.RabbitSubscribers | ||
{ | ||
public class ClientSettingsUpdatesSubscriber : IStartable, IDisposable | ||
{ | ||
private readonly RabbitMqSubscriber<ClientSettingsCashoutBlockUpdated> _subscriber; | ||
private readonly IHftInternalClient _hftInternalClient; | ||
private readonly ITokenService _tokenService; | ||
private readonly ILog _log; | ||
|
||
public ClientSettingsUpdatesSubscriber(ILogFactory logFactory, | ||
string connectionString, | ||
IHftInternalClient hftInternalClient, | ||
ITokenService tokenService) | ||
{ | ||
_hftInternalClient = hftInternalClient; | ||
_tokenService = tokenService; | ||
_log = logFactory.CreateLog(this); | ||
|
||
var subscriptionSettings = RabbitMqSubscriptionSettings | ||
.ForSubscriber( | ||
connectionString, | ||
"client-account.client-settings-updated", | ||
$"client-account.client-settings-updated.hft-api-v2-{Environment.MachineName}") | ||
.UseRoutingKey(nameof(ClientSettingsCashoutBlockUpdated)); | ||
|
||
subscriptionSettings.DeadLetterExchangeName = null; | ||
|
||
var strategy = new DefaultErrorHandlingStrategy(logFactory, subscriptionSettings); | ||
|
||
_subscriber = new RabbitMqSubscriber<ClientSettingsCashoutBlockUpdated>(logFactory, subscriptionSettings, strategy) | ||
.SetMessageDeserializer(new JsonMessageDeserializer<ClientSettingsCashoutBlockUpdated>()) | ||
.SetMessageReadStrategy(new MessageReadWithTemporaryQueueStrategy()) | ||
.Subscribe(HandleMessage); | ||
} | ||
|
||
public async Task HandleMessage(ClientSettingsCashoutBlockUpdated evt) | ||
{ | ||
// Race condition with KeyUpdatedEvent event handling is possible, but it is decided | ||
// that it's acceptable since these events are not very frequent | ||
|
||
_log.Info($"Got client trades blocking update. Trades are blocked: {evt.TradesBlocked}", context: new | ||
{ | ||
ClientId = evt.ClientId | ||
}); | ||
|
||
var enabledClientApiKeys = await _hftInternalClient.Keys.GetKeys(evt.ClientId); | ||
|
||
if (evt.TradesBlocked) | ||
{ | ||
foreach (var key in enabledClientApiKeys) | ||
{ | ||
_tokenService.Remove(key.ApiKey); | ||
|
||
var apiKeyStart = key.ApiKey.Substring(0, 4); | ||
|
||
_log.Info($"API key has been cached", context: new | ||
{ | ||
ClientId = evt.ClientId, | ||
ApiKeyStart = apiKeyStart | ||
}); | ||
} | ||
} | ||
else | ||
{ | ||
foreach (var key in enabledClientApiKeys) | ||
{ | ||
_tokenService.Add(key.ApiKey); | ||
|
||
var apiKeyStart = key.ApiKey.Substring(0, 4); | ||
|
||
_log.Info($"API key has been evicted from the cache", context: new | ||
{ | ||
ClientId = evt.ClientId, | ||
ApiKeyStart = apiKeyStart | ||
}); | ||
} | ||
} | ||
} | ||
|
||
public void Start() | ||
{ | ||
_subscriber.Start(); | ||
} | ||
|
||
public void Dispose() | ||
{ | ||
_subscriber?.Stop(); | ||
_subscriber?.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
10 changes: 10 additions & 0 deletions
10
src/HftApi/RabbitSubscribers/Messages/ClientSettingsCashoutBlockUpdated.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,10 @@ | ||
namespace HftApi.RabbitSubscribers.Messages | ||
{ | ||
// Owned by Lykke.Service.ClientAccount service | ||
public class ClientSettingsCashoutBlockUpdated | ||
{ | ||
public string ClientId { get; set; } | ||
public bool CashOutBlocked { get; set; } | ||
public bool TradesBlocked { get; set; } | ||
} | ||
} |
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,9 @@ | ||
using System.Threading.Tasks; | ||
|
||
namespace Lykke.HftApi.Domain.Services | ||
{ | ||
public interface IBlockedClientsService | ||
{ | ||
Task<bool> IsClientBlocked(string clientId); | ||
} | ||
} |
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,4 +1,4 @@ | ||
using System.Threading.Tasks; | ||
using System.Threading.Tasks; | ||
|
||
namespace Lykke.HftApi.Domain.Services | ||
{ | ||
|
Oops, something went wrong.