Thanks Carl.
I am using C# and at this time I developed a simple client/server pair where I use:
public override async Task SubscribeState(SubscribeStateRequest request, IServerStreamWriter<SubscribeStateReply> responseStream, ServerCallContext context)
{
_subscribers[context.Peer] = responseStream;
while (_subscribers.ContainsKey(context.Peer)) await Task.Delay(TimeSpan.FromSeconds(5));
}
Also, in order to "fire" events managing potentially disconnected clients (i.e. subscribers) I didn't find anything better than:
foreach (var sub in _subscribers.Where(s=>s.Key!=context.Peer))
{
await _semaphore.WaitAsync().ConfigureAwait(false);
if (!_subscribersExceptionCount.ContainsKey(sub.Key)) _subscribersExceptionCount[sub.Key] = 0;
try
{
await sub.Value.WriteAsync(new SubscribeStateReply() {UpdatedState = request.State});
_subscribersExceptionCount[sub.Key]=0;
}
catch (Exception ex)
{
_subscribersExceptionCount[sub.Key]++;
if (_subscribersExceptionCount[sub.Key] >= 10)
{
IServerStreamWriter<SubscribeStateReply> removed;
if (_subscribers.TryRemove(sub.Key, out removed))
{
int currentCount;
_subscribersExceptionCount.TryRemove(sub.Key, out currentCount);
}
}
Console.WriteLine(ex);
}
finally
{
_semaphore.Release();
}
}
...where _subscribersExceptionCount is a dictionary that keeps consecutive exception failures count for each subscriber.
Any suggestion? When you say "refresh connections" you actually mean sending replies through...i.e. kind of heartbeat notifications?
Thanks!