Thanks Carl.
I am using C# and at this time I developed a simple client/server pair
where I use:
(server-side) a ConcurrentDictionary<string, IServerStreamWriter<
SubscribeStateReply>> in order to keep subscriber "references" (i.e. back
channels server will send event notifications through)
in order to keep "back channels" open I use (server-side) following
Subscribe method implementation:
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!
On Tuesday, January 31, 2017 at 9:39:42 PM UTC+1, Carl Mastrangelo wrote:
>
> Your client should establish a connection to the server, and just wait for
> events to come back. What you described sound right, though you should
> probably refresh connections every hour or so. Connections go stale
> occasionally and you should design for that possibility.
>
> What language are you using?
>
> On Monday, January 30, 2017 at 3:58:21 AM UTC-8, [email protected] wrote:
>>
>> Hi.
>>
>> What is the proper way to implement unsolicited events (fired by server
>> toward all its clients)? Should I use a never-returning server-stream rpc
>> method (i.e. a "subscribe" method) in order to keep a set
>> of IServerStreamWriter references and use such references to reach clients
>> back?
>>
>> Same pattern should apply to a pub/sub scenario, as far as I understand.
>>
>> Thanks!
>>
>
--
You received this message because you are subscribed to the Google Groups
"grpc.io" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/grpc-io.
To view this discussion on the web visit
https://groups.google.com/d/msgid/grpc-io/de79fcfc-c476-4e94-a70b-69969358905e%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.