Something went wrong. Try again.
Cloudflare Cap'n'Web Go implementation
Something went wrong. Try again.
capnweb-go table.go
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195package capnweb
import "sync"
// ImportTable tracks objects imported from the remote endpoint.// The importing side allocates positive IDs starting from 1.type ImportTable struct { mu sync.Mutex nextID int64 entries map[int64]*ImportEntry}
// ImportEntry represents a single import table slot.type ImportEntry struct { ID int64 RefCount int64 // times this import was "introduced" to us Resolved bool Value any // resolved value, or nil if pending}
// NewImportTable creates an import table. ID zero is pre-populated as the// remote's bootstrap (main) interface.func NewImportTable() *ImportTable { return &ImportTable{ nextID: 1, entries: map[int64]*ImportEntry{ 0: {ID: 0, RefCount: 1}, }, }}
// Allocate reserves the next positive import ID and returns the entry.// Used when sending push/stream/pipe.func (t *ImportTable) Allocate() *ImportEntry { t.mu.Lock() defer t.mu.Unlock()
id := t.nextID t.nextID++ e := &ImportEntry{ID: id, RefCount: 1} t.entries[id] = e return e}
// Get returns the entry for the given import ID, or nil if not found.func (t *ImportTable) Get(id int64) *ImportEntry { t.mu.Lock() defer t.mu.Unlock() return t.entries[id]}
// AddRef increments the refcount for an import (called when we receive the// same ID again via an export/promise expression from the remote).func (t *ImportTable) AddRef(id int64) { t.mu.Lock() defer t.mu.Unlock() if e, ok := t.entries[id]; ok { e.RefCount++ }}
// Insert adds an entry for a remotely-chosen (negative) import ID.// Used when the remote exports an object to us via ["export", negativeId].func (t *ImportTable) Insert(id int64) *ImportEntry { t.mu.Lock() defer t.mu.Unlock()
if e, ok := t.entries[id]; ok { e.RefCount++ return e } e := &ImportEntry{ID: id, RefCount: 1} t.entries[id] = e return e}
// Remove deletes an entry from the table. Called after we send a release// message for this import.func (t *ImportTable) Remove(id int64) { t.mu.Lock() defer t.mu.Unlock() delete(t.entries, id)}
// Release decrements the refcount for an import by count. If the refcount// reaches zero, the entry is removed and true is returned.func (t *ImportTable) Release(id, count int64) bool { t.mu.Lock() defer t.mu.Unlock()
e, ok := t.entries[id] if !ok { return false } e.RefCount -= count if e.RefCount <= 0 { delete(t.entries, id) return true } return false}
// ExportTable tracks objects exported to the remote endpoint.// The exporting side allocates negative IDs starting from -1.type ExportTable struct { mu sync.Mutex nextID int64 entries map[int64]*ExportEntry targets map[any]int64 // target → ID for deduplication}
// ExportEntry represents a single export table slot.type ExportEntry struct { ID int64 Target any RefCount int64 // times we've exported this ID}
// NewExportTable creates an export table. The bootstrap object is placed at// ID zero.func NewExportTable(main any) *ExportTable { t := &ExportTable{ nextID: -1, entries: map[int64]*ExportEntry{}, targets: map[any]int64{}, } if main != nil { t.entries[0] = &ExportEntry{ID: 0, Target: main, RefCount: 1} t.targets[main] = 0 } return t}
// Export exports a target object. If the same target was already exported,// its existing ID is reused and the refcount is incremented. Returns the// export entry.func (t *ExportTable) Export(target any) *ExportEntry { t.mu.Lock() defer t.mu.Unlock()
if id, ok := t.targets[target]; ok { e := t.entries[id] e.RefCount++ return e }
id := t.nextID t.nextID-- e := &ExportEntry{ID: id, Target: target, RefCount: 1} t.entries[id] = e t.targets[target] = id return e}
// ExportWithID registers an export at a specific ID (used for result exports// where the remote chose the positive ID via push/stream/pipe).func (t *ExportTable) ExportWithID(id int64, target any) *ExportEntry { t.mu.Lock() defer t.mu.Unlock()
if e, ok := t.entries[id]; ok { e.RefCount++ return e } e := &ExportEntry{ID: id, Target: target, RefCount: 1} t.entries[id] = e return e}
// Get returns the entry for the given export ID, or nil if not found.func (t *ExportTable) Get(id int64) *ExportEntry { t.mu.Lock() defer t.mu.Unlock() return t.entries[id]}
// HandleRelease decrements the refcount for an export by the given amount.// If the refcount reaches zero, the entry is removed and true is returned.func (t *ExportTable) HandleRelease(id, refcount int64) bool { t.mu.Lock() defer t.mu.Unlock()
e, ok := t.entries[id] if !ok { return false } e.RefCount -= refcount if e.RefCount <= 0 { delete(t.entries, id) delete(t.targets, e.Target) return true } return false}