Skip to content

Commit

Permalink
Merge pull request #1488 from cloudflare/jsnell/more-worker-h-cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jasnell authored Dec 15, 2023
2 parents 7d9204f + dfc1bd2 commit 030ca38
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 45 deletions.
1 change: 1 addition & 0 deletions src/workerd/api/global-scope.c++
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <workerd/api/util.h>
#include <workerd/util/stream-utils.h>
#include <workerd/util/use-perfetto-categories.h>
#include <workerd/util/uncaught-exception-source.h>

namespace workerd::api {

Expand Down
1 change: 1 addition & 0 deletions src/workerd/api/trace.c++
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <workerd/util/thread-scopes.h>
#include <workerd/util/own-util.h>
#include <workerd/util/uuid.h>
#include <workerd/util/uncaught-exception-source.h>

namespace workerd::api {

Expand Down
1 change: 1 addition & 0 deletions src/workerd/io/io-context.c++
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <kj/debug.h>
#include <workerd/jsg/jsg.h>
#include <workerd/util/sentry.h>
#include <workerd/util/uncaught-exception-source.h>
#include <map>

namespace workerd {
Expand Down
1 change: 1 addition & 0 deletions src/workerd/io/io-context.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include <workerd/io/limit-enforcer.h>
#include <workerd/io/io-channels.h>
#include <workerd/util/perfetto-tracing.h>
#include <workerd/util/uncaught-exception-source.h>

namespace capnp { class HttpOverCapnpFactory; }

Expand Down
1 change: 1 addition & 0 deletions src/workerd/io/worker-entrypoint.c++
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
#include <workerd/util/sentry.h>
#include <workerd/util/thread-scopes.h>
#include <workerd/util/use-perfetto-categories.h>
#include <workerd/util/uncaught-exception-source.h>
#include <kj/compat/http.h>

namespace workerd {
Expand Down
20 changes: 8 additions & 12 deletions src/workerd/io/worker.c++
Original file line number Diff line number Diff line change
Expand Up @@ -114,18 +114,6 @@ const capnp::JsonCodec& getCdpJsonCodec() {

// =======================================================================================

kj::StringPtr KJ_STRINGIFY(UncaughtExceptionSource value) {
switch (value) {
case UncaughtExceptionSource::INTERNAL: return "Uncaught"_kj;
case UncaughtExceptionSource::INTERNAL_ASYNC: return "Uncaught (in promise)"_kj;
case UncaughtExceptionSource::ASYNC_TASK: return "Uncaught (async)"_kj;
case UncaughtExceptionSource::REQUEST_HANDLER: return "Uncaught (in response)"_kj;
case UncaughtExceptionSource::TRACE_HANDLER: return "Uncaught (in trace)"_kj;
case UncaughtExceptionSource::ALARM_HANDLER: return "Uncaught (in alarm)"_kj;
};
KJ_UNREACHABLE;
}

namespace {

// Inform the inspector of an exception thrown.
Expand Down Expand Up @@ -3291,6 +3279,14 @@ kj::Maybe<uint16_t> Worker::Actor::getHibernationEventType() {
return impl->hibernationEventType;
}

kj::Own<Worker::Actor> Worker::Actor::addRef() {
KJ_IF_SOME(t, tracker) {
return kj::addRef(*this).attach(t.get()->startRequest());
} else {
return kj::addRef(*this);
}
}

// =======================================================================================

uint Worker::Isolate::getCurrentLoad() const {
Expand Down
47 changes: 14 additions & 33 deletions src/workerd/io/worker.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
#include <workerd/io/actor-cache.h> // because we can't forward-declare ActorCache::SharedLru.
#include <workerd/util/weak-refs.h>
#include <workerd/util/thread-scopes.h>
#include <workerd/util/uncaught-exception-source.h>

namespace v8 { class Isolate; }

Expand Down Expand Up @@ -74,7 +75,6 @@ class Worker: public kj::AtomicRefcounted {
class Script;
class Isolate;
class Api;
using ApiIsolate [[deprecated("Use Workerd::Api")]] = Api;

class ValidationErrorReporter {
public:
Expand Down Expand Up @@ -105,10 +105,11 @@ class Worker: public kj::AtomicRefcounted {
~Worker() noexcept(false);
KJ_DISALLOW_COPY_AND_MOVE(Worker);

const Script& getScript() const { return *script; }
const Isolate& getIsolate() const;
inline const Script& getScript() const { return *script; }

const WorkerObserver& getMetrics() const { return *metrics; }
inline const Isolate& getIsolate() const;

inline const WorkerObserver& getMetrics() const { return *metrics; }

class Lock;

Expand Down Expand Up @@ -180,9 +181,8 @@ class Worker::Script: public kj::AtomicRefcounted {
KJ_DISALLOW_COPY_AND_MOVE(Script);

inline kj::StringPtr getId() const { return id; }
const Isolate& getIsolate() const { return *isolate; }

bool isModular() const { return modular; }
inline const Isolate& getIsolate() const { return *isolate; }
inline bool isModular() const { return modular; }

struct CompiledGlobal {
jsg::V8Ref<v8::String> name;
Expand Down Expand Up @@ -264,7 +264,7 @@ class Worker::Isolate: public kj::AtomicRefcounted {
~Isolate() noexcept(false);
KJ_DISALLOW_COPY_AND_MOVE(Isolate);

const IsolateObserver& getMetrics() const { return *metrics; }
inline const IsolateObserver& getMetrics() const { return *metrics; }

inline kj::StringPtr getId() const { return id; }

Expand All @@ -274,10 +274,9 @@ class Worker::Isolate: public kj::AtomicRefcounted {
IsolateObserver::StartType startType, bool logNewScript = false,
kj::Maybe<ValidationErrorReporter&> errorReporter = kj::none) const;

const IsolateLimitEnforcer& getLimitEnforcer() const { return *limitEnforcer; }
inline const IsolateLimitEnforcer& getLimitEnforcer() const { return *limitEnforcer; }

const Api& getApi() const { return *api; }
[[deprecated("use getApi()")]] const Api& getApiIsolate() const { return *api; }
inline const Api& getApi() const { return *api; }

// Returns the number of threads currently blocked trying to lock this isolate's mutex (using
// takeAsyncLock()).
Expand Down Expand Up @@ -315,7 +314,7 @@ class Worker::Isolate: public kj::AtomicRefcounted {
kj::HttpHeaderId contentEncodingHeaderId,
RequestObserver& requestMetrics) const;

kj::Maybe<kj::StringPtr> getFeatureFlagsForFl() const {
inline kj::Maybe<kj::StringPtr> getFeatureFlagsForFl() const {
return featureFlagsForFl;
}

Expand Down Expand Up @@ -465,19 +464,6 @@ class Worker::Api {
}
};

enum class UncaughtExceptionSource {
INTERNAL,
INTERNAL_ASYNC,
// We catch, log, and rethrow some exceptions at these intermediate levels, in case higher-level
// handlers fail.

ASYNC_TASK,
REQUEST_HANDLER,
TRACE_HANDLER,
ALARM_HANDLER,
};
kj::StringPtr KJ_STRINGIFY(UncaughtExceptionSource value);

// A Worker may bounce between threads as it handles multiple requests, but can only actually
// execute on one thread at a time. Each thread must therefore lock the Worker while executing
// code.
Expand Down Expand Up @@ -726,7 +712,7 @@ class Worker::Actor final: public kj::Refcounted {
// Only needs to be called when allocating a HibernationManager!
kj::Maybe<uint16_t> getHibernationEventType();

const Worker& getWorker() { return *worker; }
inline const Worker& getWorker() { return *worker; }

void assertCanSetAlarm();
kj::Promise<void> makeAlarmTaskForPreview(kj::Date scheduledTime);
Expand All @@ -743,13 +729,8 @@ class Worker::Actor final: public kj::Refcounted {
// cancelling it).
kj::Promise<WorkerInterface::ScheduleAlarmResult> scheduleAlarm(kj::Date scheduledTime);

kj::Own<Worker::Actor> addRef() {
KJ_IF_SOME(t, tracker) {
return kj::addRef(*this).attach(t.get()->startRequest());
} else {
return kj::addRef(*this);
}
}
kj::Own<Worker::Actor> addRef();

private:
kj::Promise<WorkerInterface::ScheduleAlarmResult> handleAlarm(kj::Date scheduledTime);

Expand Down
31 changes: 31 additions & 0 deletions src/workerd/util/uncaught-exception-source.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <kj/string.h>

namespace workerd {

enum class UncaughtExceptionSource {
INTERNAL,
INTERNAL_ASYNC,
// We catch, log, and rethrow some exceptions at these intermediate levels, in case higher-level
// handlers fail.

ASYNC_TASK,
REQUEST_HANDLER,
TRACE_HANDLER,
ALARM_HANDLER,
};

inline kj::StringPtr KJ_STRINGIFY(UncaughtExceptionSource value) {
switch (value) {
case UncaughtExceptionSource::INTERNAL: return "Uncaught"_kj;
case UncaughtExceptionSource::INTERNAL_ASYNC: return "Uncaught (in promise)"_kj;
case UncaughtExceptionSource::ASYNC_TASK: return "Uncaught (async)"_kj;
case UncaughtExceptionSource::REQUEST_HANDLER: return "Uncaught (in response)"_kj;
case UncaughtExceptionSource::TRACE_HANDLER: return "Uncaught (in trace)"_kj;
case UncaughtExceptionSource::ALARM_HANDLER: return "Uncaught (in alarm)"_kj;
};
KJ_UNREACHABLE;
}

} // namespace workerd

0 comments on commit 030ca38

Please sign in to comment.