Skip to content

Commit

Permalink
extract websocket and fix bug on isolate
Browse files Browse the repository at this point in the history
  • Loading branch information
necessarylion committed Nov 6, 2023
1 parent f3260c3 commit ae17885
Show file tree
Hide file tree
Showing 21 changed files with 79 additions and 638 deletions.
67 changes: 0 additions & 67 deletions example/cache/redis_driver.dart

This file was deleted.

52 changes: 0 additions & 52 deletions example/config/database.dart

This file was deleted.

34 changes: 0 additions & 34 deletions example/config/redis.dart

This file was deleted.

26 changes: 0 additions & 26 deletions example/example.dart

This file was deleted.

50 changes: 30 additions & 20 deletions lib/app.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import 'dart:isolate';

import 'package:dox_annotation/dox_annotation.dart';
import 'package:dox_core/dox_core.dart';
import 'package:dox_core/isolate/dox_isolate.dart';
import 'package:dox_core/server/dox_server.dart';
import 'package:dox_core/utils/logger.dart';
import 'package:sprintf/sprintf.dart';

IocContainer _ioc = IocContainer();

class Dox {
/// setup singleton
static Dox? _singleton;
Expand All @@ -27,17 +28,17 @@ class Dox {
late AppConfig config;

/// global dox ioc containers
late IocContainer ioc;

/// isolate send port
SendPort? sendPort;
IocContainer ioc = _ioc;

/// isolate Id
int? isolateId;
int isolateId = 1;

/// auth guard
Guard? authGuard;

/// websocket
IDoxWebsocket? websocket;

/// total isolate to spawn
int? _totalIsolate;

Expand All @@ -51,11 +52,10 @@ class Dox {
/// Dox().initialize(config);
/// ```
void initialize(AppConfig appConfig) async {
ioc = IocContainer();
config = appConfig;
}

/// set total thread
/// override total isolate from config
/// default is 3
void totalIsolate(int value) {
_totalIsolate = value;
Expand All @@ -64,28 +64,39 @@ class Dox {
/// set authorization config
/// and this function can only call after initialize()
/// ```
/// await Dox().initialize(config)
/// Dox().initialize(config)
/// Dox().setAuthConfig(AuthConfig())
/// ```
void setAuthConfig(AuthConfigInterface authConfig) {
authGuard = authConfig.guards[authConfig.defaultGuard];
}

/// set websocket
/// ```
/// Dox().initialize(config)
/// Dox().setWebsocket(DoxWebsocket())
/// ```
void setWebsocket(IDoxWebsocket ws) {
websocket = ws;
}

/// start dox server
/// ```
/// await Dox().startServer();
/// ```
Future<void> startServer() async {
_totalIsolate ??= Dox().config.totalIsolate;
if (_totalIsolate == 1) {
await startServices();
DoxServer().setResponseHandler(config.responseHandler);
await DoxServer().listen(config.serverPort, isolateId: 1);
} else {
await DoxIsolate().spawn(_totalIsolate!);
int isolatesToSpawn = _totalIsolate ?? 1;

await startServices();
DoxServer().setResponseHandler(config.responseHandler);
await DoxServer().listen(config.serverPort, isolateId: 1);

if (isolatesToSpawn > 1) {
await DoxIsolate().spawn(isolatesToSpawn);
}
DoxLogger.info(sprintf(
'Server started at http://127.0.0.1:%s with $_totalIsolate isolate',
'Server started at http://127.0.0.1:%s with $isolatesToSpawn isolate',
<dynamic>[Dox().config.serverPort],
));
}
Expand All @@ -106,12 +117,11 @@ class Dox {
/// this is internal core use only
/// your app do not need to call this function
Future<void> startServices() async {
_registerFormRequests();
_registerRoute();

for (DoxService service in doxServices) {
await service.setup();
}
_registerFormRequests();
_registerRoute();
}

/// ################ end ###########
Expand Down
3 changes: 0 additions & 3 deletions lib/dox_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,3 @@ export 'storage/storage_driver_interface.dart';
/// Utils
export 'utils/extensions.dart';
export 'utils/hash.dart';

/// Websocket
export 'websocket/socket_emitter.dart';
1 change: 1 addition & 0 deletions lib/env/env.dart
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Env {
Map<String, String> data = <String, String>{};

File envFile = file ?? File('${Directory.current.path}/.env');
if (!envFile.existsSync()) return data;
String contents = envFile.readAsStringSync();

// splitting with new line for each variables
Expand Down
4 changes: 3 additions & 1 deletion lib/http/request/dox_request.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
import 'dart:io';

import 'package:dox_annotation/dox_annotation.dart';
import 'package:dox_core/dox_core.dart';
import 'package:dox_core/http/request/http_request_body.dart';
import 'package:dox_core/router/route_data.dart';
import 'package:dox_core/utils/aes_encryptor.dart';
import 'package:dox_core/validation/dox_validator.dart';

class DoxRequest {
class DoxRequest implements IDoxRequest {
final RouteData route;
final Uri uri;
final ContentType? contentType;
final HttpHeaders httpHeaders;
@override
final HttpRequest httpRequest;

final String? clientIp;
Expand Down
22 changes: 2 additions & 20 deletions lib/isolate/dox_isolate.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:isolate';
import 'package:dox_core/dox_core.dart';
import 'package:dox_core/isolate/isolate_handler.dart';
import 'package:dox_core/isolate/isolate_interfaces.dart';
import 'package:dox_core/websocket/event.dart';

class DoxIsolate {
/// singleton
Expand All @@ -24,7 +23,7 @@ class DoxIsolate {
/// await DoxIsolate().spawn(3)
/// ```
Future<void> spawn(int count) async {
for (int i = 0; i < count; i++) {
for (int i = 1; i < count; i++) {
await _spawn(i + 1);
}
}
Expand All @@ -42,34 +41,17 @@ class DoxIsolate {

/// create a thread
Future<void> _spawn(int isolateId) async {
ReceivePort receivePort = ReceivePort();

Isolate isolate = await Isolate.spawn(
isolateHandler,
IsolateSpawnParameter(
isolateId,
receivePort.sendPort,
Dox().config,
Dox().doxServices,
authGuard: Dox().authGuard,
routes: Route().routes,
),
);

receivePort.listen((dynamic message) {
if (message is SendPort) {
_sendPorts[isolateId] = message;
}
if (message is WebSocketEmitEvent) {
DoxIsolate().isolates.forEach((int isolateId, _) {
SendPort? sendPort = DoxIsolate().sendPorts[isolateId];
if (sendPort != null) {
sendPort.send(message);
}
});
}
});

_isolates[isolateId] = isolate;
_receivePorts[isolateId] = receivePort;
}
}
Loading

0 comments on commit ae17885

Please sign in to comment.