Skip to content
This repository was archived by the owner on Mar 24, 2026. It is now read-only.

Commit 4dfd42c

Browse files
committed
add update dart_hybrid implementation
1 parent 13cdf4d commit 4dfd42c

5 files changed

Lines changed: 183 additions & 2 deletions

File tree

frameworks/Dart/dart3/dart_aot/bin/server.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ void _startServer(List<String> args) async {
8585
server
8686
..defaultResponseHeaders.clear()
8787
/// Sets [HttpServer]'s [serverHeader].
88-
..serverHeader = 'dart'
88+
..serverHeader = 'dart_aot'
8989
/// Handles [HttpRequest]'s from [HttpServer].
9090
..listen(_handleRequest);
9191
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
2+
FROM dart:3.11.0 AS build
3+
WORKDIR /app
4+
5+
COPY pubspec.yaml .
6+
COPY dart_hybrid/bin/ bin/
7+
8+
RUN dart compile exe bin/server.dart -o server
9+
10+
FROM scratch
11+
COPY --from=build /runtime/ /
12+
COPY --from=build /app/server /bin/server
13+
14+
EXPOSE 8080
15+
ENTRYPOINT ["server"]
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
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+
);
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
x-dart-template: &dart-base
2+
build:
3+
context: .
4+
dockerfile: ./dart-hybrid/Dockerfile
5+
network_mode: host
6+
restart: always
7+
volumes:
8+
- socket-data:/var/run/dart-sockets
9+
10+
services:
11+
traefik:
12+
image: traefik:latest
13+
network_mode: host
14+
cpuset: "0-15"
15+
volumes:
16+
- ./traefik.yaml:/etc/traefik/traefik.yaml:ro
17+
- socket-data:/var/run/dart-sockets
18+
19+
dart-1:
20+
<<: *dart-base
21+
cpuset: "16-23"
22+
environment:
23+
- SOCKET_PATH=/var/run/dart-sockets/dart-1.sock
24+
- MAX_ISOLATES=8
25+
26+
dart-2:
27+
<<: *dart-base
28+
cpuset: "24-31"
29+
environment:
30+
- SOCKET_PATH=/var/run/dart-sockets/dart-2.sock
31+
- MAX_ISOLATES=8
32+
33+
dart-3:
34+
<<: *dart-base
35+
cpuset: "32-39"
36+
environment:
37+
- SOCKET_PATH=/var/run/dart-sockets/dart-3.sock
38+
- MAX_ISOLATES=8
39+
40+
dart-4:
41+
<<: *dart-base
42+
cpuset: "40-47"
43+
environment:
44+
- SOCKET_PATH=/var/run/dart-sockets/dart-4.sock
45+
- MAX_ISOLATES=8
46+
47+
dart-5:
48+
<<: *dart-base
49+
cpuset: "48-55"
50+
environment:
51+
- SOCKET_PATH=/var/run/dart-sockets/dart-5.sock
52+
- MAX_ISOLATES=8
53+
54+
volumes:
55+
socket-data:
56+
driver_opts:
57+
type: tmpfs
58+
device: tmpfs

frameworks/Dart/dart3/dart_native/bin/server.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ void _startServer(List<String> args) async {
3333
server
3434
..defaultResponseHeaders.clear()
3535
/// Sets [HttpServer]'s [serverHeader].
36-
..serverHeader = 'dart'
36+
..serverHeader = 'dart_native'
3737
/// Handles [HttpRequest]'s from [HttpServer].
3838
..listen(_handleRequest);
3939
}

0 commit comments

Comments
 (0)