state: track pending c2n requests

A node answers a c2n request with a separate POST to control. Register
each request under a random id so the answer reaches the caller waiting
for it, and drop the entry when the caller gives up.
This commit is contained in:
Martín Gómez authored and Kristoffer Dalby committed 2026-11-05 15:51:04 +01:00
1 parent 2853d71d24
commit 672340c07e
2 files changed
+169

No files matched your search

+98
View File
@@ -17,6 +17,7 @@ const pingIDLength = 16
type pingTracker struct {
mu sync.Mutex
pending map[string]*pendingPing
c2n map[string]*pendingC2N
}
type pendingPing struct {
@@ -25,9 +26,17 @@ type pendingPing struct {
responseCh chan time.Duration
}
// pendingC2N is a c2n [tailcfg.PingRequest] waiting for the node to POST
// back the raw HTTP response produced by its c2n handler.
type pendingC2N struct {
nodeID types.NodeID
responseCh chan []byte
}
func newPingTracker() *pingTracker {
return &pingTracker{
pending: make(map[string]*pendingPing),
c2n: make(map[string]*pendingC2N),
}
}
@@ -90,6 +99,70 @@ func (pt *pingTracker) drain() {
close(pp.responseCh)
delete(pt.pending, id)
}
for id, pc := range pt.c2n {
close(pc.responseCh)
delete(pt.c2n, id)
}
}
// registerC2N creates a pending c2n request for nodeID and returns a
// unique ID and a channel that receives the raw HTTP response the node
// sends back.
func (pt *pingTracker) registerC2N(nodeID types.NodeID) (string, <-chan []byte) {
c2nID := rands.HexString(pingIDLength)
ch := make(chan []byte, 1)
pt.mu.Lock()
pt.c2n[c2nID] = &pendingC2N{
nodeID: nodeID,
responseCh: ch,
}
pt.mu.Unlock()
return c2nID, ch
}
// c2nTarget returns the node a pending c2n request was sent to.
func (pt *pingTracker) c2nTarget(c2nID string) (types.NodeID, bool) {
pt.mu.Lock()
defer pt.mu.Unlock()
pc, ok := pt.c2n[c2nID]
if !ok {
return 0, false
}
return pc.nodeID, true
}
// completeC2N delivers body on the response channel and returns true.
// Returns false if the c2nID is unknown (already completed or cancelled).
func (pt *pingTracker) completeC2N(c2nID string, body []byte) bool {
pt.mu.Lock()
pc, ok := pt.c2n[c2nID]
if ok {
delete(pt.c2n, c2nID)
}
pt.mu.Unlock()
if !ok {
return false
}
pc.responseCh <- body
close(pc.responseCh)
return true
}
// cancelC2N removes a pending c2n request without completing it. Idempotent.
func (pt *pingTracker) cancelC2N(c2nID string) {
pt.mu.Lock()
delete(pt.c2n, c2nID)
pt.mu.Unlock()
}
// RegisterPing tracks a pending ping and returns its ID and a channel
@@ -109,3 +182,28 @@ func (s *State) CompletePing(pingID string) bool {
func (s *State) CancelPing(pingID string) {
s.pings.cancel(pingID)
}
// RegisterC2N tracks a pending c2n request to nodeID and returns its ID
// and a channel for the node's raw HTTP response. Callers must defer
// [State.CancelC2N] or read the channel within their own timeout; there
// is no server-side TTL.
func (s *State) RegisterC2N(nodeID types.NodeID) (string, <-chan []byte) {
return s.pings.registerC2N(nodeID)
}
// C2NTarget returns the node a pending c2n request was sent to, so the
// response handler can check that the answer comes from that node.
func (s *State) C2NTarget(c2nID string) (types.NodeID, bool) {
return s.pings.c2nTarget(c2nID)
}
// CompleteC2N delivers a c2n response. Returns true if the ID was known,
// false otherwise.
func (s *State) CompleteC2N(c2nID string, body []byte) bool {
return s.pings.completeC2N(c2nID, body)
}
// CancelC2N removes a pending c2n request. Idempotent.
func (s *State) CancelC2N(c2nID string) {
s.pings.cancelC2N(c2nID)
}
+71
View File
@@ -172,3 +172,74 @@ func TestPingTracker_LatencyNonNegative(t *testing.T) {
t.Fatal("timed out")
}
}
func TestPingTracker_C2NRegisterComplete(t *testing.T) {
pt := newPingTracker()
c2nID, ch := pt.registerC2N(types.NodeID(7))
assert.NotEmpty(t, c2nID)
nodeID, ok := pt.c2nTarget(c2nID)
require.True(t, ok)
assert.Equal(t, types.NodeID(7), nodeID)
assert.True(t, pt.completeC2N(c2nID, []byte("HTTP/1.1 200 OK\r\n\r\n")))
select {
case body, ok := <-ch:
require.True(t, ok)
assert.Equal(t, "HTTP/1.1 200 OK\r\n\r\n", string(body))
case <-time.After(time.Second):
t.Fatal("timed out waiting for c2n response")
}
_, ok = pt.c2nTarget(c2nID)
assert.False(t, ok, "completed c2n request should be forgotten")
assert.False(t, pt.completeC2N(c2nID, nil), "second complete should return false")
}
func TestPingTracker_C2NCancel(t *testing.T) {
pt := newPingTracker()
c2nID, ch := pt.registerC2N(types.NodeID(1))
pt.cancelC2N(c2nID)
pt.cancelC2N(c2nID)
_, ok := pt.c2nTarget(c2nID)
assert.False(t, ok)
assert.False(t, pt.completeC2N(c2nID, []byte("late")))
select {
case <-ch:
t.Fatal("channel should not receive after cancel")
case <-time.After(50 * time.Millisecond):
// Expected: no response.
}
}
func TestPingTracker_C2NSeparateFromPings(t *testing.T) {
pt := newPingTracker()
pingID, _ := pt.register(types.NodeID(1))
c2nID, _ := pt.registerC2N(types.NodeID(1))
assert.False(t, pt.completeC2N(pingID, nil), "ping ID must not complete a c2n request")
assert.False(t, pt.complete(c2nID), "c2n ID must not complete a ping")
}
func TestPingTracker_DrainC2N(t *testing.T) {
pt := newPingTracker()
_, ch := pt.registerC2N(types.NodeID(1))
pt.drain()
select {
case _, ok := <-ch:
assert.False(t, ok, "c2n channel should be closed, got value")
case <-time.After(time.Second):
t.Fatal("c2n channel not closed by drain")
}
assert.Empty(t, pt.c2n, "c2n map should be empty after drain")
}