mirror of
https://github.com/kuhyx/utils.git
synced 2026-11-29 14:43:32 +01:00
Compare commits
No commits in common. "89f464828a12fe0e1b588b8776f898294932e329" and "46ab26fa1581afcaa79a652a327ff42add623494" have entirely different histories.
89f464828a
...
46ab26fa15
@ -7,11 +7,8 @@ import 'store.dart';
|
|||||||
/// Writes atomically -- to a per-process temp file, then `rename`d over the
|
/// Writes atomically -- to a per-process temp file, then `rename`d over the
|
||||||
/// real path -- so a concurrent reader (e.g. a background sync isolate writing
|
/// real path -- so a concurrent reader (e.g. a background sync isolate writing
|
||||||
/// while the foreground app also writes) never observes a half-written file.
|
/// while the foreground app also writes) never observes a half-written file.
|
||||||
/// The pid in the temp name keeps two processes from colliding; within one
|
/// The pid in the temp name keeps two writers from colliding. This generalizes
|
||||||
/// instance, writes are serialized in issue order (last issued wins), so
|
/// diet_guard's hand-rolled `LogStorageService` write scheme.
|
||||||
/// overlapping calls (e.g. per-keystroke saves) never share the temp file
|
|
||||||
/// mid-flight. This generalizes diet_guard's hand-rolled `LogStorageService`
|
|
||||||
/// write scheme.
|
|
||||||
///
|
///
|
||||||
/// Lives behind the `package:crdt_sync/crdt_sync_io.dart` entrypoint, not the
|
/// Lives behind the `package:crdt_sync/crdt_sync_io.dart` entrypoint, not the
|
||||||
/// main barrel, because it imports `dart:io` and so is unavailable on web --
|
/// main barrel, because it imports `dart:io` and so is unavailable on web --
|
||||||
@ -23,10 +20,6 @@ class FileLogPersistence implements LogPersistence {
|
|||||||
|
|
||||||
final File _file;
|
final File _file;
|
||||||
|
|
||||||
/// Completes when the most recently issued write has settled. Never
|
|
||||||
/// rejects, so one failed write cannot block the ones queued after it.
|
|
||||||
Future<void> _tail = Future<void>.value();
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<String?> read() async {
|
Future<String?> read() async {
|
||||||
if (!_file.existsSync()) return null;
|
if (!_file.existsSync()) return null;
|
||||||
@ -38,14 +31,7 @@ class FileLogPersistence implements LogPersistence {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Future<void> write(String text) {
|
Future<void> write(String text) async {
|
||||||
final result = _tail.then((_) => _writeNow(text));
|
|
||||||
// The caller still sees its own error via [result]; the chain ignores it.
|
|
||||||
_tail = result.then((_) {}, onError: (Object _) {});
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Future<void> _writeNow(String text) async {
|
|
||||||
await _file.parent.create(recursive: true);
|
await _file.parent.create(recursive: true);
|
||||||
final tmp = File('${_file.path}.$pid.tmp');
|
final tmp = File('${_file.path}.$pid.tmp');
|
||||||
await tmp.writeAsString(text);
|
await tmp.writeAsString(text);
|
||||||
|
|||||||
@ -2,7 +2,7 @@ name: crdt_sync
|
|||||||
description: >-
|
description: >-
|
||||||
Shared CRDT merge scheme + GitHub-Contents-API sync transport for
|
Shared CRDT merge scheme + GitHub-Contents-API sync transport for
|
||||||
cross-device apps. Pure Dart, no Flutter dependency.
|
cross-device apps. Pure Dart, no Flutter dependency.
|
||||||
version: 0.12.1
|
version: 0.12.0
|
||||||
publish_to: 'none'
|
publish_to: 'none'
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
|
|||||||
@ -128,32 +128,5 @@ void main() {
|
|||||||
await store.write('{"hello":"world"}');
|
await store.write('{"hello":"world"}');
|
||||||
expect(await store.read(), '{"hello":"world"}');
|
expect(await store.read(), '{"hello":"world"}');
|
||||||
});
|
});
|
||||||
|
|
||||||
test('50 overlapping writes all succeed and the last issued wins',
|
|
||||||
() async {
|
|
||||||
// Regression: every call shared one `<file>.<pid>.tmp`, so the first
|
|
||||||
// rename moved it away and a later rename threw PathNotFoundException.
|
|
||||||
final store = FileLogPersistence(File('${dir.path}/log.json'));
|
|
||||||
await Future.wait([
|
|
||||||
for (var i = 0; i < 50; i++) store.write('{"n":$i}'),
|
|
||||||
]);
|
|
||||||
expect(await store.read(), '{"n":49}');
|
|
||||||
expect(dir.listSync().map((e) => e.uri.pathSegments.last),
|
|
||||||
['log.json']);
|
|
||||||
});
|
|
||||||
|
|
||||||
test('a failed write rejects its caller but does not block later writes',
|
|
||||||
() async {
|
|
||||||
final target = File('${dir.path}/log.json');
|
|
||||||
// A directory at the target path makes the rename fail.
|
|
||||||
Directory(target.path).createSync();
|
|
||||||
final store = FileLogPersistence(target);
|
|
||||||
await expectLater(
|
|
||||||
store.write('{"n":0}'), throwsA(isA<FileSystemException>()));
|
|
||||||
|
|
||||||
Directory(target.path).deleteSync();
|
|
||||||
await store.write('{"n":1}');
|
|
||||||
expect(await store.read(), '{"n":1}');
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,7 +4,7 @@ description: >-
|
|||||||
persisted per-install device id, and a one-call bootstrap. Removes the
|
persisted per-install device id, and a one-call bootstrap. Removes the
|
||||||
~150 lines of firebase_backend.dart every consuming app was copying.
|
~150 lines of firebase_backend.dart every consuming app was copying.
|
||||||
publish_to: 'none'
|
publish_to: 'none'
|
||||||
version: 0.5.1
|
version: 0.5.0
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.12.2
|
sdk: ^3.12.2
|
||||||
@ -13,7 +13,7 @@ dependencies:
|
|||||||
crdt_sync:
|
crdt_sync:
|
||||||
git:
|
git:
|
||||||
url: https://github.com/kuhyx/utils
|
url: https://github.com/kuhyx/utils
|
||||||
ref: crdt_sync_dart-v0.12.1
|
ref: crdt_sync_dart-v0.12.0
|
||||||
path: crdt_sync_dart
|
path: crdt_sync_dart
|
||||||
crypto: 3.0.8
|
crypto: 3.0.8
|
||||||
flutter:
|
flutter:
|
||||||
|
|||||||
@ -91,8 +91,6 @@ NPM_ACCEPT: Final = "application/vnd.npm.install-v1+json"
|
|||||||
# --- Cache ------------------------------------------------------------------
|
# --- Cache ------------------------------------------------------------------
|
||||||
|
|
||||||
CACHE_PATH_ENV: Final = "DEP_FRESHNESS_CACHE"
|
CACHE_PATH_ENV: Final = "DEP_FRESHNESS_CACHE"
|
||||||
# Seconds; overrides every ecosystem TTL (a repo that wants a once-a-day gate).
|
|
||||||
TTL_ENV: Final = "DEP_FRESHNESS_TTL_SECONDS"
|
|
||||||
DEFAULT_CACHE_DIR: Final = "~/.cache/dep-freshness"
|
DEFAULT_CACHE_DIR: Final = "~/.cache/dep-freshness"
|
||||||
CACHE_FILE: Final = "registry.json"
|
CACHE_FILE: Final = "registry.json"
|
||||||
TTL_SECONDS: Final = 6 * 3600
|
TTL_SECONDS: Final = 6 * 3600
|
||||||
|
|||||||
@ -21,7 +21,6 @@ from dep_freshness._tables import (
|
|||||||
DEFAULT_CACHE_DIR,
|
DEFAULT_CACHE_DIR,
|
||||||
GITCOMMIT,
|
GITCOMMIT,
|
||||||
GITTAG,
|
GITTAG,
|
||||||
TTL_ENV,
|
|
||||||
TTL_GITTAG_SECONDS,
|
TTL_GITTAG_SECONDS,
|
||||||
TTL_SECONDS,
|
TTL_SECONDS,
|
||||||
)
|
)
|
||||||
@ -34,14 +33,7 @@ def cache_dir() -> Path:
|
|||||||
|
|
||||||
|
|
||||||
def ttl_for(ecosystem: str) -> int:
|
def ttl_for(ecosystem: str) -> int:
|
||||||
"""Git refs move rarely and cost a subprocess, so they cache longer.
|
"""Git refs move rarely and cost a subprocess, so they cache longer."""
|
||||||
|
|
||||||
`DEP_FRESHNESS_TTL_SECONDS` overrides both; garbage is ignored, because a
|
|
||||||
typo in an env var must not break a commit hook.
|
|
||||||
"""
|
|
||||||
override = os.environ.get(TTL_ENV, "")
|
|
||||||
if override.isdigit() and int(override) > 0:
|
|
||||||
return int(override)
|
|
||||||
return TTL_GITTAG_SECONDS if ecosystem in (GITTAG, GITCOMMIT) else TTL_SECONDS
|
return TTL_GITTAG_SECONDS if ecosystem in (GITTAG, GITCOMMIT) else TTL_SECONDS
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -12,7 +12,6 @@ from pathlib import Path
|
|||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
from dep_freshness._tables import TTL_SECONDS
|
|
||||||
from dep_freshness.cache import Cache, Entry, ttl_for
|
from dep_freshness.cache import Cache, Entry, ttl_for
|
||||||
from dep_freshness.models import Dep
|
from dep_freshness.models import Dep
|
||||||
from dep_freshness.registries import http
|
from dep_freshness.registries import http
|
||||||
@ -140,16 +139,3 @@ def _offline(*_args):
|
|||||||
|
|
||||||
def _explode(*_args):
|
def _explode(*_args):
|
||||||
raise RuntimeError("boom")
|
raise RuntimeError("boom")
|
||||||
|
|
||||||
|
|
||||||
def test_ttl_env_overrides_the_registry_ttl(monkeypatch) -> None:
|
|
||||||
"""A repo can stretch the refresh window (once-a-day pre-push gates)."""
|
|
||||||
monkeypatch.setenv("DEP_FRESHNESS_TTL_SECONDS", "86400")
|
|
||||||
assert ttl_for("npm") == 86400
|
|
||||||
assert ttl_for("gittag") == 86400
|
|
||||||
|
|
||||||
|
|
||||||
def test_ttl_env_garbage_falls_back_to_the_default(monkeypatch) -> None:
|
|
||||||
"""A malformed value must never break a commit hook."""
|
|
||||||
monkeypatch.setenv("DEP_FRESHNESS_TTL_SECONDS", "soon")
|
|
||||||
assert ttl_for("npm") == TTL_SECONDS
|
|
||||||
|
|||||||
@ -5,7 +5,7 @@ description: >-
|
|||||||
injected closures -- no keystore or platform plugin access lives in this
|
injected closures -- no keystore or platform plugin access lives in this
|
||||||
package itself.
|
package itself.
|
||||||
publish_to: 'none'
|
publish_to: 'none'
|
||||||
version: 0.6.2
|
version: 0.6.1
|
||||||
|
|
||||||
environment:
|
environment:
|
||||||
sdk: ^3.12.2
|
sdk: ^3.12.2
|
||||||
@ -14,7 +14,7 @@ dependencies:
|
|||||||
crdt_sync:
|
crdt_sync:
|
||||||
git:
|
git:
|
||||||
url: https://github.com/kuhyx/utils
|
url: https://github.com/kuhyx/utils
|
||||||
ref: crdt_sync_dart-v0.12.1
|
ref: crdt_sync_dart-v0.12.0
|
||||||
path: crdt_sync_dart
|
path: crdt_sync_dart
|
||||||
data_transfer_flutter:
|
data_transfer_flutter:
|
||||||
git:
|
git:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user