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

add region field to balancer protocol #1124

Merged
merged 1 commit into from
Nov 3, 2023
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
5 changes: 4 additions & 1 deletion crates/harness/src/monolith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,10 @@ impl Monolith {
loop {
let (stream, _) = _listener.accept().await.unwrap();
let mut ws = tokio_tungstenite::accept_async(stream).await.unwrap();
let init = M2BInit { port: http_port };
let init = M2BInit {
port: http_port,
region: "unknown".into(),
Copy link
Contributor

Choose a reason for hiding this comment

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

what does .into() do?

Copy link
Owner Author

Choose a reason for hiding this comment

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

It comes from the Into trait. https://doc.rust-lang.org/std/convert/trait.Into.html

You can read more in depth about From/Into here: https://practice.rs/type-conversions/from-into.html Specifically, this into() converts a &'static str into a String.

};
let msg = serde_json::to_string(&MsgM2B::Init(init)).unwrap();
ws.send(Message::Text(msg)).await.unwrap();
state.lock().unwrap().connected = true;
Expand Down
1 change: 1 addition & 0 deletions crates/ott-balancer-protocol/src/monolith.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ pub enum MsgM2B {
pub struct M2BInit {
/// The port that the monolith is listening for HTTP requests on.
pub port: u16,
pub region: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
Copy link
Contributor

Choose a reason for hiding this comment

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

line 99?

Copy link
Owner Author

Choose a reason for hiding this comment

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

derive invokes derive macros. They are macros that automatically implement traits for you. For example, #[derive(Clone)] will automatically implement Clone, and allow you to call clone() on the type that it's implemented for.

Expand Down
2 changes: 2 additions & 0 deletions server/balancer.ts
Copy link
Contributor

Choose a reason for hiding this comment

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

This file won't let me comment on individual lines so:
wss.on()?
this.emit() - emits to what?

Copy link
Owner Author

Choose a reason for hiding this comment

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

wss is a WebSocket.Server from the ws node js package.

on(event, handler) runs handler whenever event is emitted by the object.

this in that context is BalancerManager, which has an event bus that emits events from BalancerManagerEvemts (which ive just realized I've typod)

Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
balancerManager.addBalancerConnection(conn);
});
wss.on("error", error => {
log.error(`Balancer websocket error: ${error}`);

Check warning on line 42 in server/balancer.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Invalid type "Error" of template literal expression

Check warning on line 42 in server/balancer.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Invalid type "Error" of template literal expression
});
wss.on("listening", () => {
log.info(
Expand Down Expand Up @@ -75,6 +75,7 @@
type: "init",
payload: {
port: conf.get("port"),
region: conf.get("balancing.region"),
},
};
conn.send(init);
Expand All @@ -96,7 +97,7 @@
}

private onBalancerError(conn: BalancerConnection, error: WebSocket.ErrorEvent) {
log.error(`Error from balancer ${conn.id}: ${error}`);

Check warning on line 100 in server/balancer.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Invalid type "ErrorEvent" of template literal expression

Check warning on line 100 in server/balancer.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Invalid type "ErrorEvent" of template literal expression
this.emit("error", conn, error);
}

Expand Down Expand Up @@ -170,6 +171,7 @@
type: "init",
payload: {
port: conf.get("port"),
region: conf.get("balancing.region"),
},
};
this.send(init);
Expand All @@ -191,7 +193,7 @@
}
this.emit("message", result.value);
} else {
log.error(`Error parsing incoming balancer message: ${result.value} - ${data}`);

Check warning on line 196 in server/balancer.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Invalid type "Error" of template literal expression

Check warning on line 196 in server/balancer.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Invalid type "Data" of template literal expression

Check warning on line 196 in server/balancer.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Invalid type "Error" of template literal expression

Check warning on line 196 in server/balancer.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Invalid type "Data" of template literal expression
}
}

Expand Down Expand Up @@ -258,7 +260,7 @@
const result = await roommanager.getRoom(roomName, { mustAlreadyBeLoaded: true });
if (!result.ok) {
log.error(
`Failed to grab room that should have been loaded. Can't inform balancers. room=${roomName}: ${result.value}`

Check warning on line 263 in server/balancer.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Invalid type "RoomNotFoundException | RoomAlreadyLoadedException" of template literal expression

Check warning on line 263 in server/balancer.ts

View workflow job for this annotation

GitHub Actions / lint (18.x)

Invalid type "RoomNotFoundException | RoomAlreadyLoadedException" of template literal expression
);
return;
}
Expand Down
1 change: 1 addition & 0 deletions server/generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export interface B2MClientMsg<T = unknown> {
export interface M2BInit {
/** The port that the monolith is listening for HTTP requests on. */
port: number;
region: string;
}

export enum Visibility {
Expand Down
11 changes: 11 additions & 0 deletions server/ott-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,12 @@ export const conf = convict({
default: 3002,
env: "BALANCING_PORT",
},
region: {
doc: "The region that this server is in.",
format: String,
default: "unknown",
env: "BALANCING_REGION",
},
},
mail: {
enabled: {
Expand Down Expand Up @@ -513,6 +519,11 @@ function postProcessConfig(): void {
if (conf.get("mail.enabled")) {
validateMail();
Copy link
Contributor

Choose a reason for hiding this comment

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

what does mail mean in this context?

Copy link
Owner Author

Choose a reason for hiding this comment

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

email. used for password recovery emails.

}

if (process.env.FLY_REGION) {
log.info("Found FLY_REGION. Using it for balancing.region.");
conf.set("balancing.region", process.env.FLY_REGION);
}
}

function validateMail() {
Expand Down
Loading