// methods
        public void Initialize()
        {
            if (_state.TryChange(State.Initial, State.Open))
            {
                if (_listener != null)
                {
                    _listener.ServerBeforeOpening(_serverId, _settings);
                }

                var stopwatch = Stopwatch.StartNew();
                _connectionPool.Initialize();
                var metronome = new Metronome(_settings.HeartbeatInterval);
                AsyncBackgroundTask.Start(
                    HeartbeatAsync,
                    ct =>
                {
                    var newDelay = new InterruptibleDelay(metronome.GetNextTickDelay(), ct);
                    Interlocked.Exchange(ref _heartbeatDelay, newDelay);
                    return(newDelay.Task);
                },
                    _heartbeatCancellationTokenSource.Token)
                .HandleUnobservedException(ex => { });     // TODO: do we need to do anything here?
                stopwatch.Stop();

                if (_listener != null)
                {
                    _listener.ServerAfterOpening(_serverId, _settings, stopwatch.Elapsed);
                }
            }
        }
 private void StartBackgroundTasks()
 {
     AsyncBackgroundTask.Start(SendBackgroundTask, TimeSpan.FromMilliseconds(0), _backgroundTaskCancellationTokenSource.Token)
     .HandleUnobservedException(ConnectionFailed);
     AsyncBackgroundTask.Start(ReceiveBackgroundTask, TimeSpan.FromMilliseconds(0), _backgroundTaskCancellationTokenSource.Token)
     .HandleUnobservedException(ConnectionFailed);
 }
        public void Initialize()
        {
            ThrowIfDisposed();
            if (_state.TryChange(State.Initial, State.Open))
            {
                if (_listener != null)
                {
                    _listener.ConnectionPoolBeforeOpening(_serverId, _settings);
                }
                AsyncBackgroundTask.Start(
                    ct => MaintainSizeAsync(ct),
                    _settings.MaintenanceInterval,
                    _maintenanceCancellationTokenSource.Token)
                .HandleUnobservedException(ex => { });     // TODO: do we need to handle any error here?

                if (_listener != null)
                {
                    _listener.ConnectionPoolAfterOpening(_serverId, _settings);
                }
            }
        }
Example #4
0
        public override void Initialize()
        {
            base.Initialize();
            if (_state.TryChange(State.Initial, State.Open))
            {
                if (Listener != null)
                {
                    Listener.ClusterBeforeOpening(ClusterId, Settings);
                }

                var stopwatch = Stopwatch.StartNew();
                AsyncBackgroundTask.Start(
                    MonitorServersAsync,
                    TimeSpan.Zero,
                    _monitorServersCancellationTokenSource.Token)
                .HandleUnobservedException(ex => { });     // TODO: do we need to handle any error here?

                // We lock here even though AddServer locks. Monitors
                // are re-entrant such that this won't cause problems,
                // but could prevent issues of conflicting reports
                // from servers that are quick to respond.
                var clusterDescription = Description;
                lock (_serversLock)
                {
                    foreach (var endPoint in Settings.EndPoints)
                    {
                        clusterDescription = EnsureServer(clusterDescription, endPoint);
                    }
                }

                UpdateClusterDescription(clusterDescription);
                stopwatch.Stop();

                if (Listener != null)
                {
                    Listener.ClusterAfterOpening(ClusterId, Settings, stopwatch.Elapsed);
                }
            }
        }