vibe_limiter

Small, dependency-free helpers for keeping background work polite in Dart apps.

  • LruCache: a least-recently-used map bounded by entry count and, optionally, total weight (for example bytes). A read refreshes an entry.
  • BoundedQueue: a job queue with priorities. The same key never queues twice. When the queue is full the lowest-priority job is dropped, or the new job is rejected if it is not more important. Every drop is reported through onDrop.
  • ResourceGovernor: answers "how many background workers may run now, and how long should they pause between jobs?" from foreground activity, CPU cores and free memory.
  • GovernorPolicy: the rules behind the governor. Use DefaultGovernorPolicy (adjustable thresholds) or write your own for battery, temperature or a user setting.

Pure Dart, no dependencies.

Extracted from the VIBE music player, which uses them to scan a library without disturbing playback.

Install

dependencies:
  vibe_limiter: ^0.1.0

or dart pub add vibe_limiter.

Platform support

All Dart platforms. Free memory is read from /proc/meminfo, so the memory rules apply on Linux and Android. Elsewhere it reads 0 (unknown) and those rules are skipped; pass your own memAvailableMb to supply a reading.

Use

import 'package:vibe_limiter/vibe_limiter.dart';

final cache = LruCache<String, List<int>>(
  maxEntries: 200,
  maxWeight: 50 * 1024 * 1024,
  weigher: (bytes) => bytes.length,
);

final queue = BoundedQueue<String>(maxPending: 500, onDrop: (key, item) => log('dropped $key'));
queue.add('track-42', '/music/42.mp3', pri: 0); // 2 user, 1 normal, 0 background
final next = queue.removeFirst();

final governor = ResourceGovernor()..foregroundBusy = () => player.isPlaying;
final workers = governor.heavyWorkers; // 1 while playing or low on memory, else up to 2
await Future.delayed(governor.pace);   // pause between jobs
if (governor.underPressure) { /* skip optional work */ }

All thresholds are parameters of DefaultGovernorPolicy: maxWorkers, minCoresForMore, plentyMemoryMb, lowMemoryMb, pressureMb and the three paces. Pass policy: to ResourceGovernor to use it or your own GovernorPolicy.

Your own rules

class BatteryPolicy extends GovernorPolicy {
  @override
  int workers(GovernorSnapshot s) => onBattery ? 1 : 2;
  @override
  Duration pace(GovernorSnapshot s) => Duration(milliseconds: onBattery ? 1000 : 250);
  @override
  bool underPressure(GovernorSnapshot s) => s.memAvailableMb > 0 && s.memAvailableMb < 300;
}

final governor = ResourceGovernor(policy: BatteryPolicy());

Details worth knowing

  • LruCache always keeps the newest entry, even when it is heavier than maxWeight. It goes at the next insert.
  • BoundedQueue runs higher priority first, and oldest first among equals. When it has to drop, it drops the lowest priority, newest first.
  • heavyWorkers is cached for period (5 seconds by default), so calling it often is cheap.
  • Nothing here is thread-safe across isolates. Use one instance per isolate.

License

MIT.

Libraries

vibe_limiter
Small resource limiters for background work in Dart apps: a weighted LRU cache, a bounded priority queue with de-duplication and visible drops, and a governor that picks how many background workers may run.