public static void Main(string[] args) { // See https://support.microsoft.com/en-gb/help/821268/contention-poor-performance-and-deadlocks-when-you-make-calls-to-web-s // Experimentation shows we need the ThreadPool to always spin up threads for good performance under load ThreadPool.GetMaxThreads(out var workerThreads, out var ioThreads); ThreadPool.SetMinThreads(workerThreads, ioThreads); Parser.Default.ParseArguments <PartyServerCommandLineArgs>(args) .WithParsed(parsedArgs => { parsedArgs.Validate(); var spatialRefreshToken = Secrets.GetEnvSecret(SpatialRefreshTokenEnvironmentVariable); PartyDataModel.Defaults.MinMembers = (uint)parsedArgs.DefaultMinMembers; PartyDataModel.Defaults.MaxMembers = (uint)parsedArgs.DefaultMaxMembers; Log.Logger = new LoggerConfiguration() .WriteTo.Console(new RenderedCompactJsonFormatter()) .Enrich.FromLogContext() .CreateLogger(); using (var server = GrpcBaseServer.Build(parsedArgs)) using (var memoryStoreManager = new RedisClientManager(parsedArgs.RedisConnectionString)) { Log.Information($"Successfully connected to Redis at {parsedArgs.RedisConnectionString}"); server.AddInterceptor(new PlayerIdentityTokenValidatingInterceptor( PlayerAuthServiceClient.Create(credentials: new PlatformRefreshTokenCredential(spatialRefreshToken)), memoryStoreManager.GetRawClient(Database.CACHE) )) .AddInterceptor(new ExceptionMappingInterceptor(new Dictionary <Type, StatusCode> { { typeof(EntryNotFoundException), StatusCode.NotFound }, { typeof(EntryAlreadyExistsException), StatusCode.AlreadyExists }, { typeof(TransactionAbortedException), StatusCode.Unavailable } })); server.AddService( PartyService.BindService(new PartyServiceImpl(memoryStoreManager))); server.AddService( InviteService.BindService(new InviteServiceImpl(memoryStoreManager))); var serverTask = Task.Run(() => server.Start()); var signalTask = Task.Run(() => UnixSignal.WaitAny(new[] { new UnixSignal(Signum.SIGINT), new UnixSignal(Signum.SIGTERM) })); Task.WaitAny(serverTask, signalTask); if (signalTask.IsCompleted) { Log.Information($"Received UNIX signal {signalTask.Result}"); Log.Information("Server shutting down..."); server.Shutdown(); serverTask.Wait(); Log.Information("Server stopped cleanly"); } else { /* The server task has completed; we can just exit. */ Log.Information("The Party server has stopped itself or encountered an unhandled exception."); } } }); }
public HomeController(ICacheClient client, ISysModulesService modulesService, IHostingEnvironment environment, RedisClientManager redisClient) { _modulesService = modulesService; _environment = environment; _redisClient = redisClient; _client = client; }
public int GetSubModelCount <TModel>(string modelId) { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.GetHashCount(RedisKeyFactory.SubModelKey <TModel>(modelId))); } }
/// <summary> /// ONLY FOR REDIS VERSION DB /// /// Here there is a bug in ServiceStack.Redis, it will not refresh the info aftering reget a client from /// the pool, which means the number of commands will be not changed. /// /// The bug has been fixed in Version 4.0.58 (Commerical Version), our reference version is Version 3.9 (The last open source version). /// /// So here we have to dispose the redis client manager and reconnect with redis to get the lastest commands. /// </summary> /// <returns></returns> private long GetCurrentCommandCount() { RedisVersionDb redisVersionDb = this.versionDb as RedisVersionDb; if (redisVersionDb == null) { return(0); } RedisClientManager clientManager = redisVersionDb.RedisManager; clientManager.Dispose(); long commandCount = 0; for (int i = 0; i < clientManager.RedisInstanceCount; i++) { using (RedisClient redisClient = clientManager.GetLastestClient(0, 0)) { string countStr = redisClient.Info["total_commands_processed"]; long count = Convert.ToInt64(countStr); commandCount += count; } } return(commandCount); }
public bool DeleteEntryFromHash(string modelId, string subModelId) { using (var Redis = RedisClientManager.GetClient()) { return(Redis.RemoveEntryFromHash(modelId, subModelId)); } }
public void ClearServiceCache() { using (var Redis = RedisClientManager.GetClient()) { Redis.RemoveAll(Redis.SearchKeys("SVC:*")); } }
public void AddItemToQueue <T>(string queueId, T queueItem) { using (var Redis = RedisClientManager.GetClient()) { Redis.AddItemToList(RedisKeyFactory.QueueKey <T>(queueId), JsonConvert.SerializeObject(queueItem)); } }
public List <string> DiffSets(string fromSetId, params string[] setIds) { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.GetDifferencesFromSet(fromSetId, setIds).ToList()); } }
public List <string> GetAllItemsFromSet(string setId) { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.GetAllItemsFromSet(setId).ToList()); } }
public List <string> GetAllKeysFromHash(string modelId) { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.GetHashKeys(modelId)); } }
public void AddRangeToSet(string setId, List <string> items) { using (var Redis = RedisClientManager.GetClient()) { Redis.AddRangeToSet(setId, items); } }
public void FlushAll() { using (var Redis = RedisClientManager.GetClient()) { Redis.FlushAll(); } }
public bool IsActive <T>(string id) where T : IRedisModelBase { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.SortedSetContainsItem(RedisKeyFactory.ListAllKeys <T>(), GetKey <T>(id))); } }
public int IncrementValueInHash(string hashKey, string key, int incrementBy = 1) { using (var Redis = RedisClientManager.GetClient()) { return(Redis.IncrementValueInHash(hashKey, key, incrementBy)); } }
public virtual void Delete <T>(T model, bool IsRemoveSubModel = true) where T : IRedisModelBase { using (var Redis = RedisClientManager.GetClient()) { if (model != null) { string modelKey = GetKey <T>(model); Redis.Remove(modelKey); Redis.RemoveItemFromSortedSet(RedisKeyFactory.ListAllKeys <T>(), modelKey); Redis.IncrementValueBy(RedisKeyFactory.ListAllNumKeys <T>(), -1); if (GetAllCount <T>() == 0) { Redis.Remove(RedisKeyFactory.ListAllNumKeys <T>()); } BuildIndex <T>(model, true); if (IsRemoveSubModel) { Redis.Remove(RedisKeyFactory.SubModelKey <T>(model.Id)); } } } }
public long GetNextSequenceNum(string key) { using (var Redis = RedisClientManager.GetClient()) { return(Redis.IncrementValue(key)); } }
public int RemoveItemFromQueue <T>(string queueId, T queueItem) { using (var Redis = RedisClientManager.GetClient()) { return(Redis.RemoveItemFromList(RedisKeyFactory.QueueKey <T>(queueId), JsonConvert.SerializeObject(queueItem))); } }
public int GetLengthOfQueue <T>(string queueId) { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.GetListCount(RedisKeyFactory.QueueKey <T>(queueId))); } }
public bool IsExist(string key) { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.ContainsKey(key)); } }
public T Get <T>(string id) where T : IRedisModelBase { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.Get <T>(GetKey <T>(id))); } }
public bool IsExistInHash(string modelId, string subModelId) { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.HashContainsEntry(modelId, subModelId)); } }
public List <string> GetAllActiveModelIds <T>() where T : IRedisModelBase { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.GetRangeFromSortedSetDesc(RedisKeyFactory.ListAllKeys <T>(), 0, -1)); } }
public void AddItemToSet(string setId, string item) { using (var Redis = RedisClientManager.GetClient()) { Redis.AddItemToSet(setId, item); } }
public bool Remove(string key) { using (var Redis = RedisClientManager.GetClient()) { return(Redis.Remove(key)); } }
public bool SetContainsItem(string setId, string item) { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.SetContainsItem(setId, item)); } }
public override T Get <T>(string category, string key) { if (string.IsNullOrWhiteSpace(category)) { throw new ArgumentNullException("category"); } if (string.IsNullOrWhiteSpace(key)) { throw new ArgumentNullException("key"); } try { DataEntry retValue; using (var client = RedisClientManager.GetClient(_host, _port)) { var typedClient = client.As <DataEntry>(); retValue = typedClient.GetValue(GetKey(category, key)); } return(retValue == null ? default(T) : GetFormatter().FromFormatted <T>(retValue.ObjectValue)); } catch (RedisException) { var resolver = new DnsResolver(); resolver.RemoveCache(_host); throw; } }
public List <string> UnionSets(params string[] setIds) { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.GetUnionFromSets(setIds).ToList()); } }
public virtual string Add <T>(T model) where T : IRedisModel { using (var Redis = RedisClientManager.GetClient()) { if (model == null) { return(null); } if (string.IsNullOrWhiteSpace(model.Id)) { model.Id = NextId <T>().ToString(); } if (Get <T>(model.Id) != null) { return(null); } string modelKey = GetKey <T>(model); model.ModuleName = CurrentMode; Redis.Set <T>(modelKey, model); Redis.AddItemToSortedSet(RedisKeyFactory.ListAllKeys <T>(), modelKey, model.CreateDateTime.Ticks); Redis.IncrementValue(RedisKeyFactory.ListAllNumKeys <T>()); Redis.IncrementValue(RedisKeyFactory.NextKey <T>()); BuildIndex <T>(model); return(model.Id); } }
public List <string> KeyFuzzyFind(string generalKeyPattern) { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.SearchKeys(generalKeyPattern)); } }
public bool SetEntryInHash <T>(string modelId, string subModelId, T subModel) { using (var Redis = RedisClientManager.GetClient()) { return(Redis.SetEntryInHash(modelId, subModelId, JsonConvert.SerializeObject(subModel))); } }
public void TestInit() { RedisConnectionString = "localhost"; DataBaseNumber = 1; RedisManager = new RedisClientManager(); RedisManager.GetDatabase(RedisConnectionString, DataBaseNumber); }
public StudentManager(RedisClientManager redisManager) { RedisManager = redisManager; }