runRequest method

Future<void> runRequest(
  1. Future<Response> handler(
    1. Request req
    ),
  2. Request req,
  3. Response res, {
  4. Duration? timeout,
})

Runs a Flint Dart Request handler in an isolate.

Useful for offloading HTTP request processing to isolates for CPU-heavy tasks. The response res is sent automatically when the task completes.

Optionally, a timeout can be specified.

Implementation

Future<void> runRequest(
  Future<Response> Function(Request req) handler,
  Request req,
  Response res, {
  Duration? timeout,
}) async {
  try {
    final response = await run<Response, Map<String, dynamic>>(
      (params) async {
        final reqData = params['req'] as Request;
        return await handler(reqData);
      },
      {'req': req},
      timeout: timeout,
    );

    // Replace with actual Response sending logic
    await res.sendStreamed(response);
  } catch (e, stack) {
    res.status(500).respond({'error': e.toString()});
    print('IsolatePoolHelper.runRequest error: $e\n$stack');
  }
}