private PhysicalBridge CreateBridge(ConnectionType type, TextWriter log)
        {
            multiplexer.Trace(type.ToString());
            var bridge = new PhysicalBridge(this, type);

            bridge.TryConnect(log);
            return(bridge);
        }
示例#2
0
        public void Execute()
        {
            var snapshot = pending;

            pending = null;
            if (snapshot == null || snapshot.Count == 0)
            {
                return;
            }

            // group into per-bridge chunks
            var byBridge = new Dictionary <PhysicalBridge, List <Message> >();

            // optimisation: assume most things are in a single bridge
            PhysicalBridge lastBridge = null;
            List <Message> lastList   = null;

            foreach (var message in snapshot)
            {
                var server = multiplexer.SelectServer(message);
                if (server == null)
                {
                    FailNoServer(snapshot);
                    throw ExceptionFactory.NoConnectionAvailable(multiplexer.IncludeDetailInExceptions, message.Command, message, server, multiplexer.GetServerSnapshot());
                }
                var bridge = server.GetBridge(message.Command);
                if (bridge == null)
                {
                    FailNoServer(snapshot);
                    throw ExceptionFactory.NoConnectionAvailable(multiplexer.IncludeDetailInExceptions, message.Command, message, server, multiplexer.GetServerSnapshot());
                }

                // identity a list
                List <Message> list;
                if (bridge == lastBridge)
                {
                    list = lastList;
                }
                else if (!byBridge.TryGetValue(bridge, out list))
                {
                    list = new List <Message>();
                    byBridge.Add(bridge, list);
                }
                lastBridge = bridge;
                lastList   = list;

                list.Add(message);
            }

            foreach (var pair in byBridge)
            {
                if (!pair.Key.TryEnqueue(pair.Value, pair.Key.ServerEndPoint.IsSlave))
                {
                    FailNoServer(pair.Value);
                }
            }
        }
        public void Dispose()
        {
            isDisposed = true;
            var tmp = interactive;

            interactive = null;
            tmp?.Dispose();

            tmp          = subscription;
            subscription = null;
            tmp?.Dispose();
        }
        public PhysicalBridge GetBridge(ConnectionType type, bool create = true, TextWriter log = null)
        {
            if (isDisposed)
            {
                return(null);
            }
            switch (type)
            {
            case ConnectionType.Interactive:
                return(interactive ?? (create ? interactive = CreateBridge(ConnectionType.Interactive, log) : null));

            case ConnectionType.Subscription:
                return(subscription ?? (create ? subscription = CreateBridge(ConnectionType.Subscription, log) : null));
            }
            return(null);
        }
        public PhysicalConnection(PhysicalBridge bridge)
        {
            lastWriteTickCount  = lastReadTickCount = Environment.TickCount;
            lastBeatTickCount   = 0;
            this.connectionType = bridge.ConnectionType;
            this.Multiplexer    = bridge.Multiplexer;
            this.ChannelPrefix  = Multiplexer.RawConfig.ChannelPrefix;
            if (this.ChannelPrefix != null && this.ChannelPrefix.Length == 0)
            {
                this.ChannelPrefix = null;                                                               // null tests are easier than null+empty
            }
            var endpoint = bridge.ServerEndPoint.EndPoint;

            physicalName = connectionType + "#" + Interlocked.Increment(ref totalCount) + "@" + Format.ToString(endpoint);
            this.Bridge  = bridge;
#if CORE_CLR
            endRead = EndReadFactory(this);
#endif
            OnCreateEcho();
        }
        public ServerEndPoint(ConnectionMultiplexer multiplexer, EndPoint endpoint, TextWriter log)
        {
            this.multiplexer = multiplexer;
            this.endpoint    = endpoint;
            var config = multiplexer.RawConfig;

            version           = config.DefaultVersion;
            slaveReadOnly     = true;
            isSlave           = false;
            databases         = 0;
            writeEverySeconds = config.KeepAlive > 0 ? config.KeepAlive : 60;
            interactive       = CreateBridge(ConnectionType.Interactive, log);
            serverType        = ServerType.Standalone;

            // overrides for twemproxy
            if (multiplexer.RawConfig.Proxy == Proxy.Twemproxy)
            {
                databases  = 1;
                serverType = ServerType.Twemproxy;
            }
        }
 internal void QueueDirectFireAndForget <T>(Message message, ResultProcessor <T> processor, PhysicalBridge bridge = null)
 {
     if (message != null)
     {
         message.SetSource(processor, null);
         multiplexer.Trace("Enqueue: " + message);
         (bridge ?? GetBridge(message.Command)).TryEnqueue(message, isSlave);
     }
 }
        internal Task <T> QueueDirectAsync <T>(Message message, ResultProcessor <T> processor, object asyncState = null, PhysicalBridge bridge = null)
        {
            var tcs    = TaskSource.CreateDenyExecSync <T>(asyncState);
            var source = ResultBox <T> .Get(tcs);

            message.SetSource(processor, source);
            if (bridge == null)
            {
                bridge = GetBridge(message.Command);
            }
            if (!bridge.TryEnqueue(message, isSlave))
            {
                ConnectionMultiplexer.ThrowFailed(tcs, ExceptionFactory.NoConnectionAvailable(multiplexer.IncludeDetailInExceptions, message.Command, message, this, multiplexer.GetServerSnapshot()));
            }
            return(tcs.Task);
        }