GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #3916 from developing-today/queue/4860-840-world-i… · developing-today/code@b963332

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • pkgs/id/src
    • world_session
      • mod.rs
      • tests.rs
      • types.rs
    • world
      • actor.rs
      • handle.rs
      • mod.rs
      • tests.rs
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎pkgs/id/src/world/actor.rs‎

Copy file name to clipboardExpand all lines: pkgs/id/src/world/actor.rs
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ pub(crate) enum WorldCommand {
104104
Input {
105105
token: JoinCapability,
106106
input: Vec<u8>,
107+
session_id: Option<SessionId>,
107108
/// The player-signed move event bytes for this input (#2814), when
108109
/// the participant plays signed. `None` is the unsigned path,
109110
/// which refuses participants with a bound author key.
@@ -325,6 +326,9 @@ pub(crate) struct WorldActor {
325326
/// The participant whose input is being processed. Requests the program
326327
/// makes in response are checked against that participant's scopes.
327328
pub(crate) acting: Option<u64>,
329+
/// The connection whose input is being processed. Ephemeral actor context;
330+
/// it is deliberately absent from `WorldEvent` and the journal.
331+
pub(crate) acting_session: Option<SessionId>,
328332
}
329333

330334
impl WorldActor {
@@ -363,6 +367,7 @@ impl WorldActor {
363367
self.metrics.record_command(started.elapsed());
364368
self.after_change().await;
365369
self.acting = None;
370+
self.acting_session = None;
366371
}
367372
}
368373

@@ -1295,6 +1300,7 @@ impl WorldActor {
12951300
WorldCommand::Input {
12961301
token,
12971302
input,
1303+
session_id,
12981304
signed_move,
12991305
reply,
13001306
} => {
@@ -1320,6 +1326,7 @@ impl WorldActor {
13201326
)),
13211327
Ok((candidate, signed)) if self.program_healthy => {
13221328
self.acting = Some(candidate.participant_id);
1329+
self.acting_session = session_id;
13231330
match self
13241331
.program
13251332
.update(&candidate)
@@ -2070,6 +2077,7 @@ impl WorldActor {
20702077
ticks: BTreeMap::new(),
20712078
overdue_at_open,
20722079
acting: None,
2080+
acting_session: None,
20732081
auth: crate::directory_auth::DirectoryAuth::default(),
20742082
metrics: WorldMetrics::default(),
20752083
}
Collapse file

‎pkgs/id/src/world/handle.rs‎

Copy file name to clipboardExpand all lines: pkgs/id/src/world/handle.rs
+23Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl WorldHandle {
127127
ticks: BTreeMap::new(),
128128
overdue_at_open,
129129
acting: None,
130+
acting_session: None,
130131
auth: crate::directory_auth::DirectoryAuth::default(),
131132
};
132133
tokio::spawn(actor.run(receiver));
@@ -682,11 +683,32 @@ impl WorldHandle {
682683

683684
/// Submit bounded opaque game input.
684685
pub async fn input(&self, token: JoinCapability, input: Vec<u8>) -> Result<WorldEvent> {
686+
self.input_with_session(token, input, None).await
687+
}
688+
689+
/// Submit bounded opaque game input from a live session connection.
690+
pub(crate) async fn input_from_session(
691+
&self,
692+
token: JoinCapability,
693+
input: Vec<u8>,
694+
session_id: SessionId,
695+
) -> Result<WorldEvent> {
696+
self.input_with_session(token, input, Some(session_id))
697+
.await
698+
}
699+
700+
async fn input_with_session(
701+
&self,
702+
token: JoinCapability,
703+
input: Vec<u8>,
704+
session_id: Option<SessionId>,
705+
) -> Result<WorldEvent> {
685706
let (reply, response) = oneshot::channel();
686707
self.commands
687708
.send(WorldCommand::Input {
688709
token,
689710
input,
711+
session_id,
690712
signed_move: None,
691713
reply,
692714
})
@@ -732,6 +754,7 @@ impl WorldHandle {
732754
.send(WorldCommand::Input {
733755
token,
734756
input,
757+
session_id: None,
735758
signed_move: Some(signed_move),
736759
reply,
737760
})
Collapse file

‎pkgs/id/src/world/mod.rs‎

Copy file name to clipboardExpand all lines: pkgs/id/src/world/mod.rs
+11Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,17 @@ pub(crate) use util::{
7474
sanitize_display_name, token_digest,
7575
};
7676

77+
/// Identity of one live connection. It stays in memory only and is never part
78+
/// of a world event or journal entry.
79+
#[derive(Clone, Copy, Eq, Hash, PartialEq)]
80+
pub(crate) struct SessionId(u64);
81+
82+
impl SessionId {
83+
pub(crate) const fn new(value: u64) -> Self {
84+
Self(value)
85+
}
86+
}
87+
7788
/// A chat participant may send at most this many lines during the rolling window.
7889
pub(crate) const CHAT_RATE_BURST: u8 = 50;
7990
pub(crate) const CHAT_RATE_WINDOW: std::time::Duration = std::time::Duration::from_secs(10);
Collapse file

‎pkgs/id/src/world/tests.rs‎

Copy file name to clipboardExpand all lines: pkgs/id/src/world/tests.rs
+52Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5481,6 +5481,7 @@ mod schedule_tests {
54815481
.handle(WorldCommand::Input {
54825482
token: token.clone(),
54835483
input: document.to_string().into_bytes(),
5484+
session_id: None,
54845485
signed_move: None,
54855486
reply,
54865487
})
@@ -5490,6 +5491,57 @@ mod schedule_tests {
54905491
let _ = response.await.unwrap().unwrap();
54915492
}
54925493

5494+
#[tokio::test]
5495+
async fn input_session_attribution_is_ephemeral_and_distinguishes_connections() {
5496+
let (mut actor, _, token, journal) = scheduling_actor("sessions", &[], false);
5497+
const SESSION_A: u64 = 0x5eed_cafe_0000_0001;
5498+
const SESSION_B: u64 = 0x5eed_cafe_0000_0002;
5499+
let session_ids = [SessionId::new(SESSION_A), SessionId::new(SESSION_B)];
5500+
let mut participant = None;
5501+
5502+
for session_id in session_ids {
5503+
let (reply, response) = tokio::sync::oneshot::channel();
5504+
actor
5505+
.handle(WorldCommand::Input {
5506+
token: token.clone(),
5507+
input: b"{}".to_vec(),
5508+
session_id: Some(session_id),
5509+
signed_move: None,
5510+
reply,
5511+
})
5512+
.await;
5513+
5514+
let acting = actor
5515+
.acting
5516+
.expect("input participant is set while processing");
5517+
if let Some(first) = participant {
5518+
assert_eq!(acting, first);
5519+
} else {
5520+
participant = Some(acting);
5521+
}
5522+
assert!(actor.acting_session == Some(session_id));
5523+
response.await.unwrap().unwrap();
5524+
actor.after_change().await;
5525+
actor.acting = None;
5526+
actor.acting_session = None;
5527+
}
5528+
5529+
let committed: Vec<_> = journal
5530+
.entries()
5531+
.into_iter()
5532+
.filter(|entry| matches!(entry, JournalEntry::Committed { .. }))
5533+
.collect();
5534+
assert_eq!(committed.len(), 2);
5535+
for entry in committed {
5536+
let value = serde_json::to_value(entry).unwrap();
5537+
assert_eq!(value.as_object().unwrap().len(), 2);
5538+
assert!(value.get("session_id").is_none());
5539+
let serialized = value.to_string();
5540+
assert!(!serialized.contains(&SESSION_A.to_string()));
5541+
assert!(!serialized.contains(&SESSION_B.to_string()));
5542+
}
5543+
}
5544+
54935545
/// The records the program last published.
54945546
fn records(actor: &WorldActor) -> Arc<Records> {
54955547
actor.records_sender.borrow().1.clone()
Collapse file

‎pkgs/id/src/world_session/mod.rs‎

Copy file name to clipboardExpand all lines: pkgs/id/src/world_session/mod.rs
+20-4Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//! is (an Iroh node ID); that never grants world authority.
1616
1717
use std::sync::Arc;
18+
use std::sync::atomic::Ordering;
1819
use std::time::Duration;
1920

2021
use anyhow::Context as _;
@@ -30,8 +31,8 @@ use tokio::sync::{Mutex, OwnedSemaphorePermit, RwLock, Semaphore, broadcast};
3031
use crate::directory_auth::Signed;
3132
use crate::time::unix_ms;
3233
use crate::world::{
33-
Authority, CapabilityBounds, FileScope, Isolation, JoinCapability, Participant, WorldHandle,
34-
WorldMetrics, WorldPolicy, WorldScopes,
34+
Authority, CapabilityBounds, FileScope, Isolation, JoinCapability, Participant, SessionId,
35+
WorldHandle, WorldMetrics, WorldPolicy, WorldScopes,
3536
};
3637
use crate::world_compile::Compiler;
3738
#[cfg(feature = "sandbox")]
@@ -64,6 +65,11 @@ pub use types::{
6465
};
6566

6667
impl WorldService {
68+
/// Allocate an ephemeral identity for one accepted connection.
69+
pub(crate) fn next_session_id(&self) -> SessionId {
70+
SessionId::new(self.next_session_id.fetch_add(1, Ordering::Relaxed))
71+
}
72+
6773
/// Require module artifacts to carry a valid signature from one of these
6874
/// Ed25519 public keys. An empty list preserves the legacy unsigned path.
6975
#[must_use]
@@ -193,6 +199,7 @@ impl WorldService {
193199
mail: None,
194200
outbox: None,
195201
profile_operation: Arc::new(Mutex::new(())),
202+
next_session_id: Arc::new(std::sync::atomic::AtomicU64::new(1)),
196203
}
197204
}
198205

@@ -2109,6 +2116,7 @@ async fn joined<I: SessionIo>(
21092116
screen_patches: bool,
21102117
) {
21112118
let world = service.world();
2119+
let session_id = service.next_session_id();
21122120
// Subscribe before taking the snapshot so no event can fall in the gap;
21132121
// events at or below the cursor are skipped when delivered.
21142122
let mut events = world.subscribe();
@@ -2230,6 +2238,7 @@ async fn joined<I: SessionIo>(
22302238
&text,
22312239
&mut viewport,
22322240
cursor,
2241+
session_id,
22332242
&mut view_state,
22342243
screen_patches,
22352244
&mut pending_view_deadline,
@@ -2501,6 +2510,7 @@ async fn handle_client_frame<I: SessionIo>(
25012510
text: &str,
25022511
viewport: &mut Option<Viewport>,
25032512
sequence: u64,
2513+
session_id: SessionId,
25042514
view_state: &mut ViewRevisionState,
25052515
screen_patches: bool,
25062516
pending_view_deadline: &mut Option<tokio::time::Instant>,
@@ -2591,10 +2601,16 @@ async fn handle_client_frame<I: SessionIo>(
25912601
)
25922602
.await;
25932603
};
2594-
world.input(token.clone(), bytes).await
2604+
world
2605+
.input_from_session(token.clone(), bytes, session_id)
2606+
.await
25952607
}
25962608
ClientFrame::Input { data_hex } => match decode_hex(&data_hex) {
2597-
Some(data) => world.input(token.clone(), data).await,
2609+
Some(data) => {
2610+
world
2611+
.input_from_session(token.clone(), data, session_id)
2612+
.await
2613+
}
25982614
None => {
25992615
return send_error(service.frame_meter(), io, "input data must be hexadecimal")
26002616
.await;
Collapse file

‎pkgs/id/src/world_session/tests.rs‎

Copy file name to clipboardExpand all lines: pkgs/id/src/world_session/tests.rs
+8Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,14 @@ mod tests {
276276
assert_eq!(records["sequence"], 1);
277277
}
278278

279+
#[tokio::test]
280+
async fn cloned_service_allocates_distinct_ids_for_connections() {
281+
let service = service(None);
282+
let another_connection = service.clone();
283+
284+
assert!(service.next_session_id() != another_connection.next_session_id());
285+
}
286+
279287
#[tokio::test]
280288
async fn execution_profile_change_is_admin_only_and_does_not_replace_live_program() {
281289
struct MarkerProgram;
Collapse file

‎pkgs/id/src/world_session/types.rs‎

Copy file name to clipboardExpand all lines: pkgs/id/src/world_session/types.rs
+5-2Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::collections::BTreeMap;
22
use std::sync::Arc;
3+
use std::sync::atomic::AtomicU64;
34
use std::time::Duration;
45

56
use iroh_blobs::api::{Store, TempTag};
@@ -8,8 +9,8 @@ use tokio::sync::{Mutex, RwLock, Semaphore};
89

910
use crate::directory_auth::Signed;
1011
use crate::world::{
11-
CapabilityBounds, FileScope, Isolation, MetricsSnapshot, WorldEvent, WorldHandle, WorldMetrics,
12-
WorldPolicy, WorldSnapshot,
12+
CapabilityBounds, FileScope, Isolation, MetricsSnapshot, SessionId, WorldEvent, WorldHandle,
13+
WorldMetrics, WorldPolicy, WorldSnapshot,
1314
};
1415
use crate::world_compile::Compiler;
1516
use crate::world_hub::WorldInfo;
@@ -546,6 +547,8 @@ pub struct WorldService {
546547
/// The world's host metrics registry (F11.7), shared with the actor:
547548
/// the session layer records the frame bytes it sends into it.
548549
pub(crate) metrics: WorldMetrics,
550+
/// Monotonic, process-local IDs for live connections to this service.
551+
pub(crate) next_session_id: Arc<AtomicU64>,
549552
}
550553

551554
impl std::fmt::Debug for WorldService {

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.