Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

expose preventEviction as unsafePreventEviction for Durable Objects #726

Merged
merged 6 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions packages/miniflare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ function getDurableObjectClassNames(
// Fallback to current worker service if name not defined
serviceName = workerServiceName,
unsafeUniqueKey,
unsafePreventEviction,
} = normaliseDurableObject(designator);
// Get or create `Map` mapping class name to optional unsafe unique key
let classNames = serviceClassNames.get(serviceName);
Expand All @@ -279,18 +280,18 @@ function getDurableObjectClassNames(
if (classNames.has(className)) {
// If we've already seen this class in this service, make sure the
// unsafe unique keys match
const existingUnsafeUniqueKey = classNames.get(className);
if (existingUnsafeUniqueKey !== unsafeUniqueKey) {
const existingInfo = classNames.get(className);
if (existingInfo?.unsafeUniqueKey !== unsafeUniqueKey) {
throw new MiniflareCoreError(
"ERR_DIFFERENT_UNIQUE_KEYS",
`Multiple unsafe unique keys defined for Durable Object "${className}" in "${serviceName}": ${JSON.stringify(
unsafeUniqueKey
)} and ${JSON.stringify(existingUnsafeUniqueKey)}`
)} and ${JSON.stringify(existingInfo?.unsafeUniqueKey)}`
);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a similar check for unsafePreventEviction? Since a Durable Object can have multiple bindings to it, and this configuration is defined at the binding level, but set in workerd with each Durable Object, we need to make sure unsafePreventEviction is configured the same on all bindings to the same Durable Object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added. Do you want a test for this behaviour? Can't see one for existingUnsafeUniqueKey

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not too worried about it, but if you're offering... 😃

} else {
// Otherwise, just add it
classNames.set(className, unsafeUniqueKey);
classNames.set(className, { unsafeUniqueKey, unsafePreventEviction });
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/miniflare/src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -424,14 +424,15 @@ export const CORE_PLUGIN: Plugin<
compatibilityFlags: options.compatibilityFlags,
bindings: workerBindings,
durableObjectNamespaces: classNamesEntries.map(
([className, unsafeUniqueKey]) => {
([className, { unsafeUniqueKey, unsafePreventEviction }]) => {
return {
className,
// This `uniqueKey` will (among other things) be used as part of the
// path when persisting to the file-system. `-` is invalid in
// JavaScript class names, but safe on filesystems (incl. Windows).
uniqueKey:
unsafeUniqueKey ?? `${options.name ?? ""}-${className}`,
preventEviction: unsafePreventEviction,
};
}
),
Expand Down
14 changes: 12 additions & 2 deletions packages/miniflare/src/plugins/do/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ export const DurableObjectsOptionsSchema = z.object({
// another `workerd` process, to ensure the IDs created by the stub
// object can be used by the real object too.
unsafeUniqueKey: z.string().optional(),
// Prevents the Durable Object being evicted.
unsafePreventEviction: z.boolean().optional(),
}),
])
)
Expand All @@ -35,15 +37,23 @@ export function normaliseDurableObject(
designator: NonNullable<
z.infer<typeof DurableObjectsOptionsSchema>["durableObjects"]
>[string]
): { className: string; serviceName?: string; unsafeUniqueKey?: string } {
): {
className: string;
serviceName?: string;
unsafeUniqueKey?: string;
unsafePreventEviction?: boolean;
} {
const isObject = typeof designator === "object";
const className = isObject ? designator.className : designator;
const serviceName =
isObject && designator.scriptName !== undefined
? getUserServiceName(designator.scriptName)
: undefined;
const unsafeUniqueKey = isObject ? designator.unsafeUniqueKey : undefined;
return { className, serviceName, unsafeUniqueKey };
const unsafePreventEviction = isObject
? designator.unsafePreventEviction
: undefined;
return { className, serviceName, unsafeUniqueKey, unsafePreventEviction };
}

export const DURABLE_OBJECTS_PLUGIN_NAME = "do";
Expand Down
8 changes: 7 additions & 1 deletion packages/miniflare/src/plugins/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,13 @@ export type Persistence = z.infer<typeof PersistenceSchema>;
// Maps **service** names to the Durable Object class names exported by them
export type DurableObjectClassNames = Map<
string,
Map</* className */ string, /* unsafeUniqueKey */ string | undefined>
Map<
/* className */ string,
{
unsafeUniqueKey: string | undefined;
unsafePreventEviction: boolean | undefined;
RamIdeas marked this conversation as resolved.
Show resolved Hide resolved
}
>
>;

// Maps queue names to the Worker that wishes to consume it. Note each queue
Expand Down