-
-
Notifications
You must be signed in to change notification settings - Fork 110
/
capability.go
1173 lines (1044 loc) · 32.9 KB
/
capability.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package capnp
import (
"context"
"errors"
"runtime"
"strconv"
"sync"
"capnproto.org/go/capnp/v3/exc"
"capnproto.org/go/capnp/v3/exp/bufferpool"
"capnproto.org/go/capnp/v3/flowcontrol"
"capnproto.org/go/capnp/v3/internal/str"
"capnproto.org/go/capnp/v3/util/deferred"
"capnproto.org/go/capnp/v3/util/maybe"
"capnproto.org/go/capnp/v3/util/rc"
"capnproto.org/go/capnp/v3/util/sync/mutex"
)
func init() {
close(closedSignal)
}
// An Interface is a reference to a client in a message's capability table.
type Interface struct {
seg *Segment
cap CapabilityID
}
// i.EncodeAsPtr is equivalent to i.ToPtr(); for implementing TypeParam.
// The segment argument is ignored.
func (i Interface) EncodeAsPtr(*Segment) Ptr { return i.ToPtr() }
// DecodeFromPtr(p) is equivalent to p.Interface(); for implementing TypeParam.
func (Interface) DecodeFromPtr(p Ptr) Interface { return p.Interface() }
var _ TypeParam[Interface] = Interface{}
// NewInterface creates a new interface pointer.
//
// No allocation is performed in the given segment: it is used purely
// to associate the interface pointer with a message.
func NewInterface(s *Segment, cap CapabilityID) Interface {
return Interface{s, cap}
}
// ToPtr converts the interface to a generic pointer.
func (i Interface) ToPtr() Ptr {
return Ptr{
seg: i.seg,
lenOrCap: uint32(i.cap),
flags: interfacePtrFlag,
}
}
// Message returns the message whose capability table the interface
// references or nil if the pointer is invalid.
func (i Interface) Message() *Message {
if i.seg == nil {
return nil
}
return i.seg.Message()
}
// IsValid returns whether the interface is valid.
func (i Interface) IsValid() bool {
return i.seg != nil
}
// Capability returns the capability ID of the interface.
func (i Interface) Capability() CapabilityID {
return i.cap
}
// value returns a raw interface pointer with the capability ID.
func (i Interface) value(paddr address) rawPointer {
if i.seg == nil {
return 0
}
return rawInterfacePointer(i.cap)
}
// Client returns the client stored in the message's capability table
// or nil if the pointer is invalid.
func (i Interface) Client() (c Client) {
if msg := i.Message(); msg != nil {
c = msg.CapTable().Get(i)
}
return
}
// A CapabilityID is an index into a message's capability table.
type CapabilityID uint32
// String returns the ID in the format "capability X".
func (id CapabilityID) String() string {
return "capability " + str.Utod(id)
}
// GoString returns the ID as a Go expression.
func (id CapabilityID) GoString() string {
return "capnp.CapabilityID(" + str.Utod(id) + ")"
}
// A Client is a reference to a Cap'n Proto capability.
// The zero value is a null capability reference.
// It is safe to use from multiple goroutines.
type Client ClientKind
// The underlying type of Client. We expose this so that
// we can use ~ClientKind as a constraint in generics to
// capture any capability type.
type ClientKind = struct {
*client
}
type client struct {
state mutex.Mutex[clientState]
}
type clientState struct {
limiter flowcontrol.FlowLimiter
cursor *rc.Ref[clientCursor] // never nil
released bool
// extraReleasers are called by Release() if any exist.
extraReleasers []func()
stream struct {
err error // Last error from streaming calls.
wg sync.WaitGroup // Outstanding calls.
}
}
// clientCursor is an indirection pointing to a link in the resolution
// chain of clientHooks. Places that need to do path shortening should
// store one of these, rather than storing clientHook directly.
type clientCursor struct {
hook mutex.Mutex[*rc.Ref[clientHook]] // nil if resolved to nil or released
}
func newClientCursor(hook clientHook) *rc.Ref[clientCursor] {
hookRef := rc.NewRefInPlace(func(h *clientHook) func() {
*h = hook
return h.Release
})
return rc.NewRefInPlace(func(c *clientCursor) func() {
*c = clientCursor{hook: mutex.New(hookRef)}
return c.Release
})
}
// compress advances the hook referred to by this cursor as far
// as possible without blocking on a resolution.
func (c *clientCursor) compress() {
c.hook.With(func(hook **rc.Ref[clientHook]) {
for {
h := *hook
if h == nil {
return
}
res, ok := h.Value().resolution.Get()
if !ok {
return
}
l := res.Lock()
if !l.Value().isResolved() {
l.Unlock()
return
}
r := l.Value().resolvedHook
if r != nil {
r = r.AddRef()
}
l.Unlock()
h.Release()
*hook = r
}
})
}
func (c *clientCursor) Release() {
c.hook.With(func(hook **rc.Ref[clientHook]) {
(*hook).Release()
})
}
// clientHook is a reference-counted wrapper for a ClientHook.
// It is assumed that a clientHook's address uniquely identifies a hook,
// since they are only created in NewClient and NewPromisedClient.
type clientHook struct {
// ClientHook will never be nil and will not change for the lifetime of
// a clientHook.
ClientHook
// Place for callers to attach arbitrary metadata to the client.
metadata Metadata
// State of the promise's resolution. If this is absent, then
// this clientHook is not a promise.
resolution maybe.Maybe[*mutex.Mutex[resolveState]]
}
func (h *clientHook) Release() {
h.Shutdown()
r, ok := h.resolution.Get()
if ok {
r.With(func(s *resolveState) {
if s.isResolved() {
s.resolvedHook.Release()
}
})
}
}
type resolveState struct {
// resolved is closed after resolvedHook is set
resolved chan struct{}
// Valid only if resolved is closed.
resolvedHook *rc.Ref[clientHook]
}
// NewClient creates the first reference to a capability.
// If hook is nil, then NewClient returns nil.
//
// Typically the RPC system will create a client for the application.
// Most applications will not need to use this directly.
func NewClient(hook ClientHook) Client {
if hook == nil {
return Client{}
}
h := clientHook{
ClientHook: hook,
metadata: *NewMetadata(),
}
cs := mutex.New(clientState{cursor: newClientCursor(h)})
c := Client{client: &client{state: cs}}
setupLeakReporting(c)
return c
}
// NewPromisedClient creates the first reference to a capability that
// can resolve to a different capability. The hook will be shut down
// when the promise is resolved or the client has no more references,
// whichever comes first.
//
// Typically the RPC system will create a client for the application.
// Most applications will not need to use this directly.
func NewPromisedClient(hook ClientHook) (Client, Resolver[Client]) {
return newPromisedClient(hook)
}
// newPromisedClient is the same as NewPromisedClient, but the return
// value exposes the concrete type of the resolver.
func newPromisedClient(hook ClientHook) (Client, *clientPromise) {
if hook == nil {
panic("NewPromisedClient(nil)")
}
rs := mutex.New(resolveState{
resolved: make(chan struct{}),
})
cursor := newClientCursor(clientHook{
ClientHook: hook,
metadata: *NewMetadata(),
resolution: maybe.New(&rs),
})
cs := mutex.New(clientState{cursor: cursor})
c := Client{client: &client{state: cs}}
setupLeakReporting(c)
return c, &clientPromise{cursor: cursor.Weak()}
}
// startCall holds onto a hook to prevent it from shutting down until
// finish is called. It resolves the client's hook as much as possible
// first. The caller must not be holding onto c.mu.
func (c Client) startCall() (hook *rc.Ref[clientHook], resolved, released bool) {
if c.client == nil {
return nil, true, false
}
return mutex.With3(&c.state, func(c *clientState) (hook *rc.Ref[clientHook], resolved, released bool) {
if c.released || !c.cursor.IsValid() {
return nil, true, c.released
}
c.cursor.Value().compress()
hook, ok := mutex.With2(&c.cursor.Value().hook, func(h **rc.Ref[clientHook]) (*rc.Ref[clientHook], bool) {
ret := *h
if ret.IsValid() {
return ret.AddRef(), true
}
return nil, false
})
if !ok {
return nil, true, false
}
r, ok := hook.Value().resolution.Get()
if !ok {
return hook, true, false
}
resolved = mutex.With1(r, func(s *resolveState) bool {
return s.isResolved()
})
return hook, resolved, false
})
}
// Get the current flowcontrol.FlowLimiter used to manage flow control
// for this client.
func (c Client) GetFlowLimiter() flowcontrol.FlowLimiter {
return mutex.With1(&c.state, func(c *clientState) flowcontrol.FlowLimiter {
ret := c.limiter
if ret == nil {
ret = flowcontrol.NopLimiter
}
return ret
})
}
// Update the flowcontrol.FlowLimiter used to manage flow control for
// this client. This affects all future calls, but not calls already
// waiting to send. Passing nil sets the value to flowcontrol.NopLimiter,
// which is also the default.
//
// When .Release() is called on the client, it will call .Release() on
// the FlowLimiter in turn.
func (c Client) SetFlowLimiter(lim flowcontrol.FlowLimiter) {
c.state.With(func(c *clientState) {
c.limiter = lim
})
}
// SendCall allocates space for parameters, calls args.Place to fill out
// the parameters, then starts executing a method, returning an answer
// that will hold the result. The caller must call the returned release
// function when it no longer needs the answer's data.
//
// This method respects the flow control policy configured with SetFlowLimiter;
// it may block if the sender is sending too fast.
func (c Client) SendCall(ctx context.Context, s Send) (*Answer, ReleaseFunc) {
h, _, released := c.startCall()
defer h.Release()
if released {
return ErrorAnswer(s.Method, errors.New("call on released client")), func() {}
}
if h == nil {
return ErrorAnswer(s.Method, errors.New("call on null client")), func() {}
}
err := mutex.With1(&c.state, func(c *clientState) error {
return c.stream.err
})
if err != nil {
return ErrorAnswer(s.Method, exc.WrapError("stream error", err)), func() {}
}
limiter := c.GetFlowLimiter()
// We need to call PlaceArgs before we will know the size of message for
// flow control purposes, so wrap it in a function that measures after the
// arguments have been placed:
placeArgs := s.PlaceArgs
var size uint64
s.PlaceArgs = func(args Struct) error {
var err error
if placeArgs != nil {
err = placeArgs(args)
if err != nil {
return err
}
}
size, err = args.Segment().Message().TotalSize()
return err
}
ans, rel := h.Value().Send(ctx, s)
// FIXME: an earlier version of this code called StartMessage() from
// within PlaceArgs -- but that can result in a deadlock, since it means
// the client hook is holding a lock while we're waiting on the limiter.
//
// As a temporary workaround, we instead do StartMessage *after* the send.
// This still has a bug, but a much less serious one: we may slightly
// over-use our limit, but only by the size of a single message. This is
// mostly a problem in that it contradicts the documentation and is
// conceptually odd.
//
// Longer term, we should fix a more serious design problem: Send() is
// holding a lock while calling into user code (PlaceArgs), so this
// deadlock could also arise if the user code blocks. Once that is solved,
// we can back out this hack.
gotResponse, err := limiter.StartMessage(ctx, size)
if err != nil {
// HACK: An error should only happen if the context was cancelled,
// in which case the caller will notice it soon probably. The call
// still went off ok, so we can just return the result we already
// got, and trying to report the error is awkward because we can't
// return one... so we don't. Set gotResponse to something that won't
// break things, and call it a day. See comments above about a
// longer term solution to this mess.
gotResponse = func() {}
}
p := ans.f.promise
l := p.state.Lock()
if l.Value().isResolved() {
// Wow, that was fast.
l.Unlock()
gotResponse()
} else {
l.Value().signals = append(l.Value().signals, gotResponse)
l.Unlock()
}
return ans, rel
}
// SendStreamCall is like SendCall except that:
//
// 1. It does not return an answer for the eventual result.
// 2. If the call returns an error, all future calls on this
// client will return the same error (without starting
// the method or calling PlaceArgs).
func (c Client) SendStreamCall(ctx context.Context, s Send) error {
streamError := mutex.With1(&c.state, func(c *clientState) error {
err := c.stream.err
if err == nil {
c.stream.wg.Add(1)
}
return err
})
if streamError != nil {
return streamError
}
ans, release := c.SendCall(ctx, s)
go func() {
defer c.state.With(func(c *clientState) {
c.stream.wg.Done()
})
_, err := ans.Future().Ptr()
release()
if err != nil {
c.state.With(func(c *clientState) {
c.stream.err = err
})
}
}()
return nil
}
// WaitStreaming waits for all outstanding streaming calls (i.e. calls
// started with SendStreamCall) to complete, and then returns an error
// if any streaming call has failed.
func (c Client) WaitStreaming() error {
wg := mutex.With1(&c.state, func(c *clientState) *sync.WaitGroup {
return &c.stream.wg
})
wg.Wait()
return mutex.With1(&c.state, func(c *clientState) error {
return c.stream.err
})
}
// RecvCall starts executing a method with the referenced arguments
// and returns an answer that will hold the result. The hook will call
// a.Release when it no longer needs to reference the parameters. The
// caller must call the returned release function when it no longer
// needs the answer's data.
//
// Note that unlike SendCall, this method does *not* respect the flow
// control policy configured with SetFlowLimiter.
func (c Client) RecvCall(ctx context.Context, r Recv) PipelineCaller {
h, _, released := c.startCall()
defer h.Release()
if released {
r.Reject(errors.New("call on released client"))
return nil
}
if h == nil {
r.Reject(errors.New("call on null client"))
return nil
}
return h.Value().Recv(ctx, r)
}
// IsValid reports whether c is a valid reference to a capability.
// A reference is invalid if it is nil, has resolved to null, or has
// been released.
func (c Client) IsValid() bool {
h, _, released := c.startCall()
defer h.Release()
return !released && h != nil
}
// IsSame reports whether c and c2 refer to a capability created by the
// same call to NewClient. This can return false negatives if c or c2
// are not fully resolved: use Resolve if this is an issue. If either
// c or c2 are released, then IsSame panics.
func (c Client) IsSame(c2 Client) bool {
h1, _, released := c.startCall()
defer h1.Release()
if released {
panic("IsSame on released client")
}
h2, _, released := c2.startCall()
defer h2.Release()
if released {
panic("IsSame on released client")
}
valid1 := h1.IsValid()
valid2 := h2.IsValid()
if !valid1 && !valid2 {
return true
}
if !valid1 || !valid2 {
return false
}
return h1.Value() == h2.Value()
}
// Resolve blocks until the capability is fully resolved or the Context is Done.
// Resolve only returns an error if the context is canceled; it returns nil even
// if the capability resolves to an error.
func (c Client) Resolve(ctx context.Context) error {
h, resolved, released := c.startCall()
defer h.Release()
if released {
return errors.New("cannot resolve released client")
}
if resolved {
return nil
}
h, err := resolveClientHook(ctx, h)
h.Release()
return err
}
// AddRef creates a new Client that refers to the same capability as c.
// If c is nil or has resolved to null, then AddRef returns nil.
func (c Client) AddRef() Client {
if c.client == nil {
return Client{}
}
h, _, released := c.startCall()
defer h.Release()
if released {
panic("AddRef on released client")
}
return mutex.With1(&c.state, func(c *clientState) Client {
d := Client{client: &client{state: mutex.New(clientState{cursor: c.cursor.AddRef()})}}
setupLeakReporting(d)
return d
})
}
// WeakRef creates a new WeakClient that refers to the same capability
// as c. If c is nil or has resolved to null, then WeakRef returns nil.
func (c Client) WeakRef() WeakClient {
cursor := mutex.With1(&c.state, func(s *clientState) *rc.WeakRef[clientCursor] {
if s.released {
panic("WeakRef on released client")
}
return s.cursor.Weak()
})
return WeakClient{r: cursor}
}
// Snapshot reads the current state of the client. It returns the zero
// ClientSnapshot if c is nil, has resolved to null, or has been released.
func (c Client) Snapshot() ClientSnapshot {
h, _, _ := c.startCall()
s := ClientSnapshot{hook: h}
setupLeakReporting(s)
return s
}
// AttachReleaser attaches an additional releaser func to be called when c.Release()
// is called. Returns false if the client has already been released.
//
// MUST NOT be called with the client state mutex held.
func (c Client) AttachReleaser(f func()) (released bool) {
c.state.With(func(s *clientState) {
if s.released {
released = true
} else {
s.extraReleasers = append(s.extraReleasers, f)
}
})
return
}
// A Brand is an opaque value used to identify a capability.
type Brand struct {
Value any
}
// ClientSnapshot is a snapshot of a client's identity. If the Client
// is a promise, then the corresponding ClientSnapshot will *not*
// redirect to point at the resolution.
type ClientSnapshot struct {
hook *rc.Ref[clientHook]
}
func (cs ClientSnapshot) String() string {
if !cs.IsValid() {
return "ClientSnapshot{}"
}
return "ClientSnapshot{" + cs.hook.Value().String() + "}"
}
func (cs ClientSnapshot) IsValid() bool {
return cs.hook.IsValid()
}
// IsPromise returns true if the snapshot is a promise.
func (cs ClientSnapshot) IsPromise() bool {
if cs.hook == nil {
return false
}
_, ret := cs.hook.Value().resolution.Get()
return ret
}
// IsResolved returns true if the snapshot has resolved to its final value.
// If IsPromise() returns false, then this will also return false. Otherwise,
// it returns false before resolution and true afterwards.
func (cs ClientSnapshot) IsResolved() bool {
if cs.hook == nil {
return false
}
res, ok := cs.hook.Value().resolution.Get()
if !ok {
return false
}
return mutex.With1(res, func(s *resolveState) bool {
return s.isResolved()
})
}
// Send implements ClientHook.Send
func (cs ClientSnapshot) Send(ctx context.Context, s Send) (*Answer, ReleaseFunc) {
if cs.hook == nil {
return ErrorAnswer(s.Method, errors.New("call on null client")), func() {}
}
return cs.hook.Value().Send(ctx, s)
}
// Recv implements ClientHook.Recv
func (cs ClientSnapshot) Recv(ctx context.Context, r Recv) PipelineCaller {
if cs.hook == nil {
r.Reject(errors.New("call on null client"))
return nil
}
return cs.hook.Value().Recv(ctx, r)
}
// Client returns a client pointing at the most-resolved version of the snapshot.
func (cs ClientSnapshot) Client() Client {
cursor := rc.NewRefInPlace(func(c *clientCursor) func() {
*c = clientCursor{hook: mutex.New(cs.hook.AddRef())}
c.compress()
return c.Release
})
c := Client{client: &client{
state: mutex.New(clientState{cursor: cursor}),
}}
setupLeakReporting(c)
return c
}
// Brand is the value returned from the ClientHook's Brand method.
// Returns the zero Brand if the receiver is the zero ClientSnapshot.
func (cs ClientSnapshot) Brand() Brand {
if cs.hook == nil {
return Brand{}
}
return cs.hook.Value().Brand()
}
// Return a the reference to the Metadata associated with this client hook.
// Callers may store whatever they need here.
func (cs ClientSnapshot) Metadata() *Metadata {
return &cs.hook.Value().metadata
}
// Create a copy of the snapshot, with its own underlying reference.
func (cs ClientSnapshot) AddRef() ClientSnapshot {
cs.hook = cs.hook.AddRef()
setupLeakReporting(cs)
return cs
}
// Release the reference to the hook.
func (cs *ClientSnapshot) Release() {
cs.hook.Release()
}
// Shutdown is an alias for Release, to implement ClientHook.
func (cs *ClientSnapshot) Shutdown() {
cs.Release()
}
func (cs *ClientSnapshot) Resolve1(ctx context.Context) error {
var err error
cs.hook, _, err = resolve1ClientHook(ctx, cs.hook)
setupLeakReporting(*cs)
return err
}
func (cs *ClientSnapshot) resolve1(ctx context.Context) (more bool, err error) {
cs.hook, more, err = resolve1ClientHook(ctx, cs.hook)
return
}
func (cs *ClientSnapshot) Resolve(ctx context.Context) error {
var err error
cs.hook, err = resolveClientHook(ctx, cs.hook)
setupLeakReporting(*cs)
return err
}
func resolveClientHook(ctx context.Context, h *rc.Ref[clientHook]) (_ *rc.Ref[clientHook], err error) {
for {
var more bool
h, more, err = resolve1ClientHook(ctx, h)
if !more || err != nil {
return h, err
}
}
}
func resolve1ClientHook(ctx context.Context, h *rc.Ref[clientHook]) (_ *rc.Ref[clientHook], more bool, err error) {
if !h.IsValid() {
return h, false, nil
}
defer h.Release()
r, ok := h.Value().resolution.Get()
if !ok {
return h.AddRef(), false, nil
}
resolvedCh := mutex.With1(r, func(s *resolveState) <-chan struct{} {
return s.resolved
})
select {
case <-resolvedCh:
rh := mutex.With1(r, func(r *resolveState) *rc.Ref[clientHook] {
return r.resolvedHook
})
if rh == nil {
return nil, false, nil
}
return rh.AddRef(), true, nil
case <-ctx.Done():
return h.AddRef(), true, ctx.Err()
}
}
// String returns a string that identifies this capability for debugging
// purposes. Its format should not be depended on: in particular, it
// should not be used to compare clients. Use IsSame to compare clients
// for equality.
func (c Client) String() string {
if c.client == nil {
return "<nil>"
}
h, resolved, released := c.startCall()
defer h.Release()
if released {
return "<released client>"
}
if h == nil {
return "<nil>"
}
var s string
if resolved {
s = "<client " + h.Value().ClientHook.String() + ">"
} else {
s = "<unresolved client " + h.Value().ClientHook.String() + ">"
}
return s
}
// Release releases a capability reference. If this is the last
// reference to the capability, then the underlying resources associated
// with the capability will be released.
//
// Release has no effect if c has already been released, or if c is
// nil or resolved to null.
func (c Client) Release() {
if c.client == nil {
return
}
limiter := c.GetFlowLimiter()
c.state.With(func(s *clientState) {
if !s.released {
s.released = true
s.cursor.Release()
limiter.Release()
for i := range s.extraReleasers {
s.extraReleasers[i]()
}
}
})
}
func (c Client) EncodeAsPtr(seg *Segment) Ptr {
capId := seg.Message().CapTable().Add(c)
return NewInterface(seg, capId).ToPtr()
}
func (Client) DecodeFromPtr(p Ptr) Client {
return p.Interface().Client()
}
var _ TypeParam[Client] = Client{}
// isResolve reports whether the clientHook s belongs to is resolved.
func (s *resolveState) isResolved() bool {
select {
case <-s.resolved:
return true
default:
return false
}
}
var setupLeakReporting func(any) = func(any) {}
// SetClientLeakFunc sets a callback for reporting Clients that went
// out of scope without being released. The callback is not guaranteed
// to be called and must be safe to call concurrently from multiple
// goroutines. The exact format of the message is unspecified.
//
// SetClientLeakFunc must not be called after any calls to NewClient or
// NewPromisedClient.
func SetClientLeakFunc(clientLeakFunc func(msg string)) {
setupLeakReporting = func(v any) {
buf := bufferpool.Default.Get(1e6)
n := runtime.Stack(buf, false)
stack := string(buf[:n])
bufferpool.Default.Put(buf)
switch c := v.(type) {
case Client:
runtime.SetFinalizer(c.client, func(c *client) {
released := mutex.With1(&c.state, func(c *clientState) bool {
return c.released
})
if released {
return
}
clientLeakFunc("leaked client created at:\n\n" + stack)
})
case ClientSnapshot:
if !c.IsValid() {
return
}
runtime.SetFinalizer(c.hook, func(c *rc.Ref[clientHook]) {
if !c.IsValid() {
return
}
clientLeakFunc("leaked client snapshot created at:\n\n" + stack)
})
default:
panic("setupLeakReporting called on unrecognized type!")
}
}
}
// A ClientPromise resolves the identity of a client created by NewPromisedClient.
type clientPromise struct {
cursor *rc.WeakRef[clientCursor]
}
func (cp *clientPromise) Reject(err error) {
cp.Fulfill(ErrorClient(err))
}
// Fulfill resolves the client promise to c. After Fulfill returns,
// then all future calls to the client created by NewPromisedClient will
// be sent to c. It is guaranteed that the hook passed to
// NewPromisedClient will be shut down after Fulfill returns, but the
// hook may have been shut down earlier if the client ran out of
// references.
func (cp *clientPromise) Fulfill(c Client) {
dq := &deferred.Queue{}
defer dq.Run()
cp.fulfill(dq, c)
}
// fulfill is like Fulfill, except that it does not wait for outsanding calls
// to return answers or shut down the underlying hook; instead, it adds functions
// to do this to dq.
func (cp *clientPromise) fulfill(dq *deferred.Queue, c Client) {
cursor, ok := cp.cursor.AddRef()
if !ok {
return
}
dq.Defer(cursor.Release)
// Obtain next client hook.
var rh *rc.Ref[clientHook]
if (c != Client{}) {
h, _, released := c.startCall()
if released {
panic("ClientPromise.Fulfill with a released client")
}
rh = h
}
// Mark hook as resolved.
cursor.Value().hook.With(func(h **rc.Ref[clientHook]) {
r, ok := (*h).Value().resolution.Get()
if !ok {
panic("BUG: clientPromise referred to a clientHook that was not a promise")
}
r.With(func(s *resolveState) {
if s.isResolved() {
panic("ClientPromise.Fulfill called more than once")
}
s.resolvedHook = rh
close(s.resolved)
})
})
cursor.Value().compress()
}
// A WeakClient is a weak reference to a capability: it refers to a
// capability without preventing it from being shut down. The zero
// value is a null reference.
type WeakClient struct {
r *rc.WeakRef[clientCursor]
}
// AddRef creates a new Client that refers to the same capability as c
// as long as the capability hasn't already been shut down.
func (wc WeakClient) AddRef() (c Client, ok bool) {
if wc.r == nil {
return Client{}, true
}
cursor, ok := wc.r.AddRef()
if !ok {
return Client{}, false
}
c = Client{client: &client{state: mutex.New(clientState{cursor: cursor})}}
setupLeakReporting(c)
return c, true
}
// A ClientHook represents a Cap'n Proto capability. Application code
// should not pass around ClientHooks; applications should pass around
// Clients. A ClientHook must be safe to use from multiple goroutines.
//
// Calls must be delivered to the capability in the order they are made.
// This guarantee is based on the concept of a capability acknowledging
// delivery of a call: this is specific to an implementation of ClientHook.
// A type that implements ClientHook must guarantee that if foo() then bar()
// is called on a client, then the capability acknowledging foo() happens
// before the capability observing bar().
//
// ClientHook is an internal interface. Users generally SHOULD NOT supply
// their own implementations.
type ClientHook interface {
// Send allocates space for parameters, calls s.PlaceArgs to fill out
// the arguments, then starts executing a method, returning an answer
// that will hold the result. The hook must call s.PlaceArgs at most
// once, and if it does call s.PlaceArgs, it must return before Send
// returns. The caller must call the returned release function when
// it no longer needs the answer's data.
//
// Send is typically used when application code is making a call.
Send(ctx context.Context, s Send) (*Answer, ReleaseFunc)
// Recv starts executing a method with the referenced arguments
// and places the result in a message controlled by the caller.
// The hook will call r.ReleaseArgs when it no longer needs to
// reference the parameters and use r.Returner to complete the method
// call. If Recv does not call r.Returner.Return before it returns,
// then it must return a non-nil PipelineCaller.
//
// Recv is typically used when the RPC system has received a call.
Recv(ctx context.Context, r Recv) PipelineCaller
// Brand returns an implementation-specific value. This can be used
// to introspect and identify kinds of clients.
Brand() Brand
// Shutdown releases any resources associated with this capability.
// The behavior of calling any methods on the receiver after calling
// Shutdown is undefined. Shutdown must not interrupt any already
// outstanding calls.
Shutdown()
// String formats the hook as a string (same as fmt.Stringer)
String() string
}