You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I'm building a grid trading bot for my personal portfolio that should be capable of handling millions of grid bots simultaneously. Each bot would subscribe to several market data streams through the websocket protocol.
To get started with Akka.NET, I set up the dotnet new akkawebapi template.
Here is what I did so far:
[ApiController][Route("[controller]")]publicclass BotController(IRequiredActor<BotActor> botActor): ControllerBase
{privatereadonly IActorRef _botActor = botActor.ActorRef;[HttpPost("start/{botId}")]publicasyncTask<IActionResult>Start(stringbotId){varresponse=await _botActor.Ask<BotOperationResponse>(new StartBotCommand(botId), TimeSpan.FromSeconds(5));return response.Success ? Ok(response): BadRequest(response.Message);}[HttpPost("stop/{botId}")]publicasyncTask<IActionResult>Stop(stringbotId){varresponse=await _botActor.Ask<BotOperationResponse>(new StopBotCommand(botId), TimeSpan.FromSeconds(5));return response.Success ? Ok(response): BadRequest(response.Message);}// TODO: How do I fetch a specific bot by ID? How do I list all bot instances?}// AkkaConfiguration.cspublicstatic AkkaConfigurationBuilder ConfigureBotActors(thisAkkaConfigurationBuilderbuilder,IServiceProviderserviceProvider){varsettings= serviceProvider.GetRequiredService<AkkaSettings>();varbotMessageExtractor= CreateBotMessageRouter();if(settings.UseClustering){return builder.WithShardRegion<BotActor>("bot",(system,registry,resolver)=>entityId => Props.Create(()=>new BotActor(entityId)),
botMessageExtractor, settings.ShardOptions);}else{return builder.WithActors((system,registry,resolver)=>{varbotActor= system.ActorOf(Props.Create<BotActor>(),"botActor"); registry.Register<BotActor>(botActor);});}}publicstatic HashCodeMessageExtractor CreateBotMessageRouter()=>
HashCodeMessageExtractor.Create(
maxNumberOfShards:30,
entityIdExtractor:msg =>{returnmsgswitch{ IWithBotId botId => botId.BotId, _ =>null};},
messageExtractor:msg => msg);// BotActor.cspublicclassBotActor:ReceivePersistentActor{privatereadonlyHashSet<IActorRef>_subscribers=[];privateBotState_botState;privatereadonlyILoggingAdapter_log= Context.GetLogger();publicBotActor(stringbotId){PersistenceId=$"Bot_{botId}";_botState=new BotState(botId,false);Recover<BotStarted>(@event =>{_botState= _botState with{IsRunning=true};});Recover<BotStopped>(@event =>{_botState= _botState with{IsRunning=false};});Command<StartBotCommand>(cmd =>{if(!_botState.IsRunning){var@event=new BotStarted(cmd.BotId); Persist(@event,e =>{_botState= _botState with{IsRunning=true}; _log.Info("Bot started: {0}", cmd.BotId); NotifySubscribers(@event); Sender.Tell(new BotOperationResponse(cmd.BotId,true,"Bot started successfully"));});}else{ Sender.Tell(new BotOperationResponse(cmd.BotId,false,"Bot is already running"));}});Command<StopBotCommand>(cmd =>{if(_botState.IsRunning){var@event=new BotStopped(cmd.BotId); Persist(@event,e =>{_botState= _botState with{IsRunning=false}; _log.Info("Bot stopped: {0}", cmd.BotId); NotifySubscribers(@event); Sender.Tell(new BotOperationResponse(cmd.BotId,true,"Bot stopped successfully"));});}else{ Sender.Tell(new BotOperationResponse(cmd.BotId,false,"Bot is not running"));}});}privatevoidNotifySubscribers(IBotEvent@event){foreach(var subscriber in _subscribers){
subscriber.Tell(@event);}}publicoverridestringPersistenceId{get;}}publicrecordBotState(stringBotId,boolIsRunning);publicrecordBotOperationResponse(stringBotId,boolSuccess,stringMessage);// BotCommands.cspublicinterface IBotCommand :IWithBotId;publicsealedrecordStartBotCommand(stringBotId):IBotCommand;publicsealedrecordStopBotCommand(stringBotId):IBotCommand;// BotEvents.cspublicinterface IBotEvent :IWithBotId;publicsealedrecordBotStarted(stringBotId):IBotEvent;publicsealedrecordBotStopped(stringBotId):IBotEvent;// BotQueries.cspublicinterface IBotQuery :IWithBotId;publicsealedrecordFetchBot(stringBotId):IBotQuery;// IWithBotId.cspublicinterfaceIWithBotId{stringBotId{get;}}
The question is how do I add:
an endpoint that fetches a specific bot by ID
an endpoint that fetches all bot instances and their states
I'm not sure how to design it properly. I know that when I call the API endpoint to start a bot, it does not necessarily spawn a new BotActor instance. These actors are basically created, managed, and interacted with as if they are local, regardless of their actual location in the cluster.
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
I'm building a grid trading bot for my personal portfolio that should be capable of handling millions of grid bots simultaneously. Each bot would subscribe to several market data streams through the websocket protocol.
To get started with Akka.NET, I set up the
dotnet new akkawebapi
template.Here is what I did so far:
The question is how do I add:
I'm not sure how to design it properly. I know that when I call the API endpoint to start a bot, it does not necessarily spawn a new
BotActor
instance. These actors are basically created, managed, and interacted with as if they are local, regardless of their actual location in the cluster.https://github.com/Warrolen/grid
Beta Was this translation helpful? Give feedback.
All reactions