internal RedisConnection(IPEndPoint[] endpoints, RedisClientOptions options)
        {
            _loadFactor             = 100;
            _endpoints              = endpoints;
            _options                = options;
            _connectionCancellation = new CancellationTokenSource();
            _requests               = options.QueuesBoundedCapacity.HasValue ? new BlockingCollection <ExecutionToken>(options.QueuesBoundedCapacity.Value) : new BlockingCollection <ExecutionToken>();
            _pending                = new ConcurrentQueue <ExecutionToken>();
            _logger      = _options.Logger;
            _code        = this.GetHashCode().ToString();
            Initializers = new List <IConnectionInitializer>();
            Initializers.Add(new ConnectionInitializer(_options));

            if (options.PingTimeout != Timeout.InfiniteTimeSpan)
            {
                _interval       = TimeSpan.FromMilliseconds(options.PingTimeout.TotalMilliseconds / 2);
                _pingTimer      = new Timer(o => { try { Execute(GeneratePingToken(), CancellationToken.None); } catch (Exception) { } });
                _skipTimerReset = !options.PreventPingIfActive;
            }
            else
            {
                _interval       = Timeout.InfiniteTimeSpan;
                _skipTimerReset = true;
            }
            _logger.Info("Created connection {0} of type {1}.", _code, this.GetType().Name);
        }
Beispiel #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RedisClient"/> class.
        /// </summary>
        /// <param name="endpoint">The Redis endpoint.</param>
        /// <param name="options"><see cref="RedisClientOptions"/></param>
        public RedisClient(IPEndPoint endpoint, RedisClientOptions options = null)
            : this(new[] { endpoint }, options)
        {
            ParameterGuard.CannotBeNull(endpoint, "endpoint");

            _endpoints = new[] { endpoint };
        }
Beispiel #3
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RedisClient"/> class.
        /// </summary>
        /// <param name="endpoints">The Redis endpoints. The selected endpoint is selected in a rota basis.</param>
        /// <param name="options"><see cref="RedisClientOptions"/></param>
        public RedisClient(IPEndPoint[] endpoints, RedisClientOptions options = null)
            : this(options)
        {
            ParameterGuard.CannotBeNullOrEmpty(endpoints, "endpoints");

            _endpoints = endpoints.ToArray();

            _procedures = _options.Procedures != null?_options.Procedures.ToCollection() : ProcedureCollection.Empty;

            _proceduresInitializer = new ProcedureInitializer(_procedures, _options.Logger);

            _multiplexedCommander = new AggregatedCommandConnection <RedisCommanderConnection>(_options.MultiplexPool.CommandConnections, CommanderFactory, _options);
            _subscriptorsPool     = new ConnectionSelector <RedisSubscriberConnection>(_options.MultiplexPool.SubscriptionOptions, SubscriberFactory, _options);

            if (_options.ExclusivePool.Maximum > 0)
            {
                _exclusivePool = new ConnectionPool(_options.ExclusivePool.Minimum, _options.ExclusivePool.Maximum, CommanderFactory, _options.Logger);
            }
            else
            {
                _exclusivePool = DisabledConnectionPool.Instance;
            }

            IExecutionPlanner planner = new ExecutionPlanner(_procedures);
            _planner = _options.UseExecutionPlanCaching ? new CachingExecutionPlanner(planner) : planner;
        }
Beispiel #4
0
        private RedisClient(RedisClientOptions options)
        {
            _cancel  = new CancellationTokenSource();
            _options = options != null?options.Clone() : RedisClientOptions.Default;

            _logger = _options.Logger;
        }
        internal RedisClientOptions Clone()
        {
            try
            {
                ValidateMultiplex(this.MultiplexPool);
                ValidateExclusive(this.ExclusivePool);

                var clone = new RedisClientOptions()
                {
                    PingTimeout             = this.PingTimeout,
                    ReadBufferSize          = this.ReadBufferSize,
                    WriteBufferSize         = this.WriteBufferSize,
                    QueuesBoundedCapacity   = this.QueuesBoundedCapacity,
                    _procedureLoader        = this.Procedures,
                    UseExecutionPlanCaching = this.UseExecutionPlanCaching,
                    LoadBasedSelector       = this.LoadBasedSelector ?? new BasicLoadBasedSelector(),
                    UseNagleAlgorithm       = this.UseNagleAlgorithm,
                    PreventPingIfActive     = this.PreventPingIfActive,
                    Logger            = this.Logger ?? NoLogger.Instance,
                    MultiplexPool     = this.MultiplexPool,
                    ExclusivePool     = this.ExclusivePool,
                    ConnectionTimeout = this.ConnectionTimeout
                };
                if (_initializationCmds != null)
                {
                    clone._initializationCmds = new List <PreInitializationCommand>(_initializationCmds);
                }
                return(clone);
            }
            catch (Exception ex)
            {
                throw new RedisClientConfigurationException(ex.Message, ex);
            }
        }
Beispiel #6
0
        public ConnectionInitializer(RedisClientOptions options)
        {
            if (options == null)
            {
                return;
            }

            _logger   = options.Logger ?? NoLogger.Instance;
            _commands = GenerateCommands(options).ToArray();
        }
Beispiel #7
0
        internal ConnectionSelector(Int32 count, Func <TConnection> factory, RedisClientOptions options)
        {
            _connections = Enumerable
                           .Range(0, count)
                           .Select(i => factory())
                           .ToArray();

            _selector = options.LoadBasedSelector;
            _options  = options;
        }
        internal AggregatedCommandConnection(Int32 count, Func <TConnection> factory, RedisClientOptions options)
        {
            Contract.Assert(options != null, "Redis optiosn cannot be null.");

            _commanders = Enumerable
                          .Range(0, count)
                          .Select(i => factory())
                          .ToArray();

            _selector = options.LoadBasedSelector;
            _options  = options;
        }
Beispiel #9
0
        static List <RESPCommand> GenerateCommands(RedisClientOptions options)
        {
            var list    = new List <RESPCommand>();
            var planner = new ExecutionPlanner();

            foreach (var cmd in options.InitializationCommands)
            {
                var plan        = planner.Build(cmd.Command);
                var respCommand = plan.Bind(cmd.Parameters);
                list.AddRange(respCommand);
            }
            return(list);
        }
 internal RedisSubscriberConnection(IPEndPoint[] endpoints, RedisClientOptions options)
     : base(endpoints, options)
 {
     _subscriptions = new SubscriptionSplitter();
     Initializers.Add(new SubscriptionsInitialization(_subscriptions));
 }
 internal RedisCommanderConnection(IPEndPoint[] endpoints, RedisClientOptions options, ProcedureInitializer proceduresInitializer)
     : base(endpoints, options)
 {
     Initializers.Add(proceduresInitializer);
 }