AppServerBase.ConfigHotUpdate.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.Specialized;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using Microsoft.Extensions.Logging;
  9. using SuperSocket.Common;
  10. using SuperSocket.SocketBase.Config;
  11. using SuperSocket.SocketBase.Protocol;
  12. namespace SuperSocket.SocketBase
  13. {
  14. interface IConfigValueChangeNotifier
  15. {
  16. bool Notify(string newValue);
  17. }
  18. class ConfigValueChangeNotifier : IConfigValueChangeNotifier
  19. {
  20. Func<string, bool> m_Handler;
  21. public ConfigValueChangeNotifier(Func<string, bool> handler)
  22. {
  23. m_Handler = handler;
  24. }
  25. public bool Notify(string newValue)
  26. {
  27. return m_Handler(newValue);
  28. }
  29. }
  30. class ConfigValueChangeNotifier<TConfigOption> : IConfigValueChangeNotifier
  31. where TConfigOption : ConfigurationElement, new()
  32. {
  33. Func<TConfigOption, bool> m_Handler;
  34. public ConfigValueChangeNotifier(Func<TConfigOption, bool> handler)
  35. {
  36. m_Handler = handler;
  37. }
  38. public bool Notify(string newValue)
  39. {
  40. if (string.IsNullOrEmpty(newValue))
  41. return m_Handler(default(TConfigOption));
  42. else
  43. return m_Handler(ConfigurationExtension.DeserializeChildConfig<TConfigOption>(newValue));
  44. }
  45. }
  46. public abstract partial class AppServerBase<TAppSession, TRequestInfo>
  47. where TRequestInfo : class, IRequestInfo
  48. where TAppSession : AppSession<TAppSession, TRequestInfo>, IAppSession, new()
  49. {
  50. private Dictionary<string, IConfigValueChangeNotifier> m_ConfigUpdatedNotifiers = new Dictionary<string, IConfigValueChangeNotifier>(StringComparer.OrdinalIgnoreCase);
  51. /// <summary>
  52. /// Registers the configuration option value handler, it is used for reading configuration value and reload it after the configuration is changed;
  53. /// </summary>
  54. /// <typeparam name="TConfigOption">The type of the configuration option.</typeparam>
  55. /// <param name="config">The server configuration.</param>
  56. /// <param name="name">The changed config option's name.</param>
  57. /// <param name="handler">The handler.</param>
  58. protected bool RegisterConfigHandler<TConfigOption>(IServerConfig config, string name, Func<TConfigOption, bool> handler)
  59. where TConfigOption : ConfigurationElement, new()
  60. {
  61. var notifier = new ConfigValueChangeNotifier<TConfigOption>(handler);
  62. m_ConfigUpdatedNotifiers.Add(name, notifier);
  63. return notifier.Notify(config.Options.GetValue(name));
  64. }
  65. /// <summary>
  66. /// Registers the configuration option value handler, it is used for reading configuration value and reload it after the configuration is changed;
  67. /// </summary>
  68. /// <param name="config">The server configuration.</param>
  69. /// <param name="name">The changed config option name.</param>
  70. /// <param name="handler">The handler.</param>
  71. protected bool RegisterConfigHandler(IServerConfig config, string name, Func<string, bool> handler)
  72. {
  73. var notifier = new ConfigValueChangeNotifier(handler);
  74. m_ConfigUpdatedNotifiers.Add(name, notifier);
  75. return notifier.Notify(config.OptionElements.GetValue(name));
  76. }
  77. int CheckConfigOptionsChange(NameValueCollection oldOptions, NameValueCollection newOptions)
  78. {
  79. var changed = 0;
  80. if (oldOptions == null && newOptions == null)
  81. return changed;
  82. var oldOptionsDict = oldOptions == null
  83. ? new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase)
  84. : Enumerable.Range(0, oldOptions.Count)
  85. .Select(i => new KeyValuePair<string, string>(oldOptions.GetKey(i), oldOptions.Get(i)))
  86. .ToDictionary(p => p.Key, p => p.Value, StringComparer.OrdinalIgnoreCase);
  87. foreach(var key in newOptions.AllKeys)
  88. {
  89. var newValue = newOptions[key];
  90. var oldValue = string.Empty;
  91. if (oldOptionsDict.TryGetValue(key, out oldValue))
  92. oldOptionsDict.Remove(key);
  93. if (string.Compare(newValue, oldValue) == 0)
  94. continue;
  95. NotifyConfigUpdated(key, newValue);
  96. changed++;
  97. }
  98. if (oldOptionsDict.Count > 0)
  99. {
  100. foreach (var p in oldOptionsDict)
  101. {
  102. NotifyConfigUpdated(p.Key, string.Empty);
  103. changed++;
  104. }
  105. }
  106. return changed;
  107. }
  108. private void NotifyConfigUpdated(string key, string newValue)
  109. {
  110. IConfigValueChangeNotifier notifier;
  111. if (!m_ConfigUpdatedNotifiers.TryGetValue(key, out notifier))
  112. return;
  113. try
  114. {
  115. if (!notifier.Notify(newValue))
  116. throw new Exception("returned false in the handling logic");
  117. }
  118. catch (Exception e)
  119. {
  120. Logger.LogError(e, e.Message);
  121. }
  122. }
  123. void IWorkItemBase.ReportPotentialConfigChange(IServerConfig config)
  124. {
  125. var oldConfig = this.Config;
  126. CheckConfigOptionsChange(oldConfig.Options, config.Options);
  127. CheckConfigOptionsChange(oldConfig.OptionElements, config.OptionElements);
  128. var updatableConfig = oldConfig as ServerConfig;
  129. if (updatableConfig == null)
  130. return;
  131. config.CopyPropertiesTo(p => p.GetCustomAttributes(typeof(HotUpdateAttribute), true).Length > 0, updatableConfig);
  132. }
  133. }
  134. }