예제 #1
0
 public PooledMultiplexer(ConfigurationOptions configuration, RespConnectionPoolOptions options)
 {
     Configuration = configuration.Clone();
     if (configuration.EndPoints.Count != 1)
     {
         throw new ArgumentException("A single endpoint is expected", nameof(configuration));
     }
     _pool = new RespConnectionPool(cancellation => ConnectAsync(cancellation), options);
 }
예제 #2
0
        async Task <Connection> CreateConnection(string addr)
        {
            ConfigurationOptions options = _options.Clone();

            options.EndPoints.Clear();
            options.EndPoints.Add(addr);
            return(new Connection
            {
                ConnectionMultiplexer = await ConnectionMultiplexer.ConnectAsync(options),
                Address = addr
            });
        }
예제 #3
0
        public ConnectionMultiplexer Connect(ConfigurationOptions configOpts)
        {
            ConnectionMultiplexer conn;

            if (string.IsNullOrWhiteSpace(configOpts.ServiceName))
            {
                var points = string.Join <EndPoint>(",", configOpts.EndPoints.ToArray());
                Logger.LogInformation($"Create Redis: {points},db:{configOpts.DefaultDatabase}");
                conn = ConnectionMultiplexer.Connect(configOpts);
            }
            else
            {
                var clusterConfig = configOpts.Clone();
                clusterConfig.EndPoints.Clear();

                //哨兵模式
                configOpts.TieBreaker = string.Empty;
                configOpts.Password   = string.Empty;
                configOpts.CommandMap = CommandMap.Sentinel;

                using (var sentinelConn = ConnectionMultiplexer.Connect(configOpts))
                {
                    //订阅哨兵事件
                    sentinelConn.GetSubscriber().Subscribe("+reset-master", (channel, value) =>
                    {
                        _connections.Clear();
                    });
                    var endpoints = GetAllRedisServersFromSentinel(sentinelConn, configOpts.ServiceName);
                    foreach (var endpoint in endpoints)
                    {
                        clusterConfig.EndPoints.Add(endpoint);
                    }
                    var points = string.Join <EndPoint>(",", endpoints);
                    Logger.LogInformation($"Create Redis: {points},db:{configOpts.DefaultDatabase}");
                    conn = ConnectionMultiplexer.Connect(clusterConfig);
                }
            }

            conn.ConfigurationChanged += (sender, e) =>
            {
                Logger.LogDebug($"Redis Configuration changed: {e.EndPoint}");
            };
            conn.ConnectionRestored += (sender, e) => { Logger.LogDebug($"Redis ConnectionRestored: {e.EndPoint}"); };
            conn.ErrorMessage       += (sender, e) => { Logger.LogError($"Redis Error{e.EndPoint}: {e.Message}"); };
            conn.ConnectionFailed   += (sender, e) =>
            {
                Logger.LogWarning(
                    $"Redis 重新连接:Endpoint failed: ${e.EndPoint}, ${e.FailureType},${e.Exception?.Message}");
            };
            conn.InternalError += (sender, e) => { Logger.LogWarning($"Redis InternalError:{e.Exception.Message}"); };
            return(conn);
        }
        protected RedisStoreImplBase(
            ConfigurationOptions redisConfig,
            string prefix,
            Logger log
            )
        {
            _log = log;
            var redisConfigCopy = redisConfig.Clone();

            _redis  = ConnectionMultiplexer.Connect(redisConfigCopy);
            _prefix = prefix;
            _log.Info("Using Redis data store at {0} with prefix \"{1}\"",
                      string.Join(", ", redisConfig.EndPoints.Select(DescribeEndPoint)), prefix);
        }
예제 #5
0
        private ConfigurationOptions TranslateDnsToIP(ConfigurationOptions configurationOptions)
        {
            var endpoints = configurationOptions.EndPoints
                            .Select(endpoint => endpoint as DnsEndPoint)
                            .SelectMany(endpoint =>
                                        Dns.GetHostAddressesAsync(endpoint.Host).Result
                                        .Select(ip => new IPEndPoint(ip, endpoint.Port))
                                        );
            var result = configurationOptions.Clone();

            result.EndPoints.Clear();
            foreach (var endpoint in endpoints)
            {
                result.EndPoints.Add(endpoint);
            }
            return(result);
        }
 public void setConfigurationOption(ConfigurationOptions option)
 {
     _option = option.Clone();
 }
 /// <summary>
 /// Specifies all Redis configuration options at once.
 /// </summary>
 /// <param name="config">a <see cref="ConfigurationOptions"/> instance</param>
 /// <returns>the same builder instance</returns>
 public RedisFeatureStoreBuilder WithRedisConfiguration(ConfigurationOptions config)
 {
     RedisConfig = config.Clone();
     return(this);
 }
예제 #8
0
        async Task <bool> Probe()
        {
            ConfigurationOptions configDB0 = _configurationOptions.Clone();

            configDB0.DefaultDatabase = 0;
            RedisConnector connectorDB0 = null;

            try
            {
                connectorDB0 = new RedisConnector(configDB0, true);
            }
            catch (Exception ex)
            {
                throw new RedisHelperException(
                          "Cannot create DbConnection.", ex);
            }

            IDatabase db0 = null;

            try
            {
                db0 = connectorDB0.Connection.GetDatabase(0);
            }
            catch (Exception ex)
            {
                throw new RedisHelperException("Cannot GetDatabase(0).", ex);
            }

            TimeSpan pingDuration = default(TimeSpan);

            try
            {
                pingDuration = await db0.PingAsync();
            }
            catch (Exception ex)
            {
                throw new RedisHelperException("Cannot ping db0.", ex);
            }

            try
            {
                _server = connectorDB0.Connection.GetServer(
                    configDB0.EndPoints[0]);
                _features = _server.Features;
            }
            catch (Exception ex)
            {
                throw new RedisHelperException(
                          "Cannot get server features.", ex);
            }

            try
            {
                IGrouping <string, KeyValuePair <string, string> >[] info =
                    await _server.InfoAsync("Server");

                if (info[0].Key.Equals("Server",
                                       StringComparison.CurrentCultureIgnoreCase))
                {
                    foreach (KeyValuePair <string, string> kvp in info[0])
                    {
                        if (kvp.Key.Equals("redis_version",
                                           StringComparison.CurrentCultureIgnoreCase))
                        {
                            _version = kvp.Value;
                            break;
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                throw new RedisHelperException(
                          "Cannot get redis_version.", ex);
            }

            sbyte maxDb = 0;

            do
            {
                try
                {
                    IDatabase nxtDb = connectorDB0
                                      .Connection.GetDatabase(maxDb);
                    RedisResult rr = await nxtDb.ExecuteAsync("PING");

                    if (rr == null || rr.IsNull ||
                        !rr.ToString().Equals(
                            "PONG", StringComparison.CurrentCultureIgnoreCase))
                    {
                        break;
                    }
                    ++maxDb;
                }
                catch
                {
                    // expected... swallow
                    break;
                }
            } while (maxDb < sbyte.MaxValue);
            _maxDb = maxDb;

            return(true);
        }
예제 #9
0
 /// <summary>
 /// Specifies all Redis configuration options at once.
 /// </summary>
 /// <param name="config">a <see cref="ConfigurationOptions"/> instance</param>
 /// <returns>the builder</returns>
 public RedisDataStoreBuilder RedisConfiguration(ConfigurationOptions config)
 {
     _redisConfig = config.Clone();
     return(this);
 }