mirror of
https://github.com/microsoft/regorus.git
synced 2026-08-05 02:16:11 +00:00
Policy evaluation at scale needs to be able to set memory limits so that a bad policy does not hog memory or to ensure that policy evaluation itself does not use too much memory which could cause other components to suffer. This PR introduces capability to set and enforce global memory limits. It also lays the groundwork for enabling per evaluation limits in future. Once a global memory limit is set, Regorus maintains per thread counters to track memory activity (allocation, deallocation) of a thread. These counters are periodically flushed to global memory counters. Per thread counters avoid the contention that updating global counters on each alloc/free would cause. Policy evaluation periodically checks these counters and raises errors if allocated memory has exceeded the configured limit. Currently memory limit capability is exposed only to FFI and C#. Also update mimalloc to v2.2.6 Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
25 lines
754 B
C#
25 lines
754 B
C#
// Copyright (c) Microsoft Corporation.
|
|
// Licensed under the MIT License.
|
|
|
|
using System;
|
|
|
|
#nullable enable
|
|
|
|
namespace Regorus.Internal
|
|
{
|
|
internal static class StatusExtensions
|
|
{
|
|
internal static Exception CreateException(this RegorusStatus status, string? message)
|
|
{
|
|
var details = string.IsNullOrWhiteSpace(message) ? "Regorus call failed." : message;
|
|
|
|
return status switch
|
|
{
|
|
RegorusStatus.Panic => new InvalidOperationException($"Regorus engine panicked: {details}"),
|
|
RegorusStatus.Poisoned => new InvalidOperationException($"Regorus engine is poisoned: {details}"),
|
|
_ => new InvalidOperationException(details),
|
|
};
|
|
}
|
|
}
|
|
}
|