Refactor concurrency handling and improve sleep precision in concurrency.dart and header.dart
This commit is contained in:
@@ -5,7 +5,7 @@ import 'dart:isolate';
|
||||
import 'dart:math';
|
||||
|
||||
import 'package:sweepstore/header.dart';
|
||||
import 'package:sweepstore/helper_extensions.dart';
|
||||
import 'package:sweepstore/helpers.dart';
|
||||
import 'package:sweepstore/structures.dart';
|
||||
|
||||
int _randomId() {
|
||||
@@ -28,17 +28,18 @@ void spawnTicket(RandomAccessFile file, {
|
||||
void log(String message) {
|
||||
|
||||
String prefix = debugLabel != null ? "\x1B[38;5;208m[Ticket Spawner - $debugLabel]:\x1B[0m " : "\x1B[38;5;208m[Ticket Spawner]:\x1B[0m ";
|
||||
print("$prefix$message");
|
||||
// print("$prefix$message");
|
||||
|
||||
}
|
||||
void tickSleep([int microsecondVariance = 10]) {
|
||||
sleep(Duration(microseconds: 100 + Random().nextInt(microsecondVariance)));
|
||||
preciseSleep(Duration(microseconds: 100 + Random().nextInt(microsecondVariance)));
|
||||
}
|
||||
Map<String, int> expSleepTracker = {};
|
||||
void expSleep(String label) {
|
||||
int count = expSleepTracker[label] ?? 0;
|
||||
int sleepTime = (1 << count); // Exponential backoff
|
||||
sleep(Duration(milliseconds: sleepTime));
|
||||
// sleepTime = max(1, min(sleepTime, 1000)); // Clamp between 1ms and 1000ms
|
||||
preciseSleep(Duration(microseconds: sleepTime * 5000));
|
||||
expSleepTracker[label] = count + 1;
|
||||
}
|
||||
|
||||
@@ -83,6 +84,7 @@ void spawnTicket(RandomAccessFile file, {
|
||||
|
||||
expSleep("acquire_loop");
|
||||
|
||||
|
||||
// Ensure we still own the ticket - if not, reset and try again
|
||||
if (ticketIndex != null) {
|
||||
SweepstoreWorkerTicket ticket = concurrencyHeader[ticketIndex];
|
||||
@@ -145,7 +147,7 @@ void initialiseMasterListener(RandomAccessFile file) async {
|
||||
Isolate.spawn((_) {
|
||||
|
||||
void log(String message) {
|
||||
print("\x1B[38;5;82m[Master Listener]:\x1B[0m $message");
|
||||
// print("\x1B[38;5;82m[Master Listener]:\x1B[0m $message");
|
||||
}
|
||||
|
||||
RandomAccessFile file = File(filePath).openSync(mode: FileMode.append);
|
||||
@@ -183,7 +185,7 @@ void initialiseMasterListener(RandomAccessFile file) async {
|
||||
|
||||
}
|
||||
|
||||
sleep(Duration(milliseconds: 1));
|
||||
preciseSleep(Duration(milliseconds: 1));
|
||||
|
||||
}
|
||||
}, null);
|
||||
|
||||
Reference in New Issue
Block a user