|
| 1 | +import 'dart:convert'; |
| 2 | +import 'dart:io'; |
| 3 | +import 'dart:isolate'; |
| 4 | + |
| 5 | +void main() { |
| 6 | + /// Retrieve the maximum number of isolates from the environment variables. |
| 7 | + final maxIsolatesFromEnvironment = Platform.environment['MAX_ISOLATES']; |
| 8 | + if (maxIsolatesFromEnvironment == null) { |
| 9 | + throw Exception('MAX_ISOLATES environment variable is not set.'); |
| 10 | + } |
| 11 | + |
| 12 | + /// Parse the isolate count; ensures we have a valid integer for scaling. |
| 13 | + final maxIsolates = int.tryParse(maxIsolatesFromEnvironment); |
| 14 | + if (maxIsolates == null) { |
| 15 | + throw Exception('MAX_ISOLATES must be a valid integer.'); |
| 16 | + } |
| 17 | + |
| 18 | + /// Retrieve the file system path for the Unix Domain Socket. |
| 19 | + final socketPath = Platform.environment['SOCKET_PATH']; |
| 20 | + if (socketPath == null) { |
| 21 | + throw Exception('SOCKET_PATH environment variable is not set.'); |
| 22 | + } |
| 23 | + |
| 24 | + /// Define the network address as a Unix Domain Socket (UDS). |
| 25 | + /// This is often faster than TCP for local inter-process communication. |
| 26 | + final address = InternetAddress(socketPath, type: InternetAddressType.unix); |
| 27 | + |
| 28 | + /// Create an [Isolate] containing an [HttpServer] |
| 29 | + /// for each processor after the first |
| 30 | + for (int i = 1; i < maxIsolates; i++) { |
| 31 | + Isolate.spawn<InternetAddress>(_startServer, address); |
| 32 | + } |
| 33 | + |
| 34 | + /// Create a [HttpServer] for the first processor |
| 35 | + _startServer(address); |
| 36 | +} |
| 37 | + |
| 38 | +/// Initializes and binds the [HttpServer] to the provided [address]. |
| 39 | +/// |
| 40 | +/// Setting [shared] to true allows multiple isolates to bind to the same |
| 41 | +/// address/port, enabling automatic load balancing by the OS kernel. |
| 42 | +void _startServer(InternetAddress address) async { |
| 43 | + final server = await HttpServer.bind(address, 0, shared: true); |
| 44 | + |
| 45 | + server |
| 46 | + ..defaultResponseHeaders.clear() |
| 47 | + /// Sets [HttpServer]'s [serverHeader]. |
| 48 | + ..serverHeader = 'dart_hybrid' |
| 49 | + /// Handles [HttpRequest]'s from [HttpServer]. |
| 50 | + ..listen(_handleRequest); |
| 51 | +} |
| 52 | + |
| 53 | +/// Dispatches requests to specific handlers. |
| 54 | +void _handleRequest(HttpRequest request) { |
| 55 | + switch (request.uri.path) { |
| 56 | + case '/json': |
| 57 | + _jsonTest(request); |
| 58 | + break; |
| 59 | + case '/plaintext': |
| 60 | + _plaintextTest(request); |
| 61 | + break; |
| 62 | + default: |
| 63 | + _sendResponse(request, HttpStatus.notFound); |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +/// Completes the given [request] by writing the [bytes] with the given |
| 68 | +/// [statusCode] and [type]. |
| 69 | +void _sendResponse( |
| 70 | + HttpRequest request, |
| 71 | + int statusCode, { |
| 72 | + ContentType? type, |
| 73 | + List<int> bytes = const [], |
| 74 | +}) => request.response |
| 75 | + ..statusCode = statusCode |
| 76 | + ..headers.contentType = type |
| 77 | + ..headers.date = DateTime.now() |
| 78 | + ..contentLength = bytes.length |
| 79 | + ..add(bytes) |
| 80 | + ..close(); |
| 81 | + |
| 82 | +/// Completes the given [request] by writing the [response] as JSON. |
| 83 | +void _sendJson(HttpRequest request, Object response) => _sendResponse( |
| 84 | + request, |
| 85 | + HttpStatus.ok, |
| 86 | + type: ContentType.json, |
| 87 | + bytes: JsonUtf8Encoder().convert(response), |
| 88 | +); |
| 89 | + |
| 90 | +/// Completes the given [request] by writing the [response] as plain text. |
| 91 | +void _sendText(HttpRequest request, String response) => _sendResponse( |
| 92 | + request, |
| 93 | + HttpStatus.ok, |
| 94 | + type: ContentType.text, |
| 95 | + bytes: utf8.encode(response), |
| 96 | +); |
| 97 | + |
| 98 | +/// Responds with the JSON test to the [request]. |
| 99 | +void _jsonTest(HttpRequest request) => _sendJson( |
| 100 | + request, |
| 101 | + const {'message': 'Hello, World!'}, |
| 102 | +); |
| 103 | + |
| 104 | +/// Responds with the plaintext test to the [request]. |
| 105 | +void _plaintextTest(HttpRequest request) => _sendText( |
| 106 | + request, |
| 107 | + 'Hello, World!', |
| 108 | +); |
0 commit comments