mirror of
https://github.com/outline/outline.git
synced 2026-11-22 05:51:43 +03:00
perf: Throttle lastActiveAt writes from collaboration edits (#14281)
* perf: Throttle lastActiveAt writes from collaboration edits to once every five minutes * Reuse User.updateActiveAt for collaboration activity
This commit is contained in:
1 parent
e471d95b40
commit
d035e0fdff
3 files changed
+53
-7
No files matched your search
@@ -0,0 +1,38 @@
|
||||
import type { onChangePayload } from "@hocuspocus/server";
|
||||
import { subMinutes } from "date-fns";
|
||||
import { User } from "@server/models";
|
||||
import { buildDocument, buildUser } from "@server/test/factories";
|
||||
import { ViewsExtension } from "./ViewsExtension";
|
||||
|
||||
describe("ViewsExtension", () => {
|
||||
const change = async (user: User, documentId: string, socketId: string) => {
|
||||
const extension = new ViewsExtension();
|
||||
await extension.onChange({
|
||||
documentName: `document.${documentId}`,
|
||||
context: { user },
|
||||
socketId,
|
||||
} as onChangePayload);
|
||||
};
|
||||
|
||||
it("should write lastActiveAt when it is older than five minutes", async () => {
|
||||
const old = subMinutes(new Date(), 10);
|
||||
const user = await buildUser({ lastActiveAt: old });
|
||||
const document = await buildDocument({ teamId: user.teamId });
|
||||
|
||||
await change(user, document.id, "socket-1");
|
||||
|
||||
const reloaded = await User.findByPk(user.id, { rejectOnEmpty: true });
|
||||
expect(reloaded.lastActiveAt!.getTime()).toBeGreaterThan(old.getTime());
|
||||
});
|
||||
|
||||
it("should not write lastActiveAt within five minutes", async () => {
|
||||
const recent = subMinutes(new Date(), 2);
|
||||
const user = await buildUser({ lastActiveAt: recent });
|
||||
const document = await buildDocument({ teamId: user.teamId });
|
||||
|
||||
await change(user, document.id, "socket-1");
|
||||
|
||||
const reloaded = await User.findByPk(user.id, { rejectOnEmpty: true });
|
||||
expect(reloaded.lastActiveAt).toEqual(recent);
|
||||
});
|
||||
});
|
||||
@@ -43,7 +43,7 @@ export class ViewsExtension implements Extension {
|
||||
);
|
||||
await Promise.all([
|
||||
View.touch(documentId, context.user.id, true),
|
||||
context.user.update({ lastActiveAt: new Date() }),
|
||||
context.user.updateActiveAt(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
+14
-6
@@ -574,23 +574,31 @@ class User extends ParanoidModel<
|
||||
);
|
||||
};
|
||||
|
||||
updateActiveAt = async (ctx: Context, force = false) => {
|
||||
const { ip } = ctx.request;
|
||||
/**
|
||||
* Updates the lastActiveAt timestamp, at most once every five minutes.
|
||||
*
|
||||
* @param ctx the request context, if any, used to record the IP and client.
|
||||
* @param force whether to update even if the last update was recent.
|
||||
* @returns a promise that resolves with the updated user.
|
||||
*/
|
||||
updateActiveAt = async (ctx?: Context, force = false) => {
|
||||
const fiveMinutesAgo = subMinutes(new Date(), 5);
|
||||
|
||||
// ensure this is updated only every few minutes otherwise
|
||||
// we'll be constantly writing to the DB as API requests happen
|
||||
if (!this.lastActiveAt || this.lastActiveAt < fiveMinutesAgo || force) {
|
||||
this.lastActiveAt = new Date();
|
||||
this.lastActiveIp = ip;
|
||||
if (ctx) {
|
||||
this.lastActiveIp = ctx.request.ip;
|
||||
}
|
||||
}
|
||||
|
||||
// Track the clients each user is using
|
||||
if (ctx.userAgent?.source.includes("Outline/")) {
|
||||
if (ctx?.userAgent?.source.includes("Outline/")) {
|
||||
this.setFlag(UserFlag.Desktop);
|
||||
} else if (ctx.userAgent?.isDesktop) {
|
||||
} else if (ctx?.userAgent?.isDesktop) {
|
||||
this.setFlag(UserFlag.DesktopWeb);
|
||||
} else if (ctx.userAgent?.isMobile) {
|
||||
} else if (ctx?.userAgent?.isMobile) {
|
||||
this.setFlag(UserFlag.MobileWeb);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user