TimeLimitStrategy.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 OpenSim 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;
  28. using System.Collections.Generic;
  29. namespace OpenSim.Framework.Communications.Limit
  30. {
  31. /// <summary>
  32. /// Limit requests by discarding repeat attempts that occur within a given time period
  33. ///
  34. /// XXX Don't use this for limiting texture downloading, at least not until we better handle multiple requests
  35. /// for the same texture at different resolutions.
  36. /// </summary>
  37. public class TimeLimitStrategy<TId> : IRequestLimitStrategy<TId>
  38. {
  39. /// <summary>
  40. /// Record the time at which an asset request occurs.
  41. /// </summary>
  42. private readonly Dictionary<TId, Request> requests = new Dictionary<TId, Request>();
  43. /// <summary>
  44. /// The minimum time period between which requests for the same data will be serviced.
  45. /// </summary>
  46. private readonly TimeSpan m_repeatPeriod;
  47. public TimeSpan RepeatPeriod
  48. {
  49. get { return m_repeatPeriod; }
  50. }
  51. /// <summary></summary>
  52. /// <param name="repeatPeriod"></param>
  53. public TimeLimitStrategy(TimeSpan repeatPeriod)
  54. {
  55. m_repeatPeriod = repeatPeriod;
  56. }
  57. /// <summary>
  58. /// <see cref="IRequestLimitStrategy"/>
  59. /// </summary>
  60. public bool AllowRequest(TId id)
  61. {
  62. if (IsMonitoringRequests(id))
  63. {
  64. DateTime now = DateTime.Now;
  65. TimeSpan elapsed = now - requests[id].Time;
  66. if (elapsed < RepeatPeriod)
  67. {
  68. requests[id].Refusals += 1;
  69. return false;
  70. }
  71. requests[id].Time = now;
  72. }
  73. return true;
  74. }
  75. /// <summary>
  76. /// <see cref="IRequestLimitStrategy"/>
  77. /// </summary>
  78. public bool IsFirstRefusal(TId id)
  79. {
  80. if (IsMonitoringRequests(id))
  81. {
  82. if (1 == requests[id].Refusals)
  83. {
  84. return true;
  85. }
  86. }
  87. return false;
  88. }
  89. /// <summary>
  90. /// <see cref="IRequestLimitStrategy"/>
  91. /// </summary>
  92. public void MonitorRequests(TId id)
  93. {
  94. if (!IsMonitoringRequests(id))
  95. {
  96. requests.Add(id, new Request(DateTime.Now));
  97. }
  98. }
  99. /// <summary>
  100. /// <see cref="IRequestLimitStrategy"/>
  101. /// </summary>
  102. public bool IsMonitoringRequests(TId id)
  103. {
  104. return requests.ContainsKey(id);
  105. }
  106. }
  107. /// <summary>
  108. /// Private request details.
  109. /// </summary>
  110. class Request
  111. {
  112. /// <summary>
  113. /// Time of last request
  114. /// </summary>
  115. public DateTime Time;
  116. /// <summary>
  117. /// Number of refusals associated with this request
  118. /// </summary>
  119. public int Refusals;
  120. public Request(DateTime time)
  121. {
  122. Time = time;
  123. }
  124. }
  125. }