Advertisement
Hulkstance

Untitled

Oct 1st, 2022
41
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.74 KB | None | 0 0
  1. using MktData.Api.Actors.Exchanges;
  2. using MktData.Api.Models;
  3. using Proto;
  4.  
  5. namespace MktData.Api.Actors;
  6.  
  7. // Roger:
  8. // I personally use both ways.
  9. // Usually I use FromAddress to find known remote services. e.g. infrastructure actors for cluster/remote.
  10. // And I would use dictionary approach for anything where I have many actors that I need to keep track of.
  11. // e.g. where the name is not known in advance.
  12. internal sealed class ManagerAnotherApproach : IActor
  13. {
  14.     private readonly Dictionary<string, PID> _children = new();
  15.  
  16.     private readonly ILogger<ManagerAnotherApproach> _logger;
  17.     private readonly IServiceProvider _serviceProvider;
  18.  
  19.     public ManagerAnotherApproach(ILogger<ManagerAnotherApproach> logger, IServiceProvider serviceProvider)
  20.     {
  21.         _logger = logger;
  22.         _serviceProvider = serviceProvider;
  23.     }
  24.  
  25.     public Task ReceiveAsync(IContext context)
  26.     {
  27.         switch (context.Message)
  28.         {
  29.             case Started:
  30.                 break;
  31.  
  32.             case SubscriberRequest { Exchange: var exchange, AccountId: var accountId }:
  33.                 CreateChild(context, exchange, accountId);
  34.                 break;
  35.  
  36.             case OneOffBackFill { Exchange: var exchange, AccountId: var accountId }:
  37.                 ForwardToChild(context, exchange, accountId);
  38.                 break;
  39.  
  40.             case Notification:
  41.                 SendToAllChildren(context);
  42.                 break;
  43.  
  44.             case Terminated:
  45.                 StopAll(context);
  46.                 break;
  47.         }
  48.  
  49.         return Task.CompletedTask;
  50.     }
  51.  
  52.     private void CreateChild(IContext context, string exchange, string accountId)
  53.     {
  54.         if (!_children.TryGetValue($"{exchange}:{accountId}", out _))
  55.         {
  56.             var childProps = Props.FromProducer(() =>
  57.                 ActivatorUtilities.CreateInstance<FtxSubscriptionActor>(_serviceProvider, accountId)
  58.             );
  59.  
  60.             var child = context.Spawn(childProps);
  61.  
  62.             _children.Add($"{exchange}:{accountId}", child);
  63.         }
  64.     }
  65.  
  66.     private void ForwardToChild(IContext context, string exchange, string accountId)
  67.     {
  68.         if (!_children.TryGetValue($"{exchange}:{accountId}", out var pid))
  69.         {
  70.             _logger.LogInformation("The PID for account id {AccountId} was not found", accountId);
  71.             return;
  72.         }
  73.  
  74.         context.Forward(pid);
  75.     }
  76.  
  77.     // Fan-out mechanism
  78.     private void SendToAllChildren(IContext context)
  79.     {
  80.         foreach (var child in context.Children)
  81.         {
  82.             context.Forward(child);
  83.         }
  84.     }
  85.  
  86.     // Terminates parent and its children
  87.     private void StopAll(IContext context)
  88.     {
  89.         context.Stop(context.Self);
  90.     }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement