private Redis()
        {
            try
            {
                redis = ConnectionMultiplexer.Connect("localhost");
                DB = redis.GetDatabase();
            }
            catch (RedisConnectionException rce)
            {

                ProcessStartInfo startInfo = new ProcessStartInfo();
                startInfo.Arguments = Environment.CurrentDirectory + @"\redis\redis.windows.conf";
                startInfo.FileName = Environment.CurrentDirectory + @"\redis\redis-server.exe";
                startInfo.WorkingDirectory = Environment.CurrentDirectory + @"\redis\";
                Process p = Process.Start(startInfo);
                Thread.Sleep(200);
                for (int i = 0; i < 3; ++i)
                {
                    try
                    {
                        redis = ConnectionMultiplexer.Connect("localhost");
                        DB = redis.GetDatabase();
                        break;
                    }
                    catch (RedisConnectionException rce2)
                    {
                        Thread.Sleep(1000);
                    }
                }
            }

        }
 /// <summary>
 /// Initializes a new instance of the <see cref="RedisCache"/> class.
 /// </summary>
 /// <param name="configuration">The configuration.</param>
 public RedisCache(ConfigurationOptions configuration)
 {
     if (RedisCache.connection == null)
     {
         try
         {
             connectionTask = ConnectionMultiplexer.ConnectAsync(configuration);
             connectionTask.ContinueWith(t =>
                 {
                     lock (syncronizationObject)
                     {
                         if (RedisCache.connection == null)
                             RedisCache.connection = t.Result;
                     }
                     this.cache = RedisCache.connection.GetDatabase();
                     Trace.TraceInformation("Redis Cache Provider connection complete - Correlation Id = {0}", Trace.CorrelationManager.ActivityId);
                 });
         }
         catch (AggregateException age)
         {
             age.Handle(e =>
             {
                 Trace.TraceError("Redis Cache Provider error - Correlation Id = {0}\n {1}\n {2}", Trace.CorrelationManager.ActivityId, e.Message, e.StackTrace);
                 return true;
             });
         }
         catch (Exception ex)
         {
             Trace.TraceError("Redis Cache Provider exception - Correlation Id = {0}\n {1}\n {2}", Trace.CorrelationManager.ActivityId, ex.Message, ex.StackTrace);
         }
     }
 }
Exemplo n.º 3
0
 public MessagesController(ApplicationDbContext context, UserManager<ApplicationUser> userManager, MapperConfiguration mapperConfiguration, ConnectionMultiplexer redis)
 {
     _context = context;
     _userManager = userManager;
     _mapperConfiguration = mapperConfiguration;
     _redis = redis;
 }
Exemplo n.º 4
0
        public RedisCache(string regionName, IDictionary<string, string> properties, RedisCacheElement element, ConnectionMultiplexer connectionMultiplexer, RedisCacheProviderOptions options)
        {
            this.connectionMultiplexer = connectionMultiplexer.ThrowIfNull("connectionMultiplexer");
            this.options = options.ThrowIfNull("options").ShallowCloneAndValidate();

            RegionName = regionName.ThrowIfNull("regionName");

            if (element == null)
            {
                expiry = TimeSpan.FromSeconds(
                    PropertiesHelper.GetInt32(Cfg.Environment.CacheDefaultExpiration, properties, DefaultExpiry)
                );
            }
            else
            {
                expiry = element.Expiration;
            }

            log.DebugFormat("using expiration : {0} seconds", expiry.TotalSeconds);

            var @namespace = CacheNamePrefix + RegionName;

            CacheNamespace = new RedisNamespace(@namespace);
            SyncInitialGeneration();
        }
        public static ConnectionMultiplexer RetrieveMultiplexer(ConfigurationOptions config)
        {
           if(_multiplexer != null) return _multiplexer;

            _multiplexer = ConnectionMultiplexer.Connect(config);
            return _multiplexer;
        }
        public static ConnectionMultiplexer RetrieveMultiplexer(string connectionString)
        {
           if(_multiplexer != null) return _multiplexer;

            _multiplexer = ConnectionMultiplexer.Connect(connectionString);
            return _multiplexer;
        }
 public StackExchange.Redis.ConnectionMultiplexer Create()
 {
     Logger.Info("Connect to Redis.");
     if (instance != null && instance.IsConnected)
     {
         Logger.Info("Redis is connected 1");
         return instance;
     }
     lock (LockObject)
     {
         if (instance != null && instance.IsConnected)
         {
             Logger.Info("Redis is connected 2");
             return instance;
         }
         if (instance != null)
         {
             instance.Dispose();
             Logger.Info("Connection disconnected. Disposing connection...");
         }
         instance = ConnectionMultiplexer.Connect(redisConnectionString);
         Logger.Info("Creating new instance of Redis Connection");
     }
     return instance;
 }
        public RedisStorage(string connectionString, RedisStorageOptions options = null)
        {
            if (connectionString == null) throw new ArgumentNullException("connectionString");
            if (options == null) options = new RedisStorageOptions();

            _connectionMultiplexer = ConnectionMultiplexer.Connect(connectionString);
            _invisibilityTimeout = options.InvisibilityTimeout;
            var endpoint = _connectionMultiplexer.GetEndPoints()[0];
            if (endpoint is IPEndPoint)
            {
                var ipEp = endpoint as IPEndPoint;
                ConnectionString = string.Format("{0}:{1}", TryGetHostName(ipEp.Address), ipEp.Port);
            }
            else
            {
                var dnsEp = endpoint as DnsEndPoint;
                ConnectionString = string.Format("{0}:{1}", dnsEp.Host, dnsEp.Port);
            }

            Db = options.Db;
            if (Prefix != options.Prefix)
            {
                Prefix = options.Prefix;
            }
            identity = Guid.NewGuid().ToString();
        }
Exemplo n.º 9
0
 public RedisSubscriptionBroker(ConnectionMultiplexer connection, string prefix)
 {
     this.connection = connection;
     this.subscriptionChannel = string.Format("{0}.subscriptions.broker", prefix);
     this.handlers = new ConcurrentBag<Action<SubscriptionChange, Subscription>>();
     this.initializationTask = new Lazy<Task>(() => this.Initialize());
 }
Exemplo n.º 10
0
        static RedisCacheConfig()
        {
            string isLocal = WebConfigReader.Read("IsLocal");

            if (isLocal == "1")
            {
                connection = ConnectionMultiplexer.Connect(WebConfigReader.Read("RedisServer"));
            }
            else
            {
                var options = new ConfigurationOptions();

                options.EndPoints.Add(WebConfigReader.Read("RedisKeyDns"), 6380);
                options.Ssl = true;

                options.Password = WebConfigReader.Read("RedisPassword");
                options.AllowAdmin = true;

                // necessary?
                options.KeepAlive = 30;
                options.ConnectTimeout = 15000;
                options.SyncTimeout = 15000;

                connection = ConnectionMultiplexer.Connect(options);
            }
        }
 /// <summary>
 /// Construct a new redis connection (which will be actually setup on demand)
 /// with an object to synchronize access on.
 /// </summary>
 public AsyncRedisConnection(string tier, string delimitedConfiguration, Func<bool> shouldPromoteSlaveToMaster, string tieBreakerKey, string name, bool preserveAsyncOrder = false, bool shareSocketManager = false, TextWriter log = null)
 {
     if (log != null) log.WriteLine("{0} > AsyncRedisConnection", DateTime.UtcNow.ToShortDateString());
     var options = ConfigurationOptions.Parse(delimitedConfiguration, true);
     muxer = Create(tier, options, tieBreakerKey, name, preserveAsyncOrder, shareSocketManager, log, out subscriber);
     if (log != null) log.WriteLine("{0} < AsyncRedisConnection", DateTime.UtcNow.ToShortDateString());
 }
Exemplo n.º 12
0
 public async Task OpenAsync(PartitionContext context)
 {
     if (!WebJobsHelper.RunAsWebJobs)
         Console.WriteLine(string.Format("EventProcessor initialization. Partition: '{0}', Offset: '{1}'",
             context.Lease.PartitionId, context.Lease.Offset));
     partitionContext = context;
     var retries = 3;
     while (retries > 0)
     {
         try
         {
             retries--;
             hubClient = EventHubClient.CreateFromConnectionString(
                 ConfigurationManager.ConnectionStrings["SigfoxDemoAlertSender"].ConnectionString,
                 "alert");
             cacheConnection = await ConnectionMultiplexer.ConnectAsync(ConfigurationManager.ConnectionStrings["SigfoxDemoCache"].ConnectionString);
             cacheDatabase = cacheConnection.GetDatabase();
             sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SigfoxDemoDatabase"].ConnectionString);
             //sqlConnection.Open();
             //sqlCommand = new SqlCommand("InsertAlert", sqlConnection) { CommandType = CommandType.StoredProcedure };
             //sqlCommand.Parameters.Add(new SqlParameter("@Device", SqlDbType.VarChar));
             retries = 0;
         }
         catch (Exception e)
         {
             Console.Error.WriteLine("Error opening destination Event Hub: " + e.Message);
             if (retries == 0)
                 throw;
         }
     }
     checkpointStopWatch = new Stopwatch();
     checkpointStopWatch.Start();
 }
        public static void Init(string sHost, int iPort, string sPassword)
        {
            CacheDatabase._Host = sHost;
            CacheDatabase._Port = iPort;
            CacheDatabase._Password = sPassword;
            if (CacheDatabase._Password.Length > 0)
            {
                CacheDatabase._AuthRequired = true;
            }
            else
            {
                CacheDatabase._AuthRequired = false;
            }

            _oConectionOptions = new ConfigurationOptions();
            if (CacheDatabase._AuthRequired)
            {
                CacheDatabase._oConectionOptions.Password = CacheDatabase._Password;
            }

            CacheDatabase._oConectionOptions.EndPoints.Add(CacheDatabase._Host + ":" + CacheDatabase._Port.ToString());
            CacheDatabase._oCacheConnection = ConnectionMultiplexer.Connect(CacheDatabase._oConectionOptions);
            CacheDatabase._oCommand = CacheDatabase._oCacheConnection.GetDatabase();
            //Check to make sure the Key Exists and if not then set it to 0
            if (!_oCommand.KeyExists(CacheDatabase._ObjectCounterKeyName))
            {
                CacheDatabase._oCommand.StringSet(CacheDatabase._ObjectCounterKeyName, 0);
            }
        }
Exemplo n.º 14
0
        public RedisAdaptor(List<EndPoint> endPoints)
        {
            var config = new ConfigurationOptions()
            {
                AllowAdmin = true,
            };

            foreach (var endPoint in endPoints)
            {
                config.EndPoints.Add(endPoint);
            }

            muxerInstance = ConnectionMultiplexer.Connect(config);

            Handle = muxerInstance.GetDatabase();

            var script = Load("update_multikeys_multifields.lua");

            //todo a hack way .. to be changed later
            foreach (var endPoint in muxerInstance.GetEndPoints())
            {
                var server = muxerInstance.GetServer(endPoint);

                updateScriptSha = server.ScriptLoad(script);
            }

            Handle.StringSet("test", "111");
        }
Exemplo n.º 15
0
        internal WorkflowManagement(ConnectionMultiplexer mux, ITaskHandler taskHandler, WorkflowHandler workflowHandler, string identifier, IEnumerable<string> typesProcessed, ILua lua, EventHandler<Exception> exceptionHandler = null, Behaviours behaviours = Behaviours.All)
        {
            _taskHandler = taskHandler;

            _workflowHandler = workflowHandler;

            if (exceptionHandler != null)
            {
                ExceptionThrown += exceptionHandler;
            }

            _typesProcessed = typesProcessed;

            _db = mux.GetDatabase();

            _sub = mux.GetSubscriber();

            if (_typesProcessed == null || _typesProcessed.Count() == 0)
            {
                _sub.Subscribe("submittedTask", (c, v) =>
                {
                    ProcessNextTask();
                });
            }
            else
            {
                foreach(var t in _typesProcessed)
                {
                    _sub.Subscribe("submittedTask:" + t, (c, v) =>
                    {
                        ProcessNextTask(t);
                    });
                }
            }

            _sub.Subscribe("workflowFailed", (c, v) =>
            {
                ProcessNextFailedWorkflow();
            });

            _sub.Subscribe("workflowComplete", (c, v) =>
            {
                ProcessNextCompleteWorkflow();
            });

            _lua = lua;
            _lua.LoadScripts(_db, mux.GetServer("localhost:6379"));

            _identifier = identifier;

            if (behaviours.HasFlag(Behaviours.AutoRestart))
            {
                var resubmittedTasks = ResubmitTasks();

                foreach (var item in resubmittedTasks)
                {
                    Console.WriteLine("Resubmitted {0}", item);
                }
            }
        }
Exemplo n.º 16
0
        public WatcherManager(IEnumerable<WatchGroup> groups)
        {
            var groupList = groups.ToList();
            var config = new ConfigurationOptions()
            {
                AllowAdmin = true,
            };

            foreach (var group in groupList)
            {
                config.EndPoints.Add(group.Master.EndPoint);
            }

            muxerInstance = ConnectionMultiplexer.Connect(config);
            muxerInstance.ConnectionRestored += MuxerInstanceOnConnectionRestored;

            foreach (var group in groupList)
            {
                var server = muxerInstance.GetServer(group.Master.EndPoint);
                var epStr = server.EndPoint.ToString();

                group.Master.Server = server;
                group.Master.OnPing(TimeSpan.Zero);

                Program.zkAdaptor.Identity(group.Master.EndPoint.ToString());
                this.groups.Add(epStr, group);
                redisInstancesDict.Add(epStr, group.Master);
            }
        }
Exemplo n.º 17
0
        public RedisManager()
        {
            var configurationOptions = new ConfigurationOptions {Password = "******"};
            configurationOptions.EndPoints.Add(HostAndPort);

            _redisConn = ConnectionMultiplexer.Connect(configurationOptions);
        }
        public static void Connect()
        {
            ConfigurationOptions options = new ConfigurationOptions();
            options.EndPoints.Add("localhost:6379");

            Connection = ConnectionMultiplexer.Connect(options);
        }
        /// <summary>
        /// Use Redis as the messaging backplane for scaling out of ASP.NET SignalR applications in a web farm.
        /// </summary>
        /// <param name="resolver">The dependency resolver</param>
        /// <param name="configuration">The Redis scale-out configuration options.</param>
        /// <param name="sharedMultiplexer">shared multiplexer</param>
        /// <returns>The dependency resolver.</returns>
        public static IDependencyResolver UseRedis(this IDependencyResolver resolver, RedisScaleoutConfiguration configuration, ConnectionMultiplexer sharedMultiplexer)
        {
            var bus = new Lazy<RedisMessageBus>(() => new RedisMessageBus(resolver, configuration, new RedisConnection(sharedMultiplexer)));
            resolver.Register(typeof(IMessageBus), () => bus.Value);

            return resolver;
        }
Exemplo n.º 20
0
        public MainForm()
        {
            InitializeComponent();
            var options = new ConfigurationOptions
            {
                ConnectTimeout = 5000,
                SyncTimeout = 2000,
                KeepAlive = 60,
                EndPoints =
                {
                    {Settings.Default.RedisHost, Settings.Default.RedisPort}
                }
            };

            _redis = ConnectionMultiplexer.Connect(options);
            var retries = 0;
            while (!_redis.IsConnected)
            {
                var config = _redis.Configuration;
                _redis.Dispose();
                if (retries > 10)
                {
                    MessageBox.Show(string.Format("Could not connect to the Redis server with configuration: {0}",
                        config));
                    Application.Exit();
                }

                _redis = ConnectionMultiplexer.Connect(options, Console.Out);
                retries++;

            }
            _red = new LightControlSet(button_RedOn, button_RedOff, button_RedFlash, textBox_RedOnDuty, textBox_RedOffDuty, textBox_RedOffset, textBox_RedPower, button_RedApply);
            _green = new LightControlSet(button_GreenOn, button_GreenOff, button_GreenFlash, textBox_GreenOnDuty, textBox_GreenOffDuty, textBox_GreenOffset, textBox_GreenPower, button_GreenApply);
            _blueYellow = new LightControlSet(button_BlueOn, button_BlueOff, button_BlueFlash, textBox_BlueOnDuty, textBox_BlueOffDuty, textBox_BlueOffset, textBox_BluePower, button_BlueApply);
        }
Exemplo n.º 21
0
 public RedisEntityTagStore(ConnectionMultiplexer connection, 
     int databaseId = 0,
     TimeSpan? expiry = null)
 {
     _expiry = expiry;
     Init(connection, databaseId);
 }
Exemplo n.º 22
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public AbpRedisCache(string name, IAbpRedisConnectionProvider redisConnectionProvider, AbpRedisCacheConfig config)
     : base(name)
 {
     _config = config;
     var connectionString = redisConnectionProvider.GetConnectionString(_config.ConnectionStringKey);
     _connectionMultiplexer = redisConnectionProvider.GetConnection(connectionString);
 }
