Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Global variables getting ignored after some duration #439

Open
CodeCuration opened this issue Jul 7, 2024 · 2 comments
Open

Global variables getting ignored after some duration #439

CodeCuration opened this issue Jul 7, 2024 · 2 comments

Comments

@CodeCuration
Copy link

I have a global variable isRunning and a loop myLoop in globals.dart which starts when /test route is hit. When there are no requests to /test route for few minutes (Don't know the specific duration) and /test route gets a hit then isRunning gets ignored and loop starts twice.

To start the server server.dart:

import 'dart:io';
import 'dart:isolate';
import 'package:shelf/shelf.dart';
import 'package:shelf/shelf_io.dart';
import 'router.dart';

void main() {
  int numberOfCores = Platform.numberOfProcessors;
  final handler = const Pipeline()
      .addMiddleware(
        logRequests(),
      )
      .addHandler(router);
  for (int i = 1; i < numberOfCores; i++) {
    Isolate.spawn(startServer, handler);
  }
  startServer(handler);
}

startServer(Handler handler) async {
  HttpServer myserver = await serve(
    handler,
    '0.0.0.0',
    8000,
    shared: true,
  );
  print('Serving at http://${myserver.address.host}:${myserver.port}');
}

Handle routes router.dart:

import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';
import 'test_controller.dart';

Handler get router {
  final router = Router();

  router.get('/test', (Request request) async {
    return await testController(request);
  });

  return router;
}

Testing controller test_controller.dart:

import 'package:shelf/shelf.dart';
import 'globals.dart';

Future<Response> testController(Request request) async {
  return await myFunction("JUSTFORTESTING");
}

Global functions and variables globals.dart:

import 'package:shelf/shelf.dart';

bool isRunning = false;

Future<Response> myFunction(String param) async {
  if (!isRunning) {
    print("🔁 Starting loop...");
    isRunning = true;
    myLoop();
  }
  return Response.ok('Test route');
}

Future myLoop() async {
  int i = 0;
  while (true) {
    i++;
    print("✅ Done $i");
    await Future.delayed(Duration(seconds: 5));
  }
}

Output:

Serving at http://0.0.0.0:8000
Serving at http://0.0.0.0:8000
Serving at http://0.0.0.0:8000
Serving at http://0.0.0.0:8000
Serving at http://0.0.0.0:8000
Serving at http://0.0.0.0:8000
Serving at http://0.0.0.0:8000
Serving at http://0.0.0.0:8000
🔁 Starting loop...
✅ Done 1
2024-07-07T19:38:16.517826  0:00:00.006100 GET     [200] /test
✅ Done 2Done 3Done 4Done 5Done 6Done 7Done 8Done 9Done 10Done 11
2024-07-07T19:39:08.385526  0:00:00.000280 GET     [200] /test
✅ Done 12Done 13Done 14Done 15Done 16Done 17Done 18Done 19Done 20Done 21
🔁 Starting loop...
✅ Done 1 // Loop started again while previous loop is already running
2024-07-07T19:40:00.279199  0:00:00.000169 GET     [200] /test
✅ Done 22Done 2Done 23Done 3Done 24
@CodeCuration
Copy link
Author

Upon further investigation it looks like it is happening because of isolates. Is there any way to share the global variable and sync between them?

@ykmnkmi
Copy link

ykmnkmi commented Jul 8, 2024

You can look at aqueduct/conduit MessageHub & Threading.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants