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

Bugfix slow network load #461

Merged
merged 8 commits into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
102 changes: 91 additions & 11 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@
"next-intl": "3.1.4",
"next-themes": "^0.2.1",
"nodemailer": "^6.9.9",
"p-limit": "^6.1.0",
"pug": "^3.0.3",
"qrcode.react": "^3.1.0",
"react": "18.2.0",
Expand Down Expand Up @@ -105,4 +106,4 @@
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
}
}
}
3 changes: 1 addition & 2 deletions src/server/api/routers/networkRouter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ export const networkRouter = createTRPCRouter({
if (input.central) {
return await ztController.central_network_detail(ctx, input.nwid, input.central);
}

// First, retrieve the network with organization details
let networkFromDatabase = await ctx.prisma.network.findUnique({
where: {
Expand Down Expand Up @@ -185,7 +184,6 @@ export const networkRouter = createTRPCRouter({
}
// check if there is other network using same routes and return a notification
const targetIPs = ztControllerResponse.network.routes.map((route) => route.target);

interface DuplicateRoutes {
authorId: string;
routes: RoutesEntity[];
Expand Down Expand Up @@ -223,6 +221,7 @@ export const networkRouter = createTRPCRouter({

// Convert the map back to an array of merged members
const mergedMembers = [...mergedMembersMap.values()];

// Construct the final response object
return {
network: {
Expand Down
14 changes: 9 additions & 5 deletions src/server/api/services/memberService.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { UserContext } from "~/types/ctx";
import { MemberEntity, Peers } from "~/types/local/member";
import * as ztController from "~/utils/ztApi";
import { determineConnectionStatus } from "../utils/memberUtils";
import * as ztController from "~/utils/ztApi";
import { prisma } from "~/server/db";
import { sendWebhook } from "~/utils/webhook";
import { HookType, MemberJoined } from "~/types/webhooks";
Expand All @@ -21,27 +21,31 @@ export const syncMemberPeersAndStatus = async (
ztMembers: MemberEntity[],
) => {
if (ztMembers.length === 0) return [];
// get peers
const controllerPeers = await ztController.peers(ctx);

const updatedMembers = await Promise.all(
ztMembers.map(async (ztMember) => {
// TODO currently there is no way to distinguish peers by network id, so we have to fetch all peers
// this will make the node active in all networks it is part of if it is active in one of them.
// Should open a issue at ZeroTier
const peers = await ztController.peer(ctx, ztMember.address).catch(() => null);
const peers = controllerPeers.filter(
(peer) => peer.address === ztMember.address,
)[0];

// Retrieve the member from the database
const dbMember = await retrieveActiveMemberFromDatabase(nwid, ztMember.id);

// Find the active preferred path in the peers object
const activePreferredPath = findActivePreferredPeerPath(peers);

const { physicalAddress, ...restOfDbMembers } = dbMember || {};

// Merge the data from the database with the data from Controller
const updatedMember = {
...restOfDbMembers,
...ztMember,
physicalAddress: activePreferredPath?.address ?? physicalAddress,
peers,
peers: peers || {},
} as MemberEntity;

// Update the connection status
Expand Down Expand Up @@ -98,7 +102,7 @@ export const syncMemberPeersAndStatus = async (
* @param peers - The peers object containing paths.
* @returns The active preferred path, or undefined if not found.
*/
const findActivePreferredPeerPath = (peers: Peers) => {
const findActivePreferredPeerPath = (peers: Peers | null) => {
if (!peers || typeof peers !== "object" || !Array.isArray(peers.paths)) {
return null;
}
Expand Down
8 changes: 1 addition & 7 deletions src/types/local/member.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,22 +67,16 @@ interface CentralMemberConfig {
}

export interface Peers {
active: boolean;
address: string;
isBonded: boolean;
latency: number;
lastReceive: number;
lastSend: number;
localSocket?: number;
paths?: Paths[];
role: string;
version: string;
physicalAddress: string;
physicalAddress?: string;
versionMajor: number;
versionMinor: number;
versionRev: number;
preferred: boolean;
trustedPathId: number;
}

export interface Paths {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/ztApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -625,7 +625,7 @@ export const member_details = async (

// Get all peers
// https://docs.zerotier.com/service/v1/#operation/getPeers
export const peers = async (ctx: UserContext): Promise<ZTControllerGetPeer> => {
export const peers = async (ctx: UserContext): Promise<ZTControllerGetPeer[]> => {
// get headers based on local or central api
const { localControllerUrl } = await getOptions(ctx, false);

Expand All @@ -635,7 +635,7 @@ export const peers = async (ctx: UserContext): Promise<ZTControllerGetPeer> => {

try {
const response: AxiosResponse = await axios.get(addr, { headers });
return response.data as ZTControllerGetPeer;
return response.data as ZTControllerGetPeer[];
} catch (error) {
const message = `${error} (peers)`;
throw new APIError(message, error as AxiosError);
Expand Down
Loading