public void Ctor_IncompatibleSerializer_ExceptionThrown()
        {
            // use a string serializer for int clients
            var settings = RedisSettings.Build()
                           .OverrideSerializer(new Mock <ISerializer <string> >().Object);

            // prevent Reset() from actually trying to connect to redis
            var clientMock = new Mock <RedisClient <int> >();

            clientMock.Setup(c => c.Reset()).Callback(() => { });

            Assert.Throws <ArgumentException>(() => clientMock.Object.ToString());
        }
Beispiel #2
0
        public Server()
        {
            string redisAddr = API.GetConvar("trackm_redis_addr", "127.0.0.1:6379");

            SplitHostPort(redisAddr, out string redisHost, out int redisPort);

            int redisDb = API.GetConvarInt("trackm_redis_db", 0);

            var settings = RedisSettings.Build()
                           .Host(redisHost)
                           .Port(redisPort)
                           .ReconnectOnIdle(true)
                           .ReissueCommandsOnReconnect(false);

            pool = new ThreadwisePool(settings, redisDb);

            IRedisClient <string> client = pool.GetClient();

            client.FlushDb();

            updateInterval = API.GetConvarInt("trackm_update_interval", 1000);
            if (updateInterval < MinimumUpdateInterval)
            {
                Debug.WriteLine("trackm_update_interval set too low ({0}ms) using minimum {1}ms.", updateInterval, MinimumUpdateInterval);
                updateInterval = MinimumUpdateInterval;
            }

            movementThreshold = API.GetConvarInt("trackm_movement_threshold", 1);
            if (movementThreshold < MinimumMovementThreshold)
            {
                Debug.WriteLine("trackm_movement_threshold set too low ({0}m) using minimum {1}m.", movementThreshold, MinimumMovementThreshold);
                movementThreshold = MinimumMovementThreshold;
            }
            // clients deal with squared distances
            movementThreshold *= movementThreshold;

            // fivem events
            EventHandlers["playerDropped"] += new Action <Player, string>(PlayerDropped);

            // internal events
            EventHandlers[Prefix + "register"]   += new Action <Player, int, string, string>(Register);
            EventHandlers[Prefix + "unregister"] += new Action <Player, int>(Unregister);

            // public events
            EventHandlers[Prefix + "Track"]          += new Action <int, int>(Track);
            EventHandlers[Prefix + "Untrack"]        += new Action <int, int>(Untrack);
            EventHandlers[Prefix + "MetadataGet"]    += new Action <int, int, string, CallbackDelegate>(MetadataGet);
            EventHandlers[Prefix + "MetadataSet"]    += new Action <int, int, string, string>(MetadataSet);
            EventHandlers[Prefix + "MetadataDelete"] += new Action <int, int, string>(MetadataDelete);
        }
Beispiel #3
0
        public static RedisClient getRedisClient()
        {
            var host     = ConfigurationManager.AppSettings["redisHost"];
            var port     = ConfigurationManager.AppSettings["redisPort"];
            var settings = RedisSettings.Build()
                           .Host(host)
                           .Port(Convert.ToInt32(port));

            var client = new RedisClient(settings);

            string dbIndex = ConfigurationManager.AppSettings["redisDB"];

            client.Select(Convert.ToInt32(dbIndex));

            return(client);
        }
Beispiel #4
0
        private WriterInfo createWriter(int bufferSize       = 1024,
                                        int?writerBufferSize = null)
        {
            var buffer = new byte[bufferSize];
            var stream = new MemoryStream(buffer, true);

            stream.SetLength(0);

            var writer = writerBufferSize.HasValue ?
                         new RedisWriter(stream, RedisSettings.Build()
                                         .WriteBufferSize(writerBufferSize.Value)) :
                         new RedisWriter(stream);

            writer.AutoFlush = true;
            return(new WriterInfo {
                Writer = writer,
                Stream = stream,
                Buffer = buffer,
                Mock = new Mock <ISerializer <string> >()
            });
        }
Beispiel #5
0
 public void Init()
 {
     Builder = RedisSettings.Build();
 }