Exemplo n.º 23
0
 public async Task OpenAsync(PartitionContext context)
 {
     if (!WebJobsHelper.RunAsWebJobs)
         Console.WriteLine(string.Format("EventProcessor initialization. Partition: '{0}', Offset: '{1}'",
             context.Lease.PartitionId, context.Lease.Offset));
     partitionContext = context;
     var retries = 3;
     while (retries > 0)
     {
         var s = string.Empty;
         try
         {
             retries--;
             s = "storage";
             storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["SigfoxDemoStorage"].ConnectionString);
             var blobClient = storageAccount.CreateCloudBlobClient();
             blobContainer = blobClient.GetContainerReference("device");
             blobContainer.SetPermissions(new BlobContainerPermissions() { PublicAccess = BlobContainerPublicAccessType.Off });
             blobContainer.CreateIfNotExists();
             s = "cache";
             cacheConnection = await ConnectionMultiplexer.ConnectAsync(ConfigurationManager.ConnectionStrings["SigfoxDemoCache"].ConnectionString);
             cacheDatabase = cacheConnection.GetDatabase();
             s = "database";
             sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["SigfoxDemoDatabase"].ConnectionString);
             sqlConnection.Open();
             sqlCommand = new SqlCommand("InsertMessage", sqlConnection) { CommandType = CommandType.StoredProcedure };
             sqlCommand.Parameters.Add(new SqlParameter("@Device", SqlDbType.VarChar));
             sqlCommand.Parameters.Add(new SqlParameter("@Data", SqlDbType.VarChar));
             sqlCommand.Parameters.Add(new SqlParameter("@Mode", SqlDbType.TinyInt));
             sqlCommand.Parameters.Add(new SqlParameter("@Periode", SqlDbType.TinyInt));
             sqlCommand.Parameters.Add(new SqlParameter("@FrameType", SqlDbType.TinyInt));
             sqlCommand.Parameters.Add(new SqlParameter("@Battery", SqlDbType.Float));
             sqlCommand.Parameters.Add(new SqlParameter("@Temperature", SqlDbType.Float));
             sqlCommand.Parameters.Add(new SqlParameter("@Humidity", SqlDbType.Float));
             sqlCommand.Parameters.Add(new SqlParameter("@ILS", SqlDbType.Bit));
             sqlCommand.Parameters.Add(new SqlParameter("@Light", SqlDbType.Float));
             sqlCommand.Parameters.Add(new SqlParameter("@Version", SqlDbType.VarChar));
             sqlCommand.Parameters.Add(new SqlParameter("@AlertCount", SqlDbType.Int));
             sqlCommand.Parameters.Add(new SqlParameter("@TimeStamp", SqlDbType.DateTime));
             sqlCommand.Parameters.Add(new SqlParameter("@Duplicate", SqlDbType.Bit));
             sqlCommand.Parameters.Add(new SqlParameter("@Signal", SqlDbType.Float));
             sqlCommand.Parameters.Add(new SqlParameter("@Station", SqlDbType.VarChar));
             sqlCommand.Parameters.Add(new SqlParameter("@AvgSignal", SqlDbType.Float));
             sqlCommand.Parameters.Add(new SqlParameter("@Latitude", SqlDbType.Float));
             sqlCommand.Parameters.Add(new SqlParameter("@Longitude", SqlDbType.Float));
             sqlCommand.Parameters.Add(new SqlParameter("@Rssi", SqlDbType.Float));
             sqlCommand.Parameters.Add(new SqlParameter("@SeqNumber", SqlDbType.Int));
             retries = 0;
         }
         catch (Exception e)
         {
             Console.Error.WriteLine("Error opening destination Event Hub: " + e.Message + "(" + s + ")");
             if (retries == 0)
                 throw;
         }
     }
     checkpointStopWatch = new Stopwatch();
     checkpointStopWatch.Start();
 }
Exemplo n.º 24
0
 public StackExchange.Redis.ConnectionMultiplexer GetClient()
 {
     StackExchange.Redis.ConfigurationOptions option = new StackExchange.Redis.ConfigurationOptions();
     option.EndPoints.Add(_redisOption.RedisIP, Int32.Parse(_redisOption.RedisPort));
     option.DefaultDatabase = 1;
     StackExchange.Redis.ConnectionMultiplexer conn = StackExchange.Redis.ConnectionMultiplexer.Connect(option);
     return(conn);
 }
Exemplo n.º 25
0
 public RedisClient(RedisConnectionManager connectionManager)
 {
     _connectionectionManager = connectionManager;
     if (_connection == null)
     {
         _connection = _connectionectionManager.GetConnection();
     }
 }
Exemplo n.º 26
0
 public PhysicalBridge(ServerEndPoint serverEndPoint, ConnectionType type)
 {
     this.serverEndPoint = serverEndPoint;
     this.connectionType = type;
     this.multiplexer = serverEndPoint.Multiplexer;
     this.Name = Format.ToString(serverEndPoint.EndPoint) + "/" + connectionType.ToString();
     this.completionManager = new CompletionManager(multiplexer, Name);
 }
Exemplo n.º 27
0
 public RedisConnectionFactory(ConnectionMultiplexer conn, ConfigurationOptions configuration, JsonSerializerSettings jsonSerializerSettings)
 {
     _configurationOptions = configuration;
     _jsonSerializerSettings = jsonSerializerSettings;
     _conn = conn;
     if (!_conn.IsConnected)
         _conn = ConnectionMultiplexer.Connect(_configurationOptions); //TO DO - add other config options
 }
Exemplo n.º 28
0
 public DataService(StackExchange.Redis.ConnectionMultiplexer redis, DistributedLockService distributedLockService, MongoClient mongoClient, string redisDownloadListKey, string mongoDownloadCollectionName)
 {
     this.redis = redis;
     this.distributedLockService      = distributedLockService;
     this.mongoClient                 = mongoClient;
     this.redisDownloadListKey        = redisDownloadListKey;
     this.mongoDownloadCollectionName = mongoDownloadCollectionName;
 }
 static RedisSessionStateStoreProvider()
 {
     ConnectionMultiplexer = ConnectionMultiplexer.Connect(new ConfigurationOptions
     {
         EndPoints = { ConfigurationManager.AppSettings["RedisServer"] },
         ConnectTimeout = 30 * 1000
     });
 }
Exemplo n.º 30
0
 public async Task ConnectAsync()
 {
     if (_connection == null)
     {
         _connection = await ConnectionMultiplexer.ConnectAsync(_options.Configuration);
         _cache = _connection.GetDatabase();
     }
 }
Exemplo n.º 31
0
 public void Connect()
 {
     if (_connection == null)
     {
         _connection = ConnectionMultiplexer.Connect(_options.Configuration);
         _cache = _connection.GetDatabase();
     }
 }
        public RedisOutputCacheStorageProvider(ShellSettings shellSettings, IRedisConnectionProvider redisConnectionProvider) {
            _shellSettings = shellSettings;
            _redisConnectionProvider = redisConnectionProvider;
            _connectionString = _redisConnectionProvider.GetConnectionString(ConnectionStringKey);
            _connectionMultiplexer = _redisConnectionProvider.GetConnection(_connectionString);

            Logger = NullLogger.Instance;
        }
Exemplo n.º 33
0
        static void CleanRedis()
        {
            connection = Redis.ConnectionMultiplexer.Connect("localhost,allowAdmin=true");

            server = connection.GetServer("localhost", 6379);
            server.FlushDatabase();

            db = connection.GetDatabase();
        }
Exemplo n.º 34
0
 /// <summary>
 /// Gets the servers.
 /// </summary>
 /// <param name="muxer">The muxer.</param>
 /// <returns>The list of servers.</returns>
 public IEnumerable <StackRedis.IServer> GetServers(StackRedis.ConnectionMultiplexer muxer)
 {
     EndPoint[] endpoints = muxer.GetEndPoints();
     foreach (var endpoint in endpoints)
     {
         var server = muxer.GetServer(endpoint);
         yield return(server);
     }
 }
Exemplo n.º 35
0
        static string getTokenDetails(string Key)
        {
            Redis.ConnectionMultiplexer connection = Redis.ConnectionMultiplexer.Connect("localhost,password=ZLDe3wse");
            Redis.IDatabase             db         = connection.GetDatabase();

            Redis.RedisValue Value = db.StringGet(Key);

            return(Value);
        }
        private void InitRedisDb()
        {
            if (_redisDb == null)
            {
                StackExchange.Redis.ConnectionMultiplexer
                    cm = ConnectionMultiplexer.Connect("127.0.0.1:6379");

                _redisDb = cm.GetDatabase(1);
            }
        }
        public async Task Init(IPEndPoint endpoint, System.Threading.CancellationToken cancel)
        {
            _connection = await StackExchange.Redis.ConnectionMultiplexer.ConnectAsync(endpoint.ToString()).ConfigureAwait(false);

            var db   = _connection.GetDatabase();
            var data = Enumerable.Range(0, 10).Select(i => (RedisValue)i.ToString("0000000000")).ToArray();

            db.SetAdd("testkeySER", data);
            db.SetAdd("testkeySER2", data);
            db.StringSet("testkeySER3", "hi this is a test");
        }
Exemplo n.º 38
0
        public string Post([FromBody] string value)
        {
            var id = Guid.NewGuid().ToString();

            try {
                StackExchange.Redis.ConnectionMultiplexer redis   = ConnectionMultiplexer.Connect("localhost");
                StackExchange.Redis.IDatabase             redisDb = redis.GetDatabase();
                this.SaveDataToRedis(redisDb, id, value);
                this.makeEvent(redis, id, value);
            } catch (Exception e) {
                Console.WriteLine(e.Message);
            }
            return(id);
        }
Exemplo n.º 39
0
        internal static void Run(StackExchange.Redis.ConnectionMultiplexer redis)
        {
            Redis = redis;
            var sub = Redis.GetSubscriber();

            Console.Clear();
            Refresh();

            while ((!Console.KeyAvailable) || (Console.ReadKey().Key != ConsoleKey.Escape))
            {
                Refresh();
                Thread.Sleep(1500);
            }
        }
Exemplo n.º 40
0
        internal static void Run(StackExchange.Redis.ConnectionMultiplexer redis)
        {
            Console.WriteLine("Hit <ESC> to shutdown this worker.");

            Redis = redis;
            while ((!Console.KeyAvailable) || (Console.ReadKey().Key != ConsoleKey.Escape))
            {
                var msg = Redis.GetDatabase().ListLeftPop(Tools.WORKQUEUE);
                if (!msg.IsNullOrEmpty)
                {
                    HandleClusterWork(msg);
                }
                else
                {
                    Thread.Sleep(0);
                }
            }
        }
Exemplo n.º 41
0
 private void UnexpectedResponse(Message message, RawResult result)
 {
     ConnectionMultiplexer.TraceWithoutContext("From " + GetType().Name, "Unexpected Response");
     ConnectionFail(message, ConnectionFailureType.ProtocolFailure, "Unexpected response to " + (message == null ? "n/a" : message.Command.ToString()) + ": " + result.ToString());
 }
 public ServerSelectionStrategy(ConnectionMultiplexer multiplexer)
 {
     this.multiplexer = multiplexer;
 }
Exemplo n.º 43
0
 bool ICompletable.TryComplete(bool isAsync) => ConnectionMultiplexer.TryCompleteHandler(handler, sender, this, isAsync);
