From fc4ff081f7e9047837b2173d499af40e98b36158 Mon Sep 17 00:00:00 2001 From: oneRain Date: Thu, 15 Aug 2019 15:11:38 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E6=8F=90=E5=87=BA=20AppRouter=20?= =?UTF-8?q?=E6=A8=A1=E5=9D=97=EF=BC=8C=E4=BB=A5=E5=A4=87=E5=90=8E=E9=9D=A2?= =?UTF-8?q?=E5=85=B1=E4=BA=AB=E4=BD=BF=E7=94=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Internal/AppRouter/AppRouterController.cs | 42 ++++++------- Storage/Storage.Test/AppRouterTest.cs | 16 +++++ Storage/Storage.Test/Storage.Test.csproj | 1 + .../Internal/Utilities/JsonExtensions.cs | 12 ++++ Storage/Storage/Public/AVClient.cs | 60 +------------------ 5 files changed, 52 insertions(+), 79 deletions(-) create mode 100644 Storage/Storage.Test/AppRouterTest.cs diff --git a/AppRouter/AppRouter/Internal/AppRouter/AppRouterController.cs b/AppRouter/AppRouter/Internal/AppRouter/AppRouterController.cs index e381954..d6525a7 100644 --- a/AppRouter/AppRouter/Internal/AppRouter/AppRouterController.cs +++ b/AppRouter/AppRouter/Internal/AppRouter/AppRouterController.cs @@ -7,36 +7,36 @@ using Newtonsoft.Json; namespace LeanCloud.Storage.Internal { public class AppRouterController { private AppRouterState currentState; - private readonly ReaderWriterLockSlim locker = new ReaderWriterLockSlim(); - public AppRouterState Get(string appId) { + private readonly SemaphoreSlim locker = new SemaphoreSlim(1); + + public async Task Get(string appId) { if (string.IsNullOrEmpty(appId)) { throw new ArgumentNullException(nameof(appId)); } - try { - locker.EnterUpgradeableReadLock(); - if (currentState != null && !currentState.IsExpired) { - return currentState; - } - // 从 AppRouter 获取服务器地址,只触发,不等待 - QueryAsync(appId).ContinueWith(t => { - if (t.IsFaulted) { + if (currentState != null && !currentState.IsExpired) { + return currentState; + } - } else { - locker.EnterWriteLock(); - currentState = t.Result; - currentState.Source = "router"; - locker.ExitWriteLock(); + await locker.WaitAsync(); + try { + if (currentState == null) { + try { + currentState = await QueryAsync(appId); + } catch (Exception) { + currentState = AppRouterState.GetFallbackServers(appId); } - }); - return AppRouterState.GetFallbackServers(appId); + } + return currentState; } finally { - locker.ExitUpgradeableReadLock(); + locker.Release(); } } public async Task QueryAsync(string appId) { + Console.WriteLine("QueryAsync"); + string url = string.Format("https://app-router.leancloud.cn/2/route?appId={0}", appId); HttpClient client = new HttpClient(); @@ -52,13 +52,13 @@ namespace LeanCloud.Storage.Internal { string content = await response.Content.ReadAsStringAsync(); response.Dispose(); - return JsonConvert.DeserializeObject(content); + AppRouterState state = JsonConvert.DeserializeObject(content); + state.Source = "router"; + return state; } public void Clear() { - locker.EnterWriteLock(); currentState = null; - locker.ExitWriteLock(); } } } diff --git a/Storage/Storage.Test/AppRouterTest.cs b/Storage/Storage.Test/AppRouterTest.cs new file mode 100644 index 0000000..9b499bd --- /dev/null +++ b/Storage/Storage.Test/AppRouterTest.cs @@ -0,0 +1,16 @@ +using NUnit.Framework; +using System.Threading.Tasks; +using LeanCloud.Storage.Internal; + +namespace LeanCloudTests { + public class AppRouterTest { + [Test] + public async Task GetServers() { + var appRouter = new AppRouterController(); + for (int i = 0; i < 1000; i++) { + var state = await appRouter.Get("BMYV4RKSTwo8WSqt8q9ezcWF-gzGzoHsz"); + TestContext.Out.WriteLine(state.ApiServer); + } + } + } +} diff --git a/Storage/Storage.Test/Storage.Test.csproj b/Storage/Storage.Test/Storage.Test.csproj index 0058401..50c6279 100644 --- a/Storage/Storage.Test/Storage.Test.csproj +++ b/Storage/Storage.Test/Storage.Test.csproj @@ -16,5 +16,6 @@ + diff --git a/Storage/Storage/Internal/Utilities/JsonExtensions.cs b/Storage/Storage/Internal/Utilities/JsonExtensions.cs index 1860133..43fe197 100644 --- a/Storage/Storage/Internal/Utilities/JsonExtensions.cs +++ b/Storage/Storage/Internal/Utilities/JsonExtensions.cs @@ -15,6 +15,12 @@ namespace LeanCloud.Storage.Internal { return str; } + public static Task SerializeAsync(object obj) { + return Task.Run(() => { + return JsonConvert.SerializeObject(obj); + }); + } + public static async Task DeserializeObjectAsync(string str, params JsonConverter[] converters) { T obj = default; await Task.Run(() => { @@ -22,5 +28,11 @@ namespace LeanCloud.Storage.Internal { }); return obj; } + + public static Task DeserializeAsync(string str, params JsonConverter[] converts) { + return Task.Run(() => { + return JsonConvert.DeserializeObject(str, converts); + }); + } } } diff --git a/Storage/Storage/Public/AVClient.cs b/Storage/Storage/Public/AVClient.cs index a2e07a5..0f9eb04 100644 --- a/Storage/Storage/Public/AVClient.cs +++ b/Storage/Storage/Public/AVClient.cs @@ -30,43 +30,6 @@ namespace LeanCloud { /// Represents the configuration of the LeanCloud SDK. /// public struct Configuration { - /// - /// 与 SDK 通讯的云端节点 - /// - public enum AVRegion { - /// - /// 默认值,LeanCloud 华北节点,同 Public_North_China - /// - [Obsolete("please use Configuration.AVRegion.Public_North_China")] - Public_CN = 0, - - /// - /// 默认值,华北公有云节点,同 Public_CN - /// - Public_North_China = 0, - - /// - /// LeanCloud 北美区公有云节点,同 Public_North_America - /// - [Obsolete("please use Configuration.AVRegion.Public_North_America")] - Public_US = 1, - /// - /// LeanCloud 北美区公有云节点,同 Public_US - /// - Public_North_America = 1, - - /// - /// 华东公有云节点,同 Public_East_China - /// - [Obsolete("please use Configuration.AVRegion.Public_East_China")] - Vendor_Tencent = 2, - - /// - /// 华东公有云节点,同 Vendor_Tencent - /// - Public_East_China = 2, - } - /// /// In the event that you would like to use the LeanCloud SDK /// from a completely portable project, with no platform-specific library required, @@ -100,17 +63,6 @@ namespace LeanCloud { /// public string ApplicationId { get; set; } - /// - /// LeanCloud C# SDK 支持的服务节点,目前支持华北,华东和北美公有云节点和私有节点,以及专属节点 - /// - public AVRegion Region { get; set; } - - internal int RegionValue { - get { - return (int)Region; - } - } - /// /// The LeanCloud application key for your app. /// @@ -255,13 +207,6 @@ namespace LeanCloud { internal static void Config(Configuration configuration) { lock (mutex) { - var nodeHash = configuration.ApplicationId.Split('-'); - if (nodeHash.Length > 1) { - if (nodeHash[1].Trim() == "9Nh9j0Va") { - configuration.Region = Configuration.AVRegion.Public_East_China; - } - } - CurrentConfiguration = configuration; } } @@ -280,11 +225,10 @@ namespace LeanCloud { Initialize(configuration); } - public static void Switch(string applicationId, string applicationKey, Configuration.AVRegion region = Configuration.AVRegion.Public_North_China) { + public static void Switch(string applicationId, string applicationKey) { var configuration = new Configuration { ApplicationId = applicationId, - ApplicationKey = applicationKey, - Region = region + ApplicationKey = applicationKey }; Switch(configuration); }