RepeatLimitStrategy.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSimulator Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System.Collections.Generic;
  28. namespace OpenSim.Framework.Communications.Limit
  29. {
  30. /// <summary>
  31. /// Limit requests by discarding them after they've been repeated a certain number of times.
  32. /// </summary>
  33. public class RepeatLimitStrategy<TId> : IRequestLimitStrategy<TId>
  34. {
  35. /// <summary>
  36. /// Record each asset request that we're notified about.
  37. /// </summary>
  38. private readonly Dictionary<TId, int> requestCounts = new Dictionary<TId, int>();
  39. /// <summary>
  40. /// The maximum number of requests that can be made before we drop subsequent requests.
  41. /// </summary>
  42. private readonly int m_maxRequests;
  43. public int MaxRequests
  44. {
  45. get { return m_maxRequests; }
  46. }
  47. /// <summary></summary>
  48. /// <param name="maxRequests">The maximum number of requests that may be served before all further
  49. /// requests are dropped.</param>
  50. public RepeatLimitStrategy(int maxRequests)
  51. {
  52. m_maxRequests = maxRequests;
  53. }
  54. /// <summary>
  55. /// <see cref="IRequestLimitStrategy"/>
  56. /// </summary>
  57. public bool AllowRequest(TId id)
  58. {
  59. if (requestCounts.ContainsKey(id))
  60. {
  61. requestCounts[id] += 1;
  62. if (requestCounts[id] > m_maxRequests)
  63. {
  64. return false;
  65. }
  66. }
  67. return true;
  68. }
  69. /// <summary>
  70. /// <see cref="IRequestLimitStrategy"/>
  71. /// </summary>
  72. public bool IsFirstRefusal(TId id)
  73. {
  74. if (requestCounts.ContainsKey(id) && m_maxRequests + 1 == requestCounts[id])
  75. {
  76. return true;
  77. }
  78. return false;
  79. }
  80. /// <summary>
  81. /// <see cref="IRequestLimitStrategy"/>
  82. /// </summary>
  83. public void MonitorRequests(TId id)
  84. {
  85. if (!IsMonitoringRequests(id))
  86. {
  87. requestCounts.Add(id, 1);
  88. }
  89. }
  90. /// <summary>
  91. /// <see cref="IRequestLimitStrategy"/>
  92. /// </summary>
  93. public bool IsMonitoringRequests(TId id)
  94. {
  95. return requestCounts.ContainsKey(id);
  96. }
  97. }
  98. }