File
Blob: src/workerd/io/actor-id.h
| 1 | #pragma once |
| 2 | |
| 3 | #include <kj/common.h> |
| 4 | #include <kj/string.h> |
| 5 | |
| 6 | namespace workerd { |
| 7 | |
| 8 | // Behavior mode for getting an actor |
| 9 | enum class ActorGetMode { |
| 10 | // Creates the actor if it does not already exist, otherwise gets the existing actor. |
| 11 | GET_OR_CREATE, |
| 12 | |
| 13 | // Get an already-created actor, throwing an error if it does not exist. |
| 14 | GET_EXISTING |
| 15 | }; |
| 16 | |
| 17 | // Routing mode for actor requests when replicas are available. |
| 18 | enum class ActorRoutingMode { |
| 19 | // Use default routing behavior (may route to replicas if available). |
| 20 | DEFAULT, |
| 21 | |
| 22 | // Always route to the primary, even if replicas exist. |
| 23 | PRIMARY_ONLY |
| 24 | }; |
| 25 | |
| 26 | // Version information for an actor. Used to specify cohort. |
| 27 | struct ActorVersion { |
| 28 | kj::Maybe<kj::String> cohort; |
| 29 | }; |
| 30 | |
| 31 | // An abstract class that implements generation of global actor IDs in a particular namespace. |
| 32 | // |
| 33 | // This is NOT at I/O type. Each global actor namespace binding holds one instance of this which |
| 34 | // it may call from any thread. |
| 35 | class ActorIdFactory { |
| 36 | public: |
| 37 | // Abstract actor ID. |
| 38 | // |
| 39 | // This is NOT an I/O type. An ActorId created in one IoContext can be used in other |
| 40 | // IoContexts. `ActorChannel` and `Actor`, however, are context-specific I/O types. It is |
| 41 | // expected that an ActorChannel's get() method can accept any ActorId generated for the same |
| 42 | // worker (by the IoChannelFactory for any IoContext), but will detect if the ID is not valid |
| 43 | // for the specific namespace. |
| 44 | class ActorId { |
| 45 | public: |
| 46 | // Get the string that could be passed to `idFromString()` to recreate this ID. |
| 47 | virtual kj::String toString() const = 0; |
| 48 | |
| 49 | // If the ActorId was created using `idFromName()`, return a copy of the name that was passed |
| 50 | // to it. Otherwise, returns null. |
| 51 | virtual kj::Maybe<kj::StringPtr> getName() const = 0; |
| 52 | |
| 53 | // Get the jurisdiction that was used when creating this ID. |
| 54 | virtual kj::Maybe<kj::StringPtr> getJurisdiction() const = 0; |
| 55 | |
| 56 | // Compare with another ID. |
| 57 | // |
| 58 | // This is allowed to assume the other ID was created by some other ActorIdFactory passed to |
| 59 | // one of the worker's other bindings, i.e. if all factories produce the same ID type, then |
| 60 | // this can downcast to that without a dynamic check. |
| 61 | virtual bool equals(const ActorId& other) const = 0; |
| 62 | |
| 63 | virtual kj::Own<ActorId> clone() const = 0; |
| 64 | }; |
| 65 | |
| 66 | virtual kj::Own<ActorId> newUniqueId(kj::Maybe<kj::StringPtr> jurisdiction) = 0; |
| 67 | virtual kj::Own<ActorId> idFromName(kj::String name) = 0; |
| 68 | virtual kj::Own<ActorId> idFromString(kj::String str) = 0; |
| 69 | virtual bool matchesJurisdiction(const ActorId& id) = 0; |
| 70 | virtual kj::Own<ActorIdFactory> cloneWithJurisdiction( |
| 71 | kj::Maybe<kj::StringPtr> maybeJurisdiction) = 0; |
| 72 | }; |
| 73 | |
| 74 | } // namespace workerd |