Skip to content
File

Blob: src/workerd/io/request-tracker.c++

1.0 KB
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 
7namespace workerd {
8 
9RequestTracker::RequestTracker(Hooks& hooksImpl): hooks(hooksImpl) {};
10 
11RequestTracker::~RequestTracker() noexcept(false) {}
12 
13RequestTracker::ActiveRequest::ActiveRequest(kj::Badge<RequestTracker>, RequestTracker& parent)
14 : maybeParent(kj::addRef(parent)) {
15 parent.requestActive();
16}
17RequestTracker::ActiveRequest::~ActiveRequest() noexcept(false) {
18 KJ_IF_SOME(p, maybeParent) {
19 p.get()->requestInactive();
20 }
21}
22 
23void RequestTracker::requestActive() {
24 if (activeRequests++ == 0) {
25 KJ_IF_SOME(h, hooks) {
26 h.active();
27 }
28 }
29}
30 
31void RequestTracker::requestInactive() {
32 KJ_IF_SOME(h, hooks) {
33 if (--activeRequests == 0) {
34 h.inactive();
35 }
36 }
37}
38 
39RequestTracker::ActiveRequest RequestTracker::startRequest() {
40 return ActiveRequest({}, *this);
41}
42 
43} // namespace workerd