Skip to content

Commit 5d1064b

Browse files
committed
Merge remote-tracking branch 'origin/main' into optimize-write-checkpoints
2 parents e4d61d4 + 2cb5252 commit 5d1064b

File tree

4 files changed

+33
-11
lines changed

4 files changed

+33
-11
lines changed

.changeset/warm-coins-wave.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@powersync/service-core': patch
3+
---
4+
5+
Minor improvement to the HTTP liveness probe. The liveness probe will return an HTTP `200` response code when running in the `API` mode. Any HTTP response in the `API` mode indicates the service is running. When running in the `UNIFIED` mode the HTTP response code is `200` if the last `touched_at` timestamp value is less than 10 seconds ago - this indicates the replication worker is running.

packages/service-core/src/routes/endpoints/probes.ts

+18-2
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,27 @@ export const startupCheck = routeDefinition({
2525
export const livenessCheck = routeDefinition({
2626
path: ProbeRoutes.LIVENESS,
2727
method: router.HTTPMethod.GET,
28-
handler: async () => {
28+
handler: async (params) => {
2929
const state = container.probes.state();
3030

31+
/**
32+
* The HTTP probes currently only function in the API and UNIFIED
33+
* modes.
34+
*
35+
* For the API mode, we don't really touch the state, but any response from
36+
* the request indicates the service is alive.
37+
*
38+
* For the UNIFIED mode we update the touched_at time while the Replicator engine is running.
39+
* If the replication engine is present and the timeDifference from the last
40+
* touched_at is large, we report that the service is not live.
41+
*
42+
* This is only an incremental improvement. In future these values should be configurable.
43+
*/
44+
45+
const isAPIOnly = !params.context.service_context.replicationEngine;
3146
const timeDifference = Date.now() - state.touched_at.getTime();
32-
const status = timeDifference < 10000 ? 200 : 400;
47+
48+
const status = isAPIOnly ? 200 : timeDifference < 10000 ? 200 : 400;
3349

3450
return new router.RouterResponse({
3551
status,

packages/service-core/test/src/routes/probes.integration.test.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
2-
import Fastify, { FastifyInstance } from 'fastify';
31
import { container } from '@powersync/lib-services-framework';
4-
import * as auth from '../../../src/routes/auth.js';
5-
import * as system from '../../../src/system/system-index.js';
2+
import Fastify, { FastifyInstance } from 'fastify';
3+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
64
import { configureFastifyServer } from '../../../src/index.js';
5+
import * as auth from '../../../src/routes/auth.js';
76
import { ProbeRoutes } from '../../../src/routes/endpoints/probes.js';
7+
import * as system from '../../../src/system/system-index.js';
88

99
vi.mock('@powersync/lib-services-framework', async () => {
1010
const actual = (await vi.importActual('@powersync/lib-services-framework')) as any;
@@ -25,7 +25,7 @@ describe('Probe Routes Integration', () => {
2525

2626
beforeEach(async () => {
2727
app = Fastify();
28-
mockSystem = { routerEngine: {} } as system.ServiceContext;
28+
mockSystem = { routerEngine: {}, replicationEngine: {} } as system.ServiceContext;
2929
await configureFastifyServer(app, { service_context: mockSystem });
3030
await app.ready();
3131
});

packages/service-core/test/src/routes/probes.test.ts

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { describe, it, expect, vi, beforeEach } from 'vitest';
21
import { container } from '@powersync/lib-services-framework';
3-
import { startupCheck, livenessCheck, readinessCheck } from '../../../src/routes/endpoints/probes.js';
2+
import { beforeEach, describe, expect, it, vi } from 'vitest';
3+
import { livenessCheck, readinessCheck, startupCheck } from '../../../src/routes/endpoints/probes.js';
44

55
// Mock the container
66
vi.mock('@powersync/lib-services-framework', () => ({
@@ -83,6 +83,7 @@ describe('Probe Routes', () => {
8383
});
8484

8585
describe('livenessCheck', () => {
86+
const mockedContext = { context: { service_context: { replicationEngine: {} } } } as any;
8687
it('has the correct route definitions', () => {
8788
expect(livenessCheck.path).toBe('/probes/liveness');
8889
expect(livenessCheck.method).toBe('GET');
@@ -97,7 +98,7 @@ describe('Probe Routes', () => {
9798

9899
vi.mocked(container.probes.state).mockReturnValue(mockState);
99100

100-
const response = await livenessCheck.handler();
101+
const response = await livenessCheck.handler(mockedContext);
101102

102103
expect(response.status).toBe(200);
103104
expect(response.data).toEqual(mockState);
@@ -112,7 +113,7 @@ describe('Probe Routes', () => {
112113

113114
vi.mocked(container.probes.state).mockReturnValue(mockState);
114115

115-
const response = await livenessCheck.handler();
116+
const response = await livenessCheck.handler(mockedContext);
116117

117118
expect(response.status).toBe(400);
118119
expect(response.data).toEqual(mockState);

0 commit comments

Comments
 (0)