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]")]publicclassBotController(IRequiredActor<BotActor>botActor):ControllerBase{privatereadonlyIActorRef_botActor=botActor.ActorRef;[HttpPost("start/{botId}")]publicasyncTask<IActionResult>Start(stringbotId){varresponse=await_botActor.Ask<BotOperationResponse>(newStartBotCommand(botId),TimeSpan.FromSeconds(5));returnresponse.Success?Ok(response):BadRequest(response.Message);}[HttpPost("stop/{botId}")]publicasyncTask<IActionResult>Stop(stringbotId){varresponse=await_botActor.Ask<BotOperationResponse>(newStopBotCommand(botId),TimeSpan.FromSeconds(5));returnresponse.Success?Ok(response):BadRequest(response.Message);}// TODO: How do I fetch a specific bot by ID? How do I list all bot instances?}// AkkaConfiguration.cspublicstaticAkkaConfigurationBuilderConfigureBotActors(thisAkkaConfigurationBuilderbuilder,IServiceProviderserviceProvider){varsettings=serviceProvider.GetRequiredService<AkkaSettings>();varbotMessageExtractor=CreateBotMessageRouter();if(settings.UseClustering){returnbuilder.WithShardRegion<BotActor>("bot",(system,registry,resolver)=> entityId =>Props.Create(()=>newBotActor(entityId)),botMessageExtractor,settings.ShardOptions);}else{returnbuilder.WithActors((system,registry,resolver)=>{varbotActor=system.ActorOf(Props.Create<BotActor>(),"botActor");registry.Register<BotActor>(botActor);});}}publicstaticHashCodeMessageExtractorCreateBotMessageRouter()=>HashCodeMessageExtractor.Create(maxNumberOfShards:30,entityIdExtractor: msg =>{returnmsgswitch{IWithBotIdbotId=>botId.BotId,
_ =>null};},messageExtractor: msg =>msg);// BotActor.cspublicclassBotActor:ReceivePersistentActor{privatereadonlyHashSet<IActorRef>_subscribers=[];privateBotState_botState;privatereadonlyILoggingAdapter_log=Context.GetLogger();publicBotActor(stringbotId){PersistenceId=$"Bot_{botId}";_botState=newBotState(botId,false);Recover<BotStarted>(@event =>{_botState=_botStatewith{IsRunning=true};});Recover<BotStopped>(@event =>{_botState=_botStatewith{IsRunning=false};});Command<StartBotCommand>(cmd =>{if(!_botState.IsRunning){var@event=newBotStarted(cmd.BotId);Persist(@event, e =>{_botState=_botStatewith{IsRunning=true};_log.Info("Bot started: {0}",cmd.BotId);NotifySubscribers(@event);Sender.Tell(newBotOperationResponse(cmd.BotId,true,"Bot started successfully"));});}else{Sender.Tell(newBotOperationResponse(cmd.BotId,false,"Bot is already running"));}});Command<StopBotCommand>(cmd =>{if(_botState.IsRunning){var@event=newBotStopped(cmd.BotId);Persist(@event, e =>{_botState=_botStatewith{IsRunning=false};_log.Info("Bot stopped: {0}",cmd.BotId);NotifySubscribers(@event);Sender.Tell(newBotOperationResponse(cmd.BotId,true,"Bot stopped successfully"));});}else{Sender.Tell(newBotOperationResponse(cmd.BotId,false,"Bot is not running"));}});}privatevoidNotifySubscribers(IBotEvent@event){foreach(varsubscriberin_subscribers){subscriber.Tell(@event);}}publicoverridestringPersistenceId{get;}}publicrecordBotState(stringBotId,boolIsRunning);publicrecordBotOperationResponse(stringBotId,boolSuccess,stringMessage);// BotCommands.cspublicinterfaceIBotCommand:IWithBotId;publicsealedrecordStartBotCommand(stringBotId):IBotCommand;publicsealedrecordStopBotCommand(stringBotId):IBotCommand;// BotEvents.cspublicinterfaceIBotEvent:IWithBotId;publicsealedrecordBotStarted(stringBotId):IBotEvent;publicsealedrecordBotStopped(stringBotId):IBotEvent;// BotQueries.cspublicinterfaceIBotQuery: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