Skip to content

Commit

Permalink
chore(zero-cache): rename dispatcher, remove unused host logic (#3368)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkgnotic authored Dec 16, 2024
1 parent 0b7d5d9 commit 6e56964
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 19 deletions.
5 changes: 2 additions & 3 deletions packages/zero-cache/src/server/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {resolver} from '@rocicorp/resolver';
import {availableParallelism} from 'node:os';
import path from 'node:path';
import {getZeroConfig} from '../config/zero-config.js';
import {Dispatcher, type Workers} from '../services/dispatcher/dispatcher.js';
import {SyncDispatcher} from '../services/dispatcher/sync-dispatcher.js';
import type {Service} from '../services/service.js';
import {initViewSyncerSchema} from '../services/view-syncer/schema/init.js';
import {pgClient} from '../types/pg.js';
Expand Down Expand Up @@ -141,8 +141,7 @@ export default async function runWorker(
const {port} = config;

if (numSyncers) {
const workers: Workers = {syncers};
mainServices.push(new Dispatcher(lc, parent, () => workers, {port}));
mainServices.push(new SyncDispatcher(lc, parent, syncers, {port}));
}

parent?.send(['ready', {ready: true}]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {expect, test} from 'vitest';
import {parseSyncPath} from './dispatcher.js';
import {parseSyncPath} from './sync-dispatcher.js';

test.each([
['/sync/v1/connect', {version: '1'}],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,22 @@ import {installWebSocketHandoff} from './websocket-handoff.js';
// servicing requests on the same domain as the application.
const CONNECT_URL_PATTERN = new UrlPattern('(/:base)/sync/v:version/connect');

export type Workers = {
syncers: Worker[];
};

export class Dispatcher extends HttpService {
export class SyncDispatcher extends HttpService {
readonly id = 'dispatcher';
readonly #workersByHostname: (hostname: string) => Workers;
readonly #syncers: Worker[];

constructor(
lc: LogContext,
parent: Worker | null,
workersByHostname: (hostname: string) => Workers,
syncers: Worker[],
opts: Options,
) {
super('dispatcher', lc, opts, fastify => {
fastify.get('/', (_req, res) => res.send('OK'));
installWebSocketHandoff(lc, req => this.#handoff(req), fastify.server);
});

this.#workersByHostname = workersByHostname;
this.#syncers = syncers;
if (parent) {
installWebSocketHandoff(lc, req => this.#handoff(req), parent);
}
Expand All @@ -51,16 +47,11 @@ export class Dispatcher extends HttpService {
if (error !== null) {
throw new Error(error);
}
const {host} = headers;
if (!host) {
throw new Error('Missing Host field');
}
const {clientGroupID} = params;
const {syncers} = this.#workersByHostname(host);
const syncer = h32(clientGroupID) % syncers.length;
const syncer = h32(clientGroupID) % this.#syncers.length;

this._lc.debug?.(`connecting ${clientGroupID} to syncer ${syncer}`);
return {payload: params, receiver: syncers[syncer]};
return {payload: params, receiver: this.#syncers[syncer]};
}
}

Expand Down

0 comments on commit 6e56964

Please sign in to comment.