Exemplo n.º 44
0
        internal static Exception Timeout(ConnectionMultiplexer mutiplexer, string baseErrorMessage, Message message, ServerEndPoint server, WriteResult?result = null)
        {
            List <Tuple <string, string> > data = new List <Tuple <string, string> > {
                Tuple.Create("Message", message.CommandAndKey)
            };
            var sb = new StringBuilder();

            if (!string.IsNullOrEmpty(baseErrorMessage))
            {
                sb.Append(baseErrorMessage);
                if (message != null)
                {
                    sb.Append(", command=").Append(message.Command); // no key here, note
                }
            }
            else
            {
                sb.Append("Timeout performing ").Append(message.Command).Append(" (").Append(Format.ToString(mutiplexer.TimeoutMilliseconds)).Append("ms)");
            }

            void add(string lk, string sk, string v)
            {
                if (v != null)
                {
                    if (lk != null)
                    {
                        data.Add(Tuple.Create(lk, v));
                    }
                    if (sk != null)
                    {
                        sb.Append(", ").Append(sk).Append(": ").Append(v);
                    }
                }
            }

            // Add timeout data, if we have it
            if (result == WriteResult.TimeoutBeforeWrite)
            {
                add("Timeout", "timeout", Format.ToString(mutiplexer.TimeoutMilliseconds));
                try
                {
#if DEBUG
                    if (message.QueuePosition >= 0)
                    {
                        add("QueuePosition", null, message.QueuePosition.ToString());                             // the position the item was when added to the queue
                    }
                    if ((int)message.ConnectionWriteState >= 0)
                    {
                        add("WriteState", null, message.ConnectionWriteState.ToString());                                         // what the physical was doing when it was added to the queue
                    }
#endif
                    if (message != null && message.TryGetPhysicalState(out var ws, out var rs, out var sentDelta, out var receivedDelta))
                    {
                        add("Write-State", null, ws.ToString());
                        add("Read-State", null, rs.ToString());
                        // these might not always be available
                        if (sentDelta >= 0)
                        {
                            add("OutboundDeltaKB", "outbound", $"{sentDelta >> 10}KiB");
                        }
                        if (receivedDelta >= 0)
                        {
                            add("InboundDeltaKB", "inbound", $"{receivedDelta >> 10}KiB");
                        }
                    }
                }
                catch { }
            }

            if (message != null)
            {
                message.TryGetHeadMessages(out var now, out var next);
                if (now != null)
                {
                    add("Message-Current", "active", mutiplexer.IncludeDetailInExceptions ? now.CommandAndKey : now.Command.ToString());
                }
                if (next != null)
                {
                    add("Message-Next", "next", mutiplexer.IncludeDetailInExceptions ? next.CommandAndKey : next.Command.ToString());
                }
            }

            // Add server data, if we have it
            if (server != null)
            {
                server.GetOutstandingCount(message.Command, out int inst, out int qs, out long @in, out int qu, out bool aw, out long toRead, out long toWrite, out var bs, out var rs, out var ws);
                add("OpsSinceLastHeartbeat", "inst", inst.ToString());
                add("Queue-Awaiting-Write", "qu", qu.ToString());
                add("Queue-Awaiting-Response", "qs", qs.ToString());
                add("Active-Writer", "aw", aw.ToString());
                if (qu != 0)
                {
                    add("Backlog-Writer", "bw", bs.ToString());
                }
                if (rs != PhysicalConnection.ReadStatus.NA)
                {
                    add("Read-State", "rs", rs.ToString());
                }
                if (ws != PhysicalConnection.WriteStatus.NA)
                {
                    add("Write-State", "ws", ws.ToString());
                }

                if (@in >= 0)
                {
                    add("Inbound-Bytes", "in", @in.ToString());
                }
                if (toRead >= 0)
                {
                    add("Inbound-Pipe-Bytes", "in-pipe", toRead.ToString());
                }
                if (toWrite >= 0)
                {
                    add("Outbound-Pipe-Bytes", "out-pipe", toWrite.ToString());
                }

                if (mutiplexer.StormLogThreshold >= 0 && qs >= mutiplexer.StormLogThreshold && Interlocked.CompareExchange(ref mutiplexer.haveStormLog, 1, 0) == 0)
                {
                    var log = server.GetStormLog(message.Command);
                    if (string.IsNullOrWhiteSpace(log))
                    {
                        Interlocked.Exchange(ref mutiplexer.haveStormLog, 0);
                    }
                    else
                    {
                        Interlocked.Exchange(ref mutiplexer.stormLogSnapshot, log);
                    }
                }
                add("Server-Endpoint", "serverEndpoint", server.EndPoint.ToString());
            }
            add("Manager", "mgr", mutiplexer.SocketManager?.GetState());

            add("Client-Name", "clientName", mutiplexer.ClientName);
            var hashSlot = message.GetHashSlot(mutiplexer.ServerSelectionStrategy);
            // only add keyslot if its a valid cluster key slot
            if (hashSlot != ServerSelectionStrategy.NoSlot)
            {
                add("Key-HashSlot", "PerfCounterHelperkeyHashSlot", message.GetHashSlot(mutiplexer.ServerSelectionStrategy).ToString());
            }
            int busyWorkerCount = PerfCounterHelper.GetThreadPoolStats(out string iocp, out string worker);
            add("ThreadPool-IO-Completion", "IOCP", iocp);
            add("ThreadPool-Workers", "WORKER", worker);
            data.Add(Tuple.Create("Busy-Workers", busyWorkerCount.ToString()));

            if (mutiplexer.IncludePerformanceCountersInExceptions)
            {
                add("Local-CPU", "Local-CPU", PerfCounterHelper.GetSystemCpuPercent());
            }

            add("Version", "v", GetLibVersion());

            sb.Append(" (Please take a look at this article for some common client-side issues that can cause timeouts: ");
            sb.Append(timeoutHelpLink);
            sb.Append(")");

            var ex = new RedisTimeoutException(sb.ToString(), message?.Status ?? CommandStatus.Unknown)
            {
                HelpLink = timeoutHelpLink
            };
            if (data != null)
            {
                var exData = ex.Data;
                foreach (var kv in data)
                {
                    exData["Redis-" + kv.Item1] = kv.Item2;
                }
            }

            if (mutiplexer.IncludeDetailInExceptions)
            {
                AddDetail(ex, message, server, null);
            }
            return(ex);
        }
        internal SocketToken BeginConnect(EndPoint endpoint, ISocketCallback callback, ConnectionMultiplexer multiplexer, TextWriter log)
        {
            var socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            SetFastLoopbackOption(socket);
            socket.NoDelay = true;
            try
            {
                CompletionType connectCompletionType = CompletionType.Any;
                this.ShouldForceConnectCompletionType(ref connectCompletionType);

                var formattedEndpoint = Format.ToString(endpoint);
                var tuple             = Tuple.Create(socket, callback);
                if (endpoint is DnsEndPoint)
                {
                    // A work-around for a Mono bug in BeginConnect(EndPoint endpoint, AsyncCallback callback, object state)
                    DnsEndPoint dnsEndpoint = (DnsEndPoint)endpoint;

#if CORE_CLR
                    multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
                    socket.ConnectAsync(dnsEndpoint.Host, dnsEndpoint.Port).ContinueWith(t =>
                    {
                        multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
                        EndConnectImpl(t, multiplexer, log, tuple);
                        multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint);
                    });
#else
                    CompletionTypeHelper.RunWithCompletionType(
                        (cb) => {
                        multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
                        return(socket.BeginConnect(dnsEndpoint.Host, dnsEndpoint.Port, cb, tuple));
                    },
                        (ar) => {
                        multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
                        EndConnectImpl(ar, multiplexer, log, tuple);
                        multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint);
                    },
                        connectCompletionType);
#endif
                }
                else
                {
#if CORE_CLR
                    multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
                    socket.ConnectAsync(endpoint).ContinueWith(t =>
                    {
                        multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
                        EndConnectImpl(t, multiplexer, log, tuple);
                    });
#else
                    CompletionTypeHelper.RunWithCompletionType(
                        (cb) => {
                        multiplexer.LogLocked(log, "BeginConnect: {0}", formattedEndpoint);
                        return(socket.BeginConnect(endpoint, cb, tuple));
                    },
                        (ar) => {
                        multiplexer.LogLocked(log, "EndConnect: {0}", formattedEndpoint);
                        EndConnectImpl(ar, multiplexer, log, tuple);
                        multiplexer.LogLocked(log, "Connect complete: {0}", formattedEndpoint);
                    },
                        connectCompletionType);
#endif
                }
            }
            catch (NotImplementedException ex)
            {
                if (!(endpoint is IPEndPoint))
                {
                    throw new InvalidOperationException("BeginConnect failed with NotImplementedException; consider using IP endpoints, or enable ResolveDns in the configuration", ex);
                }
                throw;
            }
            var token = new SocketToken(socket);
            return(token);
        }
        private void EndConnectImpl(IAsyncResult ar, ConnectionMultiplexer multiplexer, TextWriter log, Tuple <Socket, ISocketCallback> tuple)
        {
            try
            {
                bool ignoreConnect = false;
                ShouldIgnoreConnect(tuple.Item2, ref ignoreConnect);
                if (ignoreConnect)
                {
                    return;
                }
                var socket   = tuple.Item1;
                var callback = tuple.Item2;
#if CORE_CLR
                multiplexer.Wait((Task)ar); // make it explode if invalid (note: already complete at this point)
#else
                socket.EndConnect(ar);
#endif
                var netStream  = new NetworkStream(socket, false);
                var socketMode = callback == null ? SocketMode.Abort : callback.Connected(netStream, log);
                switch (socketMode)
                {
                case SocketMode.Poll:
                    multiplexer.LogLocked(log, "Starting poll");
                    OnAddRead(socket, callback);
                    break;

                case SocketMode.Async:
                    multiplexer.LogLocked(log, "Starting read");
                    try
                    { callback.StartReading(); }
                    catch (Exception ex)
                    {
                        ConnectionMultiplexer.TraceWithoutContext(ex.Message);
                        Shutdown(socket);
                    }
                    break;

                default:
                    ConnectionMultiplexer.TraceWithoutContext("Aborting socket");
                    Shutdown(socket);
                    break;
                }
            }
            catch (ObjectDisposedException)
            {
                multiplexer.LogLocked(log, "(socket shutdown)");
                if (tuple != null)
                {
                    try
                    { tuple.Item2.Error(); }
                    catch (Exception inner)
                    {
                        ConnectionMultiplexer.TraceWithoutContext(inner.Message);
                    }
                }
            }
            catch (Exception outer)
            {
                ConnectionMultiplexer.TraceWithoutContext(outer.Message);
                if (tuple != null)
                {
                    try
                    { tuple.Item2.Error(); }
                    catch (Exception inner)
                    {
                        ConnectionMultiplexer.TraceWithoutContext(inner.Message);
                    }
                }
            }
        }
