Files
annotations/Azaion.CommonSecurity/ZeroMQExtensions.cs
T
Alex Bezdieniezhnykh c1b5b5fee2 use nms in the model itself, simplify and make postprocess faster.
make inference in batches, fix c# handling, add overlap handling
2025-02-10 14:55:00 +02:00

28 lines
929 B
C#

using MessagePack;
using NetMQ;
using NetMQ.Sockets;
namespace Azaion.CommonSecurity;
public static class ZeroMqExtensions
{
public static T? Get<T>(this DealerSocket dealer, Func<byte[], bool>? shouldInterceptFn = null, int retries = 24, int tryTimeoutSeconds = 5, CancellationToken ct = default) where T : class
{
var tryNum = 0;
while (!ct.IsCancellationRequested && tryNum++ < retries)
{
if (!dealer.TryReceiveFrameBytes(TimeSpan.FromSeconds(tryTimeoutSeconds), out var bytes))
continue;
if (shouldInterceptFn != null && shouldInterceptFn(bytes))
return null;
return MessagePackSerializer.Deserialize<T>(bytes);
}
if (!ct.IsCancellationRequested)
throw new Exception($"Unable to get {typeof(T).Name} after {tryNum} retries, {tryTimeoutSeconds} seconds each");
return null;
}
}