-
-
Save profesor79/5b48fb36963eeb8baa411866ed4301f1 to your computer and use it in GitHub Desktop.
Now we have an actor and a class with buffering, Still not perfect. but working as good as possible
This file contains hidden or 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
using System.Diagnostics; | |
using Akka.Actor; | |
using Akka.Cluster.Hosting; | |
using Akka.Event; | |
using Akka.Hosting; | |
using Akka.Routing; | |
using Akka.Remote.Hosting; | |
using ASimpleApiwithActor; | |
using Microsoft.EntityFrameworkCore; | |
using System.Collections.Concurrent; | |
var environment = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") ?? "Development"; | |
var builder = WebApplication.CreateBuilder(args); | |
builder.Configuration | |
.AddJsonFile("appsettings.json") | |
.AddJsonFile($"appsettings.{environment}.json", optional: true) | |
.AddEnvironmentVariables(); | |
builder.Logging.ClearProviders();//.AddConsole(); | |
builder.Services.AddControllers(); | |
var app = builder.Build(); | |
app.UseRouting(); | |
var system = ActorSystem.Create("example"); | |
//.WithRouter(new RoundRobinPool(2)) | |
var bookActor = system.ActorOf(Props.Create(() => new BookActor()), "book"); | |
var queue = new ConcurrentQueue<Book>(); | |
app.UseEndpoints(endpoints => | |
{ | |
endpoints.MapControllers(); | |
endpoints.MapPost("/actor", async (HttpContext context, BookData bd) => | |
{ | |
bookActor.Tell(bd); | |
await context.Response.WriteAsync("ok"); | |
}); | |
endpoints.MapPost("/class", async (HttpContext context, BookData bd) => | |
{ | |
//this is normally injected as DI in the constructory | |
queue.Append(new Book | |
{ | |
AuthorName = bd.AuthorName, | |
Title = bd.Title | |
}); | |
if (queue.Count > 249) | |
{ | |
var booksList = new List<Book>(); | |
for (int i = 0; i < 250; i++) | |
{ | |
if (queue.TryDequeue(out var b)) | |
booksList.Add(b); | |
} | |
var bookWriterForClass = new BookWriter(); | |
bookWriterForClass.WriteBooks(booksList); | |
} | |
await context.Response.WriteAsync("OK"); | |
}); | |
}); | |
await app.RunAsync(); | |
public class BookWriter | |
{ | |
private FirstoneContext _ctx; | |
public ulong WriteBook(BookData bd) | |
{ | |
_ctx = new FirstoneContext(); | |
var book = new Book | |
{ | |
AuthorName = bd.AuthorName, | |
Title = bd.Title | |
}; | |
_ctx.Add(book); | |
_ctx.SaveChanges(); | |
return book.Id; | |
} | |
public void WriteBooks(List<Book> books) | |
{ | |
_ctx = new FirstoneContext(); | |
_ctx.AddRange(books); | |
_ctx.SaveChanges(); | |
} | |
} | |
public sealed class BookActor : ReceiveActor | |
{ | |
private readonly ILoggingAdapter _log = Context.GetLogger(); | |
private BookWriter _bookWriter = new BookWriter(); | |
private List<BookData> _booksList = new List<BookData>(); | |
private int _counter = 0; | |
public BookActor() | |
{ | |
Receive<BookData>(_ => | |
{ | |
_booksList.Add(_); | |
_counter++; | |
Become(Bufferring); | |
}); | |
} | |
private void Save() | |
{ | |
var books = _booksList.Select(b => new Book | |
{ | |
AuthorName = b.AuthorName, | |
Title = b.Title | |
}).ToList(); | |
_bookWriter.WriteBooks(books); | |
_booksList.Clear(); | |
_counter = 0; | |
} | |
public void Bufferring() | |
{ | |
Receive<BookData>(_ => | |
{ | |
_booksList.Add(_); | |
_counter++; | |
if (_counter > 249) | |
{ | |
Save(); | |
UnbecomeStacked(); | |
} | |
}); | |
} | |
} | |
public class BookData | |
{ | |
public long Id { get; set; } | |
public string Title { get; set; } = string.Empty; | |
public string AuthorName { get; set; } = string.Empty; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment