Example #1
0
        /// <summary>
        /// Starts or restarts the handler listening for the [ProxyUpdateMessage] messages.
        /// </summary>
        private static void StartNotifyHandler()
        {
            lock (syncLock)
            {
                // Use the latest settings to reconnect to the [proxy-notify] channel.

                if (proxyNotifyChannel != null)
                {
                    proxyNotifyChannel.Dispose();
                }

                proxyNotifyChannel = hive.HiveMQ.Internal.GetProxyNotifyChannel(useBootstrap: true).Open();

                // Register a handler for [ProxyUpdateMessage] messages that determines
                // whether the message is meant for this service instance and handle it.

                proxyNotifyChannel.ConsumeAsync <ProxyUpdateMessage>(
                    async message =>
                {
                    // We cannot process updates in parallel so we'll use an
                    // AsyncMutex to prevent this.

                    using (await asyncLock.AcquireAsync())
                    {
                        var forThisInstance = false;

                        if (isPublic)
                        {
                            forThisInstance = message.PublicProxy && !isBridge ||
                                              message.PublicBridge && isBridge;
                        }
                        else
                        {
                            forThisInstance = message.PrivateProxy && !isBridge ||
                                              message.PrivateBridge && isBridge;
                        }

                        if (!forThisInstance)
                        {
                            log.LogInfo(() => $"HAPROXY-SHIM: Received but ignorning: {message}");
                            return;
                        }

                        log.LogInfo(() => $"HAPROXY-SHIM: Received: {message}");

                        var jitter = NeonHelper.RandTimespan(HiveConst.MaxJitter);

                        log.LogDebug(() => $"HAPROXY-SHIM: Jitter delay [{jitter}].");
                        await Task.Delay(jitter);

                        await ConfigureHAProxy();
                    }
                });
            }
        }