Example #1
0
        /// <summary>
        /// Subscribes a connection to cluster events.
        /// </summary>
        /// <param name="connection">The connection.</param>
        /// <param name="correlationId">The correlation identifier.</param>
        /// <param name="cancellationToken">A cancellation token.</param>
        /// <returns>A task that will complete when the subscription has been processed, and represent whether it was successful.</returns>
        private async Task <bool> SubscribeToClusterEventsAsync(MemberConnection connection, long correlationId, CancellationToken cancellationToken)
        {
            // aka subscribe to member/partition view events
            HConsole.TraceLine(this, "subscribe");

            // handles the event
            ValueTask HandleEventAsync(ClientMessage message, object _)
            => ClientAddClusterViewListenerCodec.HandleEventAsync(message,
                                                                  HandleCodecMemberViewEvent,
                                                                  HandleCodecPartitionViewEvent,
                                                                  connection.Id,
                                                                  _clusterState.LoggerFactory);

            try
            {
                var subscribeRequest = ClientAddClusterViewListenerCodec.EncodeRequest();
                _correlatedSubscriptions[correlationId] = new ClusterSubscription(HandleEventAsync);
                _ = await _clusterMessaging.SendToMemberAsync(subscribeRequest, connection, correlationId, cancellationToken).CfAwait();

                HConsole.WriteLine(this, "subscribed");
                return(true);
            }
            catch (Exception e)
            {
                HConsole.WriteLine(this, "failed " + e);
                _correlatedSubscriptions.TryRemove(correlationId, out _);
                _logger.LogWarning(e, "Failed to subscribe to cluster events, may retry.");
                return(false);
            }
        }
            public IPAddress[] GetHostAddresses(string hostNameOrAddress)
            {
                Count++;

                HConsole.TraceLine(this, $"GetHostAddresses {hostNameOrAddress} {Count}/{_failAt}");

                if (Count == _failAt)
                {
                    throw new SocketException((int)SocketError.HostNotFound);
                }

                return(Dns.GetHostAddresses(hostNameOrAddress));
            }