Beispiel #6
0
        public void Run()
        {
            // configure benchmark parameters here:
            const int  Instances  = 7;
            const int  Iterations = 10000;
            const bool QuietMode  = false; // turn to false to make it wait for key inputs

            Func <Job> getJob = () => new PingJob();

            _settings = RedisSettings.Build()
                        .ReconnectOnIdle(false);

            _pool = new RoundRobinPool <string>(_settings, Instances);


            do
            {
                // setup
                var options = TaskCreationOptions.LongRunning;

                var tasks = Enumerable
                            .Range(0, Instances)
                            .Select(n => getJob())
                            .Select(job => new Task <BenchmarkResult>(
                                        () => benchMark(job, Iterations), options))
                            .ToArray();

                if (!QuietMode)
                {
                    Console.WriteLine("{0} instances of `{1}` each doing {2} iterations.",
                                      tasks.Count(),
                                      getJob().Description,
                                      Iterations);

                    Console.WriteLine("Hit any key to start.");
                    Console.ReadKey();
                }

                // starting
                if (Instances == 1)
                {
                    tasks.First().RunSynchronously();
                }
                else
                {
                    Array.ForEach(tasks, t => t.Start());
                }

                Task.WaitAll(tasks);

                // report time taken
                if (!QuietMode)
                {
                    Console.WriteLine("All done.");

                    var results = tasks.Select(t => t.Result);

                    foreach (var result in results)
                    {
                        Console.WriteLine("{0} : {1}ms",
                                          result.Job.Description,
                                          result.MillisecondsTaken);
                    }

                    var avg = results.Average(r => r.MillisecondsTaken);
                    var max = results.Max(r => r.MillisecondsTaken);
                    var min = results.Min(r => r.MillisecondsTaken);

                    Console.WriteLine("Maximum : {0}ms", max);
                    Console.WriteLine("Minimum : {0}ms", min);
                    Console.WriteLine("Average : {0}ms", avg);

                    Console.ReadKey();
                }
            } while (!QuietMode); // don't repeat in quietmode
        }
Beispiel #7
0
        public RedisConnectionFactory(RedisConnectionSettings connectionSettings)
        {
            var redisHost = string.IsNullOrEmpty(connectionSettings.Host)
                ? RedisConnectionSettings.DefaultHost
                : connectionSettings.Host;

            var redisPort = (connectionSettings.Port <= 0)
                ? RedisConnectionSettings.DefaultPort
                : connectionSettings.Port;

            var redisDatabase = (connectionSettings.Database < 0)
                ? RedisConnectionSettings.DefaultDatabase
                : connectionSettings.Database;

            var readBufferSize = (connectionSettings.ReadBufferSize <= 0)
                ? RedisConnectionSettings.DefaultReadBufferSize
                : connectionSettings.ReadBufferSize;

            var writeBufferSize = (connectionSettings.WriteBufferSize <= 0)
                ? RedisConnectionSettings.DefaultWriteBufferSize
                : connectionSettings.WriteBufferSize;

            var connectionTimeout = (connectionSettings.ConnectionTimeout < 0)
                ? RedisConnectionSettings.DefaultConnectionTimeout
                : connectionSettings.ConnectionTimeout;

            var maxReconnectRetries = (connectionSettings.MaxReconnectRetries <= 0)
                ? RedisConnectionSettings.DefaultMaxReconnectRetries
                : connectionSettings.MaxReconnectRetries;

            // Sider не работает, если PoolSize=1
            var poolSize = (connectionSettings.PoolSize <= 0)
                ? RedisConnectionSettings.DefaultPoolSize
                : Math.Max(connectionSettings.PoolSize, 2);

            var redisSettings = RedisSettings.Build()
                                .Host(redisHost)
                                .Port(redisPort)
                                .ReadBufferSize(readBufferSize)
                                .WriteBufferSize(writeBufferSize)
                                .ConnectionTimeout(connectionTimeout)
                                .MaxReconnectRetries(maxReconnectRetries)
                                .ReconnectOnIdle(true)
                                .ReissueCommandsOnReconnect(true)
                                .ReissuePipelinedCallsOnReconnect(true)
                                .OverrideEncoding(Encoding.UTF8);

            var redisPassword = connectionSettings.Password;

            if (!string.IsNullOrEmpty(redisPassword))
            {
                _clientInitializer += c => c.Auth(redisPassword);
            }

            if (poolSize > 0)
            {
                _clientsPool = new Lazy <IClientsPool <string> >(() => new RoundRobinPool <string>(redisSettings, poolSize));

                if (redisDatabase > 0)
                {
                    _clientInitializer += c => c.Select(redisDatabase);
                }
            }
            else
            {
                _clientsPool = new Lazy <IClientsPool <string> >(() => new ThreadwisePool <string>(redisSettings, (redisDatabase > 0) ? redisDatabase : default(int?)));
            }
        }