forked from capnproto/go-capnp
/
tables.go
255 lines (231 loc) · 5.22 KB
/
tables.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package rpc
import (
"errors"
"github.com/MadBase/go-capnproto2/v2"
"github.com/MadBase/go-capnproto2/v2/rpc/internal/refcount"
)
// Table IDs
type (
questionID uint32
answerID uint32
exportID uint32
importID uint32
embargoID uint32
)
// impent is an entry in the import table.
type impent struct {
rc *refcount.RefCount
refs int
}
// addImport increases the counter of the times the import ID was sent to this vat.
func (c *Conn) addImport(id importID) capnp.Client {
if c.imports == nil {
c.imports = make(map[importID]*impent)
} else if ent := c.imports[id]; ent != nil {
ent.refs++
return ent.rc.Ref()
}
client := &importClient{
id: id,
conn: c,
}
rc, ref := refcount.New(client)
c.imports[id] = &impent{rc: rc, refs: 1}
return ref
}
// popImport removes the import ID and returns the number of times the import ID was sent to this vat.
func (c *Conn) popImport(id importID) (refs int) {
if c.imports == nil {
return 0
}
ent := c.imports[id]
if ent == nil {
return 0
}
refs = ent.refs
delete(c.imports, id)
return refs
}
// An importClient implements capnp.Client for a remote capability.
type importClient struct {
id importID
conn *Conn
closed bool // protected by conn.mu
}
func (ic *importClient) Call(cl *capnp.Call) capnp.Answer {
select {
case <-ic.conn.mu:
if err := ic.conn.startWork(); err != nil {
return capnp.ErrorAnswer(err)
}
case <-cl.Ctx.Done():
return capnp.ErrorAnswer(cl.Ctx.Err())
}
ans := ic.lockedCall(cl)
ic.conn.workers.Done()
ic.conn.mu.Unlock()
return ans
}
// lockedCall is equivalent to Call but assumes that the caller is
// already holding onto ic.conn.mu.
func (ic *importClient) lockedCall(cl *capnp.Call) capnp.Answer {
if ic.closed {
return capnp.ErrorAnswer(errImportClosed)
}
q := ic.conn.newQuestion(cl.Ctx, &cl.Method)
msg := newMessage(nil)
msgCall, _ := msg.NewCall()
msgCall.SetQuestionId(uint32(q.id))
msgCall.SetInterfaceId(cl.Method.InterfaceID)
msgCall.SetMethodId(cl.Method.MethodID)
target, _ := msgCall.NewTarget()
target.SetImportedCap(uint32(ic.id))
payload, _ := msgCall.NewParams()
if err := ic.conn.fillParams(payload, cl); err != nil {
ic.conn.popQuestion(q.id)
return capnp.ErrorAnswer(err)
}
select {
case ic.conn.out <- msg:
case <-cl.Ctx.Done():
ic.conn.popQuestion(q.id)
return capnp.ErrorAnswer(cl.Ctx.Err())
case <-ic.conn.bg.Done():
ic.conn.popQuestion(q.id)
return capnp.ErrorAnswer(ErrConnClosed)
}
q.start()
return q
}
func (ic *importClient) Close() error {
ic.conn.mu.Lock()
if err := ic.conn.startWork(); err != nil {
ic.conn.mu.Unlock()
return err
}
closed := ic.closed
var i int
if !closed {
i = ic.conn.popImport(ic.id)
ic.closed = true
}
ic.conn.workers.Done()
ic.conn.mu.Unlock()
if closed {
return errImportClosed
}
if i == 0 {
return nil
}
msg := newMessage(nil)
mr, err := msg.NewRelease()
if err != nil {
return err
}
mr.SetId(uint32(ic.id))
mr.SetReferenceCount(uint32(i))
select {
case ic.conn.out <- msg:
return nil
case <-ic.conn.bg.Done():
return ErrConnClosed
}
}
type export struct {
id exportID
rc *refcount.RefCount
client capnp.Client
wireRefs int
}
func (c *Conn) findExport(id exportID) *export {
if int(id) >= len(c.exports) {
return nil
}
return c.exports[id]
}
// addExport ensures that the client is present in the table, returning its ID.
// If the client is already in the table, the previous ID is returned.
func (c *Conn) addExport(client capnp.Client) exportID {
for i, e := range c.exports {
if e != nil && isSameClient(e.rc.Client, client) {
e.wireRefs++
return exportID(i)
}
}
id := exportID(c.exportID.next())
rc, client := refcount.New(client)
export := &export{
id: id,
rc: rc,
client: client,
wireRefs: 1,
}
if int(id) == len(c.exports) {
c.exports = append(c.exports, export)
} else {
c.exports[id] = export
}
return id
}
func (c *Conn) releaseExport(id exportID, refs int) {
e := c.findExport(id)
if e == nil {
return
}
e.wireRefs -= refs
if e.wireRefs > 0 {
return
}
if e.wireRefs < 0 {
c.errorf("warning: export %v has negative refcount (%d)", id, e.wireRefs)
}
if err := e.client.Close(); err != nil {
c.errorf("export %v close: %v", id, err)
}
c.exports[id] = nil
c.exportID.remove(uint32(id))
}
type embargo <-chan struct{}
func (c *Conn) newEmbargo() (embargoID, embargo) {
id := embargoID(c.embargoID.next())
e := make(chan struct{})
if int(id) == len(c.embargoes) {
c.embargoes = append(c.embargoes, e)
} else {
c.embargoes[id] = e
}
return id, e
}
func (c *Conn) disembargo(id embargoID) {
if int(id) >= len(c.embargoes) {
return
}
e := c.embargoes[id]
if e == nil {
return
}
close(e)
c.embargoes[id] = nil
c.embargoID.remove(uint32(id))
}
// idgen returns a sequence of monotonically increasing IDs with
// support for replacement. The zero value is a generator that
// starts at zero.
type idgen struct {
i uint32
free []uint32
}
func (gen *idgen) next() uint32 {
if n := len(gen.free); n > 0 {
i := gen.free[n-1]
gen.free = gen.free[:n-1]
return i
}
i := gen.i
gen.i++
return i
}
func (gen *idgen) remove(i uint32) {
gen.free = append(gen.free, i)
}
var errImportClosed = errors.New("rpc: call on closed import")