File
Blob: src/workerd/io/request-tracker.c++
| 1 | // Copyright (c) 2023 Cloudflare, Inc. |
| 2 | // Licensed under the Apache 2.0 license found in the LICENSE file or at: |
| 3 | // https://opensource.org/licenses/Apache-2.0 |
| 4 | |
| 5 | #include "request-tracker.h" |
| 6 | |
| 7 | namespace workerd { |
| 8 | |
| 9 | RequestTracker::RequestTracker(Hooks& hooksImpl): hooks(hooksImpl) {}; |
| 10 | |
| 11 | RequestTracker::~RequestTracker() noexcept(false) {} |
| 12 | |
| 13 | RequestTracker::ActiveRequest::ActiveRequest(kj::Badge<RequestTracker>, RequestTracker& parent) |
| 14 | : maybeParent(kj::addRef(parent)) { |
| 15 | parent.requestActive(); |
| 16 | } |
| 17 | RequestTracker::ActiveRequest::~ActiveRequest() noexcept(false) { |
| 18 | KJ_IF_SOME(p, maybeParent) { |
| 19 | p.get()->requestInactive(); |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | void RequestTracker::requestActive() { |
| 24 | if (activeRequests++ == 0) { |
| 25 | KJ_IF_SOME(h, hooks) { |
| 26 | h.active(); |
| 27 | } |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | void RequestTracker::requestInactive() { |
| 32 | KJ_IF_SOME(h, hooks) { |
| 33 | if (--activeRequests == 0) { |
| 34 | h.inactive(); |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | RequestTracker::ActiveRequest RequestTracker::startRequest() { |
| 40 | return ActiveRequest({}, *this); |
| 41 | } |
| 42 | |
| 43 | } // namespace workerd |