Skip to content

MoLab Backend

Similar to a Gateway, the backend connects to clients and workers and forwards messages (jobs and results) between them. It is responsible for managing the WebSocket connections and the communication between the clients and the workers.

backend.molab_backend.main

ClientManager

Client manager class to manage client connections.

register(websocket) async

Register a new client.

Parameters:

Name Type Description Default
websocket WebSocket

The WebSocket connection for the client.

required

Returns:

Name Type Description
Connection Connection

The registered client instance.

unregister(client) async

Unregister an existing client.

Parameters:

Name Type Description Default
client Connection

The client instance to unregister.

required

Connection dataclass

Simple connection class to handle WebSocket connections.

Parameters:

Name Type Description Default
websocket WebSocket

The WebSocket connection for the worker or client.

required
id str

The unique identifier for the connection.

lambda: str(uuid4())()

WorkerManager

Worker manager class to manage worker connections using round-robin scheduling.

get_next_worker() async

Get the next available worker using round-robin scheduling.

Returns:

Name Type Description
Connection Connection

The next available worker instance.

register(websocket) async

Register a new worker.

Parameters:

Name Type Description Default
websocket WebSocket

The WebSocket connection for the worker.

required

Returns:

Name Type Description
Connection Connection

The registered worker instance.

unregister(worker) async

Unregister an existing worker.

Parameters:

Name Type Description Default
worker Connection

The worker instance to unregister.

required

handle_client_request(client, message) async

Handle client requests.

Parameters:

Name Type Description Default
client Connection

The client instance sending the request.

required
message dict

The message sent by the client.

required

main()

Main function to run the FastAPI app using Uvicorn.

register_client(websocket) async

WebSocket endpoint to register clients.

Parameters:

Name Type Description Default
websocket WebSocket

The WebSocket connection for the client.

required

register_worker(websocket) async

WebSocket endpoint to register workers.

Parameters:

Name Type Description Default
websocket WebSocket

The WebSocket connection for the worker.

required