llcorehttppolicy.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /**
  2. * @file llcorehttppolicy.h
  3. * @brief Declarations for internal class enforcing policy decisions.
  4. *
  5. * $LicenseInfo:firstyear=2012&license=viewerlgpl$
  6. * Second Life Viewer Source Code
  7. * Copyright (C) 2012, Linden Research, Inc.
  8. *
  9. * This library is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU Lesser General Public
  11. * License as published by the Free Software Foundation;
  12. * version 2.1 of the License only.
  13. *
  14. * This library is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * Lesser General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Lesser General Public
  20. * License along with this library; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  22. *
  23. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  24. * $/LicenseInfo$
  25. */
  26. #ifndef _LLCORE_HTTP_POLICY_H_
  27. #define _LLCORE_HTTP_POLICY_H_
  28. #include <queue>
  29. #include "llcorehttpinternal.h"
  30. #include "llcorehttpoprequest.h"
  31. #include "llcorehttppolicyclass.h"
  32. #include "llcorehttppolicyglobal.h"
  33. #include "llcorehttprequest.h"
  34. #include "llcorehttpservice.h"
  35. #include "llerror.h"
  36. namespace LLCore
  37. {
  38. // HttpReadyQueue provides a simple priority queue for HttpOpRequest objects.
  39. //
  40. // This uses the priority_queue adaptor class to provide the queue as well as
  41. // the ordering scheme while allowing us access to the raw container if we
  42. // follow a few simple rules. One of the more important of those rules is that
  43. // any iterator becomes invalid on element erasure. So pay attention.
  44. //
  45. // Threading: not thread-safe. Expected to be used entirely by a single thread,
  46. // typically a worker thread of some sort.
  47. typedef std::deque<HttpOpRequest::ptr_t> HttpReadyQueueBase;
  48. class HttpReadyQueue final : public HttpReadyQueueBase
  49. {
  50. public:
  51. HttpReadyQueue() = default;
  52. HttpReadyQueue(const HttpReadyQueue&) = delete;
  53. void operator=(const HttpReadyQueue&) = delete;
  54. // Types and methods needed to make a std::deque look more like a
  55. // std::priority_queue, at least for our purposes.
  56. typedef HttpReadyQueueBase container_type;
  57. LL_INLINE const_reference top() const { return front(); }
  58. LL_INLINE void pop() { pop_front(); }
  59. LL_INLINE void push(const value_type& v) { emplace_back(v); }
  60. LL_INLINE const container_type& get_container() const { return *this; }
  61. LL_INLINE container_type& get_container() { return *this; }
  62. };
  63. // Implements class-based queuing policies for an HttpService instance.
  64. //
  65. // Threading: Single-threaded. Other than for construction/destruction, all
  66. // methods are expected to be invoked in a single thread, typically a worker
  67. // thread of some sort.
  68. class HttpPolicy
  69. {
  70. protected:
  71. LOG_CLASS(HttpPolicy);
  72. public:
  73. HttpPolicy(HttpService*);
  74. virtual ~HttpPolicy();
  75. HttpPolicy(const HttpPolicy&) = delete;
  76. void operator=(const HttpPolicy&) = delete;
  77. typedef std::shared_ptr<HttpOpRequest> opReqPtr_t;
  78. // Threading: called by init thread.
  79. HttpRequest::policy_t createPolicyClass();
  80. // Cancel all ready and retry requests sending them to their notification
  81. // queues. Release state resources making further request handling
  82. // impossible.
  83. //
  84. // Threading: called by worker thread
  85. void shutdown();
  86. // Deliver policy definitions and enable handling of requests. One-time
  87. // call invoked before starting the worker thread.
  88. //
  89. // Threading: called by application thread
  90. void start();
  91. // Give the policy layer some cycles to scan the ready queue promoting
  92. // higher-priority requests to active as permited.
  93. //
  94. // Returns an indication of how soon this method should be called again.
  95. //
  96. // Threading: called by worker thread
  97. int processReadyQueue();
  98. // Add request to a ready queue. Caller is expected to have provided us
  99. // with a reference count to hold the request (no additional references
  100. // will be added).
  101. //
  102. // OpRequest is owned by the request queue after this call and should not
  103. // be modified by anyone until retrieved from queue.
  104. //
  105. // Threading: called by worker thread
  106. void addOp(const opReqPtr_t& op);
  107. // Similar to addOp, used when a caller wants to retry a request that has
  108. // failed. It's placed on a special retry queue but ordered by retry time
  109. // not priority. Otherwise, handling is the same and retried operations are
  110. // considered before new ones but that doesn't guarantee completion order.
  111. //
  112. // Threading: called by worker thread
  113. void retryOp(const opReqPtr_t& op);
  114. // Attempt to change the priority of an earlier request.
  115. // Request that Shadows HttpService's method
  116. //
  117. // Attempts to cancel a previous request.
  118. // Shadows HttpService's method as well.
  119. //
  120. // Threading: called by worker thread
  121. bool cancel(HttpHandle handle);
  122. // When transport is finished with an op and takes it off the active queue,
  123. // it is delivered here for dispatch. Policy may send it back to the ready/
  124. // retry queues if it needs another go or we may finalize it and send it on
  125. // to the reply queue.
  126. //
  127. // Returns true of the request is still active or ready after staging,
  128. // false if has been sent on to the reply queue.
  129. //
  130. // Threading: called by worker thread
  131. bool stageAfterCompletion(const opReqPtr_t& op);
  132. // Get a reference to global policy options. Caller is expected to do
  133. // context checks like no setting once running.
  134. //
  135. // Threading: called by any thread *but* the object may only be modified by
  136. // the worker thread once running.
  137. //
  138. LL_INLINE HttpPolicyGlobal& getGlobalOptions()
  139. {
  140. return mGlobalOptions;
  141. }
  142. // Get a reference to class policy options. Caller is expected to do
  143. // context checks like no setting once running. These are done, for
  144. // example, in HttpService interfaces.
  145. //
  146. // Threading: called by any thread *but* the object may only be modified by
  147. // the worker thread once running and read accesses by other threads are
  148. // exposed to races at that point.
  149. HttpPolicyClass& getClassOptions(HttpRequest::policy_t pclass);
  150. // Get ready counts for a particular policy class
  151. //
  152. // Threading: called by worker thread
  153. int getReadyCount(HttpRequest::policy_t policy_class) const;
  154. // Stall (or unstall) a policy class preventing requests from transitioning
  155. // to an active state. Used to allow an HTTP request policy to empty prior
  156. // to changing settings or state that isn't tolerant of changes when work
  157. // is outstanding.
  158. //
  159. // Threading: called by worker thread
  160. bool stallPolicy(HttpRequest::policy_t policy_class, bool stall);
  161. protected:
  162. struct ClassState;
  163. typedef std::vector<ClassState*> class_list_t;
  164. HttpPolicyGlobal mGlobalOptions;
  165. // Naked pointer, not refcounted, not owner:
  166. HttpService* mService;
  167. class_list_t mClasses;
  168. };
  169. } // End namespace LLCore
  170. #endif // _LLCORE_HTTP_POLICY_H_