Skip to content

Commit

Permalink
testing memory pressure
Browse files Browse the repository at this point in the history
  • Loading branch information
mizrael committed Jun 7, 2024
1 parent f3cc6ae commit e90f0da
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/EvenireDB.Server/DTO/ApiError.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
namespace EvenireDB.Server.DTO;

public record ApiError(int Code, string Message);
6 changes: 2 additions & 4 deletions src/EvenireDB.Server/DTO/EventDataDTO.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
namespace EvenireDB.Server.DTO
{
public record EventDataDTO(string Type, ReadOnlyMemory<byte> Data);
}
namespace EvenireDB.Server.DTO;
public record EventDataDTO(string Type, ReadOnlyMemory<byte> Data);
16 changes: 8 additions & 8 deletions src/EvenireDB.Server/Routes/StreamsRoutes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ private static async IAsyncEnumerable<EventDTO> GetEvents(
[FromQuery(Name = "pos")] uint startPosition = 0,
[FromQuery(Name = "dir")] Direction direction = Direction.Forward)
{
await foreach (var @event in reader.ReadAsync(streamId, direction: direction, startPosition: startPosition))
await foreach (var @event in reader.ReadAsync(streamId, direction: direction, startPosition: startPosition).ConfigureAwait(false))
yield return EventDTO.FromModel(@event);
}

Expand All @@ -36,25 +36,25 @@ private static async ValueTask<IResult> SaveEvents(
[FromBody] EventDataDTO[]? dtos)
{
if(dtos is null)
return Results.BadRequest();
return Results.BadRequest(new ApiError(ErrorCodes.BadRequest, "No events provided"));

EventData[] events;

try
{
events = mapper.ToModels(dtos);
}
catch
catch(Exception ex)
{
// TODO: build proper response
return Results.BadRequest();
return Results.BadRequest(new ApiError(ErrorCodes.BadRequest, ex.Message));
}

var result = await writer.AppendAsync(streamId, events, expectedVersion);
var result = await writer.AppendAsync(streamId, events, expectedVersion)
.ConfigureAwait(false);
return result switch
{
FailureResult { Code: ErrorCodes.DuplicateEvent } d => Results.Conflict(d.Message),
FailureResult { Code: ErrorCodes.VersionMismatch } d => Results.BadRequest(d.Message),
FailureResult { Code: ErrorCodes.DuplicateEvent } d => Results.Conflict(new ApiError(ErrorCodes.DuplicateEvent, d.Message)),
FailureResult { Code: ErrorCodes.VersionMismatch } d => Results.BadRequest(new ApiError(ErrorCodes.VersionMismatch, d.Message)),
FailureResult => Results.StatusCode(500),
_ => Results.AcceptedAtRoute(nameof(GetEvents), new { streamId })
};
Expand Down
6 changes: 5 additions & 1 deletion src/EvenireDB/Workers/MemoryWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,12 @@ protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
while (!stoppingToken.IsCancellationRequested)
{
using var process = Process.GetCurrentProcess();
var gcMemoryInfo = GC.GetGCMemoryInfo();
var totalMemory = GC.GetTotalMemory(false);

_logger.LogInformation("{TotalMemory}, {TotalCommittedBytes}", totalMemory, gcMemoryInfo.TotalCommittedBytes);

using var process = Process.GetCurrentProcess();
bool needDrop = process.PrivateMemorySize64 > _settings.MaxAllowedAllocatedBytes;
if (needDrop)
{
Expand Down

0 comments on commit e90f0da

Please sign in to comment.