AppServerBase.ConfigHotUpdate.cs 5.8 KB

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