Add Python mesh snapshot edge limits
This commit is contained in:
parent
52f9d0de00
commit
626646a95a
6 changed files with 92 additions and 15 deletions
|
|
@ -107,6 +107,9 @@ CultCache, CultNet, and CultMesh. It is evidence, not confetti.
|
|||
Its shard catalog advertises the database-owned shard ids, schema ids, and
|
||||
latest shard epochs so discovery and shard-log catch-up agree on the same
|
||||
authority.
|
||||
Public mesh edges can set snapshot document and encoded-byte limits; oversized
|
||||
snapshot responses are rejected as `cultnet.error.v0` before the payload is
|
||||
sent.
|
||||
- Python CultMesh reactive catalogs:
|
||||
Verse and peer catalogs expose sorted local views, `get(...)`, response
|
||||
application, and `watch(...)` callbacks for local discovery updates. Verse
|
||||
|
|
|
|||
|
|
@ -275,7 +275,14 @@ session = CultMesh.create_game_session(
|
|||
),
|
||||
),
|
||||
)
|
||||
server = CultMesh.serve_node(node, peer_catalog=peers, observation_hub=session.observation_hub, port=3075)
|
||||
server = CultMesh.serve_node(
|
||||
node,
|
||||
peer_catalog=peers,
|
||||
observation_hub=session.observation_hub,
|
||||
port=3075,
|
||||
max_snapshot_documents=1000,
|
||||
max_snapshot_bytes=4 * 1024 * 1024,
|
||||
)
|
||||
|
||||
client = CultMesh.create_verse_discovery_client("127.0.0.1", 3075)
|
||||
verses = client.fetch_verses(transport_version="cultmesh.v0")
|
||||
|
|
|
|||
|
|
@ -107,6 +107,8 @@ class CultMesh:
|
|||
host: str = "127.0.0.1",
|
||||
port: int = 0,
|
||||
display_name: str | None = None,
|
||||
max_snapshot_documents: int | None = None,
|
||||
max_snapshot_bytes: int | None = None,
|
||||
) -> CultMeshLocalServer:
|
||||
return CultMeshLocalServer(
|
||||
node=node,
|
||||
|
|
@ -116,6 +118,8 @@ class CultMesh:
|
|||
host=host,
|
||||
port=port,
|
||||
display_name=display_name,
|
||||
max_snapshot_documents=max_snapshot_documents,
|
||||
max_snapshot_bytes=max_snapshot_bytes,
|
||||
).start()
|
||||
|
||||
@staticmethod
|
||||
|
|
|
|||
|
|
@ -43,10 +43,18 @@ class CultMeshLocalServer:
|
|||
port: int = 0
|
||||
display_name: str | None = None
|
||||
runtime_kind: str = "python"
|
||||
max_snapshot_documents: int | None = None
|
||||
max_snapshot_bytes: int | None = None
|
||||
_socket: socket.socket | None = field(default=None, init=False, repr=False)
|
||||
_stop: threading.Event = field(default_factory=threading.Event, init=False, repr=False)
|
||||
_thread: threading.Thread | None = field(default=None, init=False, repr=False)
|
||||
|
||||
def __post_init__(self) -> None:
|
||||
if self.max_snapshot_documents is not None and self.max_snapshot_documents < 0:
|
||||
raise ValueError("max_snapshot_documents must be non-negative")
|
||||
if self.max_snapshot_bytes is not None and self.max_snapshot_bytes < 0:
|
||||
raise ValueError("max_snapshot_bytes must be non-negative")
|
||||
|
||||
def start(self) -> "CultMeshLocalServer":
|
||||
if self._socket is not None:
|
||||
return self
|
||||
|
|
@ -87,13 +95,7 @@ class CultMeshLocalServer:
|
|||
if schema_version == "cultnet.schema_catalog_request.v0":
|
||||
return self._schema_catalog_response(message)
|
||||
if schema_version == "cultnet.snapshot_request.v0":
|
||||
return self.node.database.create_snapshot_response(
|
||||
message_id=str(message.get("messageId") or ""),
|
||||
schema_ids=[str(value) for value in message.get("schemaIds") or []],
|
||||
record_keys=[str(value) for value in message.get("recordKeys") or []],
|
||||
shard_id=message.get("shardId"),
|
||||
shard_epoch=message.get("shardEpoch"),
|
||||
)
|
||||
return self._snapshot_response(message)
|
||||
if schema_version == "cultnet.shard_catalog_request.v0":
|
||||
return self._shard_catalog_response(message)
|
||||
if schema_version == "cultnet.shard_log_request.v0":
|
||||
|
|
@ -159,13 +161,12 @@ class CultMeshLocalServer:
|
|||
)
|
||||
if message.get("includeSnapshot", True) is False:
|
||||
return []
|
||||
return [
|
||||
self.node.database.create_snapshot_response(
|
||||
message_id=str(message.get("messageId") or ""),
|
||||
schema_ids=list(subscriptions[subscription_id].schema_ids),
|
||||
record_keys=list(subscriptions[subscription_id].record_keys),
|
||||
)
|
||||
]
|
||||
snapshot_request = {
|
||||
"messageId": str(message.get("messageId") or ""),
|
||||
"schemaIds": list(subscriptions[subscription_id].schema_ids),
|
||||
"recordKeys": list(subscriptions[subscription_id].record_keys),
|
||||
}
|
||||
return [self._snapshot_response(snapshot_request)]
|
||||
if schema_version == "cultnet.database_unsubscribe.v0":
|
||||
subscriptions.pop(str(message.get("subscriptionId") or ""), None)
|
||||
return []
|
||||
|
|
@ -283,6 +284,37 @@ class CultMeshLocalServer:
|
|||
"supportsSchemaCatalog": True,
|
||||
}
|
||||
|
||||
def _snapshot_response(self, request: dict[str, Any]) -> dict[str, Any]:
|
||||
response = self.node.database.create_snapshot_response(
|
||||
message_id=str(request.get("messageId") or ""),
|
||||
schema_ids=[str(value) for value in request.get("schemaIds") or []],
|
||||
record_keys=[str(value) for value in request.get("recordKeys") or []],
|
||||
shard_id=request.get("shardId"),
|
||||
shard_epoch=request.get("shardEpoch"),
|
||||
)
|
||||
return self._enforce_snapshot_limits(response)
|
||||
|
||||
def _enforce_snapshot_limits(self, response: dict[str, Any]) -> dict[str, Any]:
|
||||
document_count = len(response.get("documents") or [])
|
||||
if self.max_snapshot_documents is not None and document_count > self.max_snapshot_documents:
|
||||
return self._error_response(
|
||||
f"Snapshot document limit exceeded: {document_count} > {self.max_snapshot_documents}."
|
||||
)
|
||||
if self.max_snapshot_bytes is not None:
|
||||
response_bytes = len(msgpack.packb(response, use_bin_type=True))
|
||||
if response_bytes > self.max_snapshot_bytes:
|
||||
return self._error_response(
|
||||
f"Snapshot byte limit exceeded: {response_bytes} > {self.max_snapshot_bytes}."
|
||||
)
|
||||
return response
|
||||
|
||||
@staticmethod
|
||||
def _error_response(error: str) -> dict[str, Any]:
|
||||
return {
|
||||
"schemaVersion": "cultnet.error.v0",
|
||||
"error": error,
|
||||
}
|
||||
|
||||
def _candidate_responses(self, message: dict[str, Any]) -> list[dict[str, Any]]:
|
||||
if self.observation_hub is None:
|
||||
return []
|
||||
|
|
|
|||
|
|
@ -49,6 +49,8 @@ class CultNetRawClient:
|
|||
raise ValueError("CultNet response must be a MessagePack map")
|
||||
schema_version = response.get("schemaVersion")
|
||||
if schema_version != expected_schema_version:
|
||||
if schema_version == "cultnet.error.v0":
|
||||
raise ValueError(str(response.get("error") or "CultNet peer returned an error"))
|
||||
raise ValueError(f"Expected {expected_schema_version}, received {schema_version!r}")
|
||||
return response
|
||||
|
||||
|
|
|
|||
|
|
@ -2566,6 +2566,35 @@ class CultCacheTests(unittest.TestCase):
|
|||
self.assertIsNone(subscription_delete.raw_document)
|
||||
self.assertIsNone(node.database.get(document, "note:2"))
|
||||
|
||||
def test_cultmesh_local_server_rejects_oversized_snapshot_responses(self) -> None:
|
||||
document = define_database_entry_type(
|
||||
"mesh.snapshot_limit_note",
|
||||
[("body", 0)],
|
||||
schema_id="mesh.snapshot_limit_note.v1",
|
||||
)
|
||||
node = CultMesh.create_node(runtime_id="mesh-snapshot-limits")
|
||||
node.database.register_document(document)
|
||||
node.database.put_raw_message(document, "note:1", {"body": "first"})
|
||||
node.database.put_raw_message(document, "note:2", {"body": "second"})
|
||||
|
||||
server = CultMesh.serve_node(node, max_snapshot_documents=1)
|
||||
try:
|
||||
raw_client = CultMesh.create_client("127.0.0.1", server.port, timeout_seconds=2.0)
|
||||
filtered_snapshot = raw_client.fetch_snapshot(record_keys=["note:1"])
|
||||
error = raw_client.request(
|
||||
snapshot_request(schema_ids=["mesh.snapshot_limit_note.v1"]),
|
||||
expected_schema_version="cultnet.error.v0",
|
||||
)
|
||||
with self.assertRaisesRegex(ValueError, "Snapshot document limit exceeded"):
|
||||
raw_client.fetch_snapshot(schema_ids=["mesh.snapshot_limit_note.v1"])
|
||||
finally:
|
||||
server.stop()
|
||||
|
||||
self.assertEqual(filtered_snapshot["schemaVersion"], "cultnet.snapshot_response_raw.v0")
|
||||
self.assertEqual([document["recordKey"] for document in filtered_snapshot["documents"]], ["note:1"])
|
||||
self.assertEqual(error["schemaVersion"], "cultnet.error.v0")
|
||||
self.assertIn("Snapshot document limit exceeded", error["error"])
|
||||
|
||||
def test_cultmesh_authority_lease_requires_live_matching_lease(self) -> None:
|
||||
peer = CultMeshPeerCard(
|
||||
peer_id="voidbot-local",
|
||||
|
|
|
|||
Loading…
Reference in a new issue