-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from utopia-dart/fix-close
Improve threading and isolate handling
- Loading branch information
Showing
8 changed files
with
176 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import 'dart:developer' as dev; | ||
import 'dart:isolate'; | ||
|
||
import 'isolate_message.dart'; | ||
import 'isolate_supervisor.dart'; | ||
|
||
Future<void> entrypoint(IsolateMessage message) async { | ||
final ReceivePort receivePort = ReceivePort(); | ||
await message.server.start( | ||
message.handler, | ||
path: message.path, | ||
context: message.context, | ||
); | ||
|
||
message.sendPort.send(receivePort.sendPort); | ||
receivePort.listen((data) async { | ||
if (data == IsolateSupervisor.messageClose) { | ||
dev.log( | ||
'Received close message on isolate ${message.context}', | ||
name: 'FINE', | ||
); | ||
await message.server.stop(); | ||
receivePort.close(); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import 'dart:io'; | ||
import 'dart:isolate' as iso; | ||
|
||
import 'server.dart'; | ||
|
||
class IsolateMessage { | ||
final Handler handler; | ||
final SecurityContext? securityContext; | ||
final String? path; | ||
final String context; | ||
final iso.SendPort sendPort; | ||
final Server server; | ||
|
||
IsolateMessage({ | ||
required this.server, | ||
required this.handler, | ||
required this.context, | ||
this.path, | ||
this.securityContext, | ||
required this.sendPort, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import 'dart:developer' as dev; | ||
import 'dart:isolate' as iso; | ||
|
||
class IsolateSupervisor { | ||
final iso.Isolate isolate; | ||
final iso.ReceivePort receivePort; | ||
final String context; | ||
iso.SendPort? _isolateSendPort; | ||
|
||
static const String messageClose = '_CLOSE'; | ||
|
||
IsolateSupervisor({ | ||
required this.isolate, | ||
required this.receivePort, | ||
required this.context, | ||
}); | ||
|
||
void resume() { | ||
receivePort.listen(listen); | ||
isolate.resume(isolate.pauseCapability!); | ||
} | ||
|
||
void stop() { | ||
dev.log('Stopping isolate $context', name: 'FINE'); | ||
_isolateSendPort?.send(messageClose); | ||
receivePort.close(); | ||
} | ||
|
||
void listen(dynamic message) async { | ||
if (message is iso.SendPort) { | ||
_isolateSendPort = message; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ void main() { | |
test('file upload', fileUpload); | ||
|
||
tearDown(() async { | ||
await http?.closeServer(); | ||
await http?.stop(); | ||
}); | ||
}); | ||
} | ||
|
@@ -67,7 +67,7 @@ void jsonTest() async { | |
final data = { | ||
"userId": "myuserid", | ||
"email": "[email protected]", | ||
"name": "myname" | ||
"name": "myname", | ||
}; | ||
req.write(jsonEncode(data)); | ||
final res = await req.close(); | ||
|