Exemplo n.º 47
0
        private void CreateEvent(StackExchange.Redis.ConnectionMultiplexer redis, String id, string events, String value = null)
        {
            ISubscriber sub = redis.GetSubscriber();

            sub.Publish(events, id);
        }
Exemplo n.º 48
0
        private void ReadImpl()
        {
            List <IntPtr>          dead = null, active = new List <IntPtr>();
            List <ISocketCallback> activeCallbacks = new List <ISocketCallback>();

            IntPtr[] readSockets   = EmptyPointers, errorSockets = EmptyPointers;
            long     lastHeartbeat = Environment.TickCount;

            SocketPair[] allSocketPairs = null;
            while (true)
            {
                managerState = ManagerState.CheckForHeartbeat;
                active.Clear();
                activeCallbacks.Clear();
                if (dead != null)
                {
                    dead.Clear();
                }

                // this check is actually a pace-maker; sometimes the Timer callback stalls for
                // extended periods of time, which can cause socket disconnect
                long now = Environment.TickCount;
                if (unchecked (now - lastHeartbeat) >= 15000)
                {
                    managerState  = ManagerState.ExecuteHeartbeat;
                    lastHeartbeat = now;
                    lock (socketLookup)
                    {
                        if (allSocketPairs == null || allSocketPairs.Length != socketLookup.Count)
                        {
                            allSocketPairs = new SocketPair[socketLookup.Count];
                        }
                        socketLookup.Values.CopyTo(allSocketPairs, 0);
                    }
                    foreach (var pair in allSocketPairs)
                    {
                        var callback = pair.Callback;
                        if (callback != null)
                        {
                            try { callback.OnHeartbeat(); } catch { }
                        }
                    }
                }

                managerState = ManagerState.LocateActiveSockets;
                lock (socketLookup)
                {
                    if (isDisposed)
                    {
                        return;
                    }

                    if (socketLookup.Count == 0)
                    {
                        // if empty, give it a few seconds chance before exiting
                        managerState = ManagerState.NoSocketsPause;
                        Monitor.Wait(socketLookup, TimeSpan.FromSeconds(20));
                        if (socketLookup.Count == 0)
                        {
                            return;                          // nothing new came in, so exit
                        }
                    }
                    managerState = ManagerState.PrepareActiveSockets;
                    foreach (var pair in socketLookup)
                    {
                        var socket = pair.Value.Socket;
                        if (socket.Handle == pair.Key && socket.Connected)
                        {
                            if (pair.Value.Socket.Connected)
                            {
                                active.Add(pair.Key);
                                activeCallbacks.Add(pair.Value.Callback);
                            }
                            else
                            {
                                (dead ?? (dead = new List <IntPtr>())).Add(pair.Key);
                            }
                        }
                    }
                    if (dead != null && dead.Count != 0)
                    {
                        managerState = ManagerState.CullDeadSockets;
                        foreach (var socket in dead)
                        {
                            socketLookup.Remove(socket);
                        }
                    }
                }
                int pollingSockets = active.Count;
                if (pollingSockets == 0)
                {
                    // nobody had actual sockets; just sleep
                    managerState = ManagerState.NoActiveSocketsPause;
                    Thread.Sleep(10);
                    continue;
                }

                if (readSockets.Length < active.Count + 1)
                {
                    managerState = ManagerState.GrowingSocketArray;
                    ConnectionMultiplexer.TraceWithoutContext("Resizing socket array for " + active.Count + " sockets");
                    readSockets  = new IntPtr[active.Count + 6]; // leave so space for growth
                    errorSockets = new IntPtr[active.Count + 6];
                }
                managerState   = ManagerState.CopyingPointersForSelect;
                readSockets[0] = errorSockets[0] = (IntPtr)active.Count;
                active.CopyTo(readSockets, 1);
                active.CopyTo(errorSockets, 1);
                int ready;
                try
                {
                    var timeout = new TimeValue(1000);
                    managerState = ManagerState.ExecuteSelect;
                    ready        = select(0, readSockets, null, errorSockets, ref timeout);
                    managerState = ManagerState.ExecuteSelectComplete;
                    if (ready <= 0) // -ve typically means a socket was disposed just before; just retry
                    {
                        bool hasWorkToDo = false;
                        if (ready == 0)
                        {
                            managerState = ManagerState.CheckForStaleConnections;
                            foreach (var s in activeCallbacks)
                            {
                                if (s.IsDataAvailable)
                                {
                                    hasWorkToDo = true;
                                }
                                else
                                {
                                    s.CheckForStaleConnection();
                                }
                            }
                        }
                        else
                        {
                            lastErrorTicks = Environment.TickCount;
                        }
                        if (!hasWorkToDo)
                        {
                            continue;
                        }
                    }
                    ConnectionMultiplexer.TraceWithoutContext((int)readSockets[0] != 0, "Read sockets: " + (int)readSockets[0]);
                    ConnectionMultiplexer.TraceWithoutContext((int)errorSockets[0] != 0, "Error sockets: " + (int)errorSockets[0]);
                }
                catch (Exception ex)
                { // this typically means a socket was disposed just before; just retry
                    Trace.WriteLine(ex.Message);
                    continue;
                }

                bool haveWork   = false;
                int  queueCount = (int)readSockets[0];
                if (queueCount != 0)
                {
                    managerState = ManagerState.EnqueueRead;
                    lock (readQueue)
                    {
                        for (int i = 1; i <= queueCount; i++)
                        {
                            var callback = GetCallback(readSockets[i]);
                            if (callback != null)
                            {
                                readQueue.Enqueue(callback);
                                haveWork = true;
                            }
                        }
                    }
                }
                queueCount = (int)errorSockets[0];
                if (queueCount != 0)
                {
                    managerState = ManagerState.EnqueueError;
                    lock (errorQueue)
                    {
                        for (int i = 1; i <= queueCount; i++)
                        {
                            var callback = GetCallback(errorSockets[i]);
                            if (callback != null)
                            {
                                errorQueue.Enqueue(callback);
                                haveWork = true;
                            }
                        }
                    }
                }
                if (!haveWork)
                {
                    // edge case: select is returning 0, but data could still be available
                    managerState = ManagerState.EnqueueReadFallback;
                    lock (readQueue)
                    {
                        foreach (var callback in activeCallbacks)
                        {
                            if (callback.IsDataAvailable)
                            {
                                readQueue.Enqueue(callback);
                            }
                        }
                    }
                }


                if (ready >= 5) // number of sockets we should attempt to process by ourself before asking for help
                {
                    // seek help, work in parallel, then synchronize
                    var obj = new QueueDrainSyncLock(this);
                    lock (obj)
                    {
                        managerState = ManagerState.RequestAssistance;
                        ThreadPool.QueueUserWorkItem(HelpProcessItems, obj);
                        managerState = ManagerState.ProcessQueues;
                        ProcessItems(true);
                        if (!obj.Consume())
                        {   // then our worker arrived and picked up work; we need
                            // to let it finish; note that if it *didn't* get that far
                            // yet, the Consume() call will mean that it never tries
                            Monitor.Wait(obj);
                        }
                    }
                }
                else
                {
                    // just do it ourself
                    managerState = ManagerState.ProcessQueues;
                    ProcessItems(true);
                }
            }
        }
Exemplo n.º 49
0
        private void makeEvent(StackExchange.Redis.ConnectionMultiplexer redis, String id, String value)
        {
            ISubscriber sub = redis.GetSubscriber();

            sub.Publish("events", id);
        }
Exemplo n.º 50
0
 internal RedisServer(ConnectionMultiplexer multiplexer, ServerEndPoint server, object asyncState) : base(multiplexer, asyncState)
 {
     this.server = server ?? throw new ArgumentNullException(nameof(server));
 }
Exemplo n.º 51
0
 public async Task <ConnectionMultiplexer> Connect(string serverName, int port)
 {
     return(_RedisMux ?? (_RedisMux = await ConnectionMultiplexer.ConnectAsync($"{serverName}:{port}")));
 }
Exemplo n.º 52
0
        public async Task Init(IPEndPoint endpoint, System.Threading.CancellationToken cancel)
        {
            _connection = await StackExchange.Redis.ConnectionMultiplexer.ConnectAsync(endpoint.ToString()).ConfigureAwait(false);

            await _connection.GetDatabase().StringIncrementAsync("whatever").ConfigureAwait(false);
        }
Exemplo n.º 53
0
 // This is a hack required to get injection working
 // because Steeltoe's redis connector injected the concrete class as binding
 // and not the interface.
 public RedisLocationCache(ILogger <RedisLocationCache> logger,
                           StackExchange.Redis.ConnectionMultiplexer connectionMultiplexer) : this(logger, (IConnectionMultiplexer)connectionMultiplexer)
 {
 }