andrew4582

Search Host Manager v1

Jul 15th, 2015
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using NLog;
  7.  
  8. namespace Common
  9. {
  10.     /// <summary>
  11.     /// Provides server hosts for the search service
  12.     /// </summary>
  13.     public class SearchHostManager
  14.     {
  15.         static readonly Logger _logger = LogManager.GetCurrentClassLogger();
  16.  
  17.         readonly object syncRoot = new object();
  18.         readonly string[] _allhosts;
  19.         readonly List<string> _availableHosts;
  20.         readonly int _secondsToReset;
  21.         readonly int _searchTimeOut;
  22.         DateTime _lastHostReset = DateTime.Now;
  23.  
  24.         public SearchHostManager(string[] hosts, int timeout = 5000, int resetInSeconds = 60)
  25.         {
  26.             _allhosts = hosts.ToArray();
  27.             _availableHosts = new List<string>(_allhosts);
  28.             _searchTimeOut = timeout;
  29.             _secondsToReset = resetInSeconds;
  30.         }
  31.  
  32.         /// <summary>
  33.         /// Gets all hosts regardless if they are valid or not
  34.         /// </summary>
  35.         public string[] AllHosts
  36.         {
  37.             get { return _allhosts.ToArray(); }
  38.         }
  39.  
  40.         /// <summary>
  41.         /// Gets the number of seconds to reset failed hosts back to the original hosts. The default value is 60 seconds (1 minute).
  42.         /// </summary>
  43.         public int SecondsToReset
  44.         {
  45.             get { return _secondsToReset; }
  46.         }
  47.         /// <summary>
  48.         /// Gets the number of milliseconds to wait before the request times out. The default value is 5000 milliseconds (5 seconds).
  49.         /// </summary>
  50.         public int SearchTimeOut
  51.         {
  52.             get { return _searchTimeOut; }
  53.         }
  54.  
  55.         /// <summary>
  56.         /// Gets the number of maximum attempts that can be made with the hosts
  57.         /// </summary>
  58.         public int MaxSearchAttempts
  59.         {
  60.             get { return _allhosts.Length; }
  61.         }
  62.  
  63.         /// <summary>
  64.         /// Returns an array of available hosts that are valid
  65.         /// </summary>
  66.         public string[] GetAvailableHosts()
  67.         {
  68.             lock (syncRoot)
  69.             {
  70.                 if (DateTime.Now.Subtract(_lastHostReset) > TimeSpan.FromSeconds(_secondsToReset))
  71.                 {
  72.                     _lastHostReset = DateTime.Now;
  73.                     _availableHosts.Clear();
  74.                     _availableHosts.AddRange(_allhosts);
  75.                     _logger.Debug("Search hosts reset");
  76.                 }
  77.  
  78.                 var hosts = _availableHosts.ToArray();
  79.                 if (hosts.Length == 0)
  80.                 {
  81.                     _availableHosts.AddRange(_allhosts);
  82.                     hosts = _availableHosts.ToArray();
  83.                 }
  84.                 return hosts;
  85.             }
  86.         }
  87.  
  88.         /// <summary>
  89.         /// Removes a failed host from the available hosts
  90.         /// </summary>
  91.         public void RemoveHost(string host)
  92.         {
  93.             lock (syncRoot)
  94.                 _availableHosts.Remove(host);
  95.         }
  96.  
  97.         /// <summary>
  98.         /// Returns a random host from the available hosts
  99.         /// </summary>
  100.         public string GetNextHost(string currentHost = null)
  101.         {
  102.             var hosts = GetAvailableHosts().ToList();
  103.             if(!string.IsNullOrWhiteSpace(currentHost))
  104.                 hosts.Remove(currentHost);
  105.             return GetNextHost(hosts.ToArray());
  106.         }
  107.  
  108.         /// <summary>
  109.         /// Returns a random host from the available hosts
  110.         /// </summary>
  111.         public string GetNextHost(string[] hosts)
  112.         {
  113.             if (hosts.Length == 0)
  114.                 return null;
  115.             Random rnd = new Random(Math.Abs(Guid.NewGuid().ToString().GetHashCode()));
  116.             string h = hosts[rnd.Next(0, hosts.Length)];
  117.             return h;
  118.         }
  119.  
  120.         /// <summary>
  121.         /// Replaces the host and port (if non-standard) in the provided url
  122.         /// </summary>
  123.         public string ReplaceHostInUrl(string url, string host)
  124.         {
  125.             string h = host;
  126.             int port = url.StartsWith("https") ? 443 : 80;
  127.  
  128.             if (host.IndexOf(":") > 0)
  129.             {
  130.                 var parts = host.Split(":".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
  131.                 if (parts.Length == 2)
  132.                 {
  133.                     h = parts[0];
  134.                     int.TryParse(parts[1], out port);
  135.                 }
  136.             }
  137.  
  138.             UriBuilder ubuilder = new UriBuilder(url);
  139.             ubuilder.Host = h;
  140.             if (port > 0)
  141.                 ubuilder.Port = port;
  142.             return ubuilder.Uri.ToString();
  143.         }
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment