Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using MktData.Api.Actors.Exchanges;
- using MktData.Api.Models;
- using Proto;
- namespace MktData.Api.Actors;
- // Roger:
- // I personally use both ways.
- // Usually I use FromAddress to find known remote services. e.g. infrastructure actors for cluster/remote.
- // And I would use dictionary approach for anything where I have many actors that I need to keep track of.
- // e.g. where the name is not known in advance.
- internal sealed class ManagerAnotherApproach : IActor
- {
- private readonly Dictionary<string, PID> _children = new();
- private readonly ILogger<ManagerAnotherApproach> _logger;
- private readonly IServiceProvider _serviceProvider;
- public ManagerAnotherApproach(ILogger<ManagerAnotherApproach> logger, IServiceProvider serviceProvider)
- {
- _logger = logger;
- _serviceProvider = serviceProvider;
- }
- public Task ReceiveAsync(IContext context)
- {
- switch (context.Message)
- {
- case Started:
- break;
- case SubscriberRequest { Exchange: var exchange, AccountId: var accountId }:
- CreateChild(context, exchange, accountId);
- break;
- case OneOffBackFill { Exchange: var exchange, AccountId: var accountId }:
- ForwardToChild(context, exchange, accountId);
- break;
- case Notification:
- SendToAllChildren(context);
- break;
- case Terminated:
- StopAll(context);
- break;
- }
- return Task.CompletedTask;
- }
- private void CreateChild(IContext context, string exchange, string accountId)
- {
- if (!_children.TryGetValue($"{exchange}:{accountId}", out _))
- {
- var childProps = Props.FromProducer(() =>
- ActivatorUtilities.CreateInstance<FtxSubscriptionActor>(_serviceProvider, accountId)
- );
- var child = context.Spawn(childProps);
- _children.Add($"{exchange}:{accountId}", child);
- }
- }
- private void ForwardToChild(IContext context, string exchange, string accountId)
- {
- if (!_children.TryGetValue($"{exchange}:{accountId}", out var pid))
- {
- _logger.LogInformation("The PID for account id {AccountId} was not found", accountId);
- return;
- }
- context.Forward(pid);
- }
- // Fan-out mechanism
- private void SendToAllChildren(IContext context)
- {
- foreach (var child in context.Children)
- {
- context.Forward(child);
- }
- }
- // Terminates parent and its children
- private void StopAll(IContext context)
- {
- context.Stop(context.Self);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement