Debian Codegraph @

internal/jsonrpc2_v2/conn.go

raw symbols SCIP
1 // Copyright 2018 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package jsonrpc2scip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/
6
7 import (
8 "contextscip-go gomod github.com/golang/go/src go1.25.0 context/"
9 "encoding/jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/"
10 "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/"
11 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
12 "ioscip-go gomod github.com/golang/go/src go1.25.0 io/"
13 "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/"
14 "sync/atomicscip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/"
15 )
16
17 // Binder builds a connection configuration.
18 // This may be used in servers to generate a new configuration per connection.
19 // ConnectionOptions itself implements Binder returning itself unmodified, to
20 // allow for the simple cases where no per connection information is needed.
21 type Binderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Binder# interface {
22 // Bind returns the ConnectionOptions to use when establishing the passed-in
23 // Connection.
24 //
25 // The connection is not ready to use when Bind is called,
26 // but Bind may close it without reading or writing to it.
27 Bindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Binder#Bind.(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#
28 }
29
30 // A BinderFunc implements the Binder interface for a standalone Bind function.
31 type BinderFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/BinderFunc# func(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#
32
33 func (f BinderFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/BinderFunc#) Bindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/BinderFunc#Bind().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions# {
34 return f(ctx, c)
35 }
36
37 var _ Binderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Binder# = BinderFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/BinderFunc#(nil)
38
39 // ConnectionOptions holds the options for new connections.
40 type ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions# struct {
41 // Framer allows control over the message framing and encoding.
42 // If nil, HeaderFramer will be used.
43 Framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#Framer. Framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Framer#
44 // Preempter allows registration of a pre-queue message handler.
45 // If nil, no messages will be preempted.
46 Preempterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#Preempter. Preempterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Preempter#
47 // Handler is used as the queued message handler for inbound messages.
48 // If nil, all responses will be ErrNotHandled.
49 Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#Handler. Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Handler#
50 // OnInternalError, if non-nil, is called with any internal errors that occur
51 // while serving the connection, such as protocol errors or invariant
52 // violations. (If nil, internal errors result in panics.)
53 OnInternalErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#OnInternalError. func(error)
54 }
55
56 // Connection manages the jsonrpc2 protocol, connecting responses back to their
57 // calls.
58 // Connection is bidirectional; it does not have a designated server or client
59 // end.
60 type Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection# struct {
61 seqscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#seq. atomicscip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/.Int64scip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/Int64# // must only be accessed using atomic operations
62
63 stateMuscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#stateMu. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#
64 statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#state. inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState# // accessed only in updateInFlight
65 donescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#done. chan struct{} // closed (under stateMu) when state.closed is true and all goroutines have completed
66
67 writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#writer. chan Writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Writer# // 1-buffered; stores the writer when not in use
68
69 handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#handler. Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Handler#
70
71 onInternalErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#onInternalError. func(error)
72 onDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#onDone. func()
73 }
74
75 // inFlightState records the state of the incoming and outgoing calls on a
76 // Connection.
77 type inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState# struct {
78 connClosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#connClosing. bool // true when the Connection's Close method has been called
79 readingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#reading. bool // true while the readIncoming goroutine is running
80 readErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#readErr. error // non-nil when the readIncoming goroutine exits (typically io.EOF)
81 writeErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#writeErr. error // non-nil if a call to the Writer has failed with a non-canceled Context
82
83 // closer shuts down and cleans up the Reader and Writer state, ideally
84 // interrupting any Read or Write call that is currently blocked. It is closed
85 // when the state is idle and one of: connClosing is true, readErr is non-nil,
86 // or writeErr is non-nil.
87 //
88 // After the closer has been invoked, the closer field is set to nil
89 // and the closeErr field is simultaneously set to its result.
90 closerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#closer. ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Closerscip-go gomod github.com/golang/go/src go1.25.0 io/Closer#
91 closeErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#closeErr. error // error returned from closer.Close
92
93 outgoingCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingCalls. map[IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ID#]*AsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall# // calls only
94 outgoingNotificationsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingNotifications. int // # of notifications awaiting "write"
95
96 // incoming stores the total number of incoming calls and notifications
97 // that have not yet written or processed a result.
98 incomingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incoming. int
99
100 incomingByIDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incomingByID. map[IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ID#]*incomingRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest# // calls only
101
102 // handlerQueue stores the backlog of calls and notifications that were not
103 // already handled by a preempter.
104 // The queue does not include the request currently being handled (if any).
105 handlerQueuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#handlerQueue. []*incomingRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#
106 handlerRunningscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#handlerRunning. bool
107 }
108
109 // updateInFlight locks the state of the connection's in-flight requests, allows
110 // f to mutate that state, and closes the connection if it is idle and either
111 // is closing or has a read or write error.
112 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(f func(*inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#)) {
113 c.stateMuscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#stateMu..Lockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Lock().()
114 defer c.stateMuscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#stateMu..Unlockscip-go gomod github.com/golang/go/src go1.25.0 sync/Mutex#Unlock().()
115
116 s := &c.statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#state.
117
118 f(s)
119
120 select {
121 case <-c.donescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#done.:
122 // The connection was already completely done at the start of this call to
123 // updateInFlight, so it must remain so. (The call to f should have noticed
124 // that and avoided making any updates that would cause the state to be
125 // non-idle.)
126 if !s.idlescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#idle().() {
127 panic("jsonrpc2_v2: updateInFlight transitioned to non-idle when already done")
128 }
129 return
130 default:
131 }
132
133 if s.idlescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#idle().() && s.shuttingDownscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#shuttingDown().(ErrUnknownscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrUnknown.) != nil {
134 if s.closerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#closer. != nil {
135 s.closeErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#closeErr. = s.closerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#closer..Closescip-go gomod github.com/golang/go/src go1.25.0 io/Closer#Close().()
136 s.closerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#closer. = nil // prevent duplicate Close calls
137 }
138 if s.readingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#reading. {
139 // The readIncoming goroutine is still running. Our call to Close should
140 // cause it to exit soon, at which point it will make another call to
141 // updateInFlight, set s.reading to false, and mark the Connection done.
142 } else {
143 // The readIncoming goroutine has exited, or never started to begin with.
144 // Since everything else is idle, we're completely done.
145 if c.onDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#onDone. != nil {
146 c.onDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#onDone.()
147 }
148 close(c.donescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#done.)
149 }
150 }
151 }
152
153 // idle reports whether the connection is in a state with no pending calls or
154 // notifications.
155 //
156 // If idle returns true, the readIncoming goroutine may still be running,
157 // but no other goroutines are doing work on behalf of the connection.
158 func (s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) idlescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#idle().() bool {
159 return len(s.outgoingCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingCalls.) == 0 && s.outgoingNotificationsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingNotifications. == 0 && s.incomingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incoming. == 0 && !s.handlerRunningscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#handlerRunning.
160 }
161
162 // shuttingDown reports whether the connection is in a state that should
163 // disallow new (incoming and outgoing) calls. It returns either nil or
164 // an error that is or wraps the provided errClosing.
165 func (s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) shuttingDownscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#shuttingDown().(errClosing error) error {
166 if s.connClosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#connClosing. {
167 // If Close has been called explicitly, it doesn't matter what state the
168 // Reader and Writer are in: we shouldn't be starting new work because the
169 // caller told us not to start new work.
170 return errClosing
171 }
172 if s.readErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#readErr. != nil {
173 // If the read side of the connection is broken, we cannot read new call
174 // requests, and cannot read responses to our outgoing calls.
175 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%w: %v", errClosing, s.readErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#readErr.)
176 }
177 if s.writeErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#writeErr. != nil {
178 // If the write side of the connection is broken, we cannot write responses
179 // for incoming calls, and cannot write requests for outgoing calls.
180 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%w: %v", errClosing, s.writeErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#writeErr.)
181 }
182 return nil
183 }
184
185 // incomingRequest is used to track an incoming request as it is being handled
186 type incomingRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest# struct {
187 *Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#Request. // the request being processed
188 ctxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#ctx. contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#
189 cancelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#cancel. contextscip-go gomod github.com/golang/go/src go1.25.0 context/.CancelFuncscip-go gomod github.com/golang/go/src go1.25.0 context/CancelFunc#
190 }
191
192 // Bind returns the options unmodified.
193 func (o ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#) Bindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#Bind().(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) ConnectionOptionsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions# {
194 return o
195 }
196
197 // A ConnectionConfig configures a bidirectional jsonrpc2 connection.
198 type ConnectionConfigscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig# struct {
199 Readerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#Reader. Readerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Reader# // required
200 Writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#Writer. Writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Writer# // required
201 Closerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#Closer. ioscip-go gomod github.com/golang/go/src go1.25.0 io/.Closerscip-go gomod github.com/golang/go/src go1.25.0 io/Closer# // required
202 Preempterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#Preempter. Preempterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Preempter# // optional
203 Bindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#Bind. func(*Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Handler# // required
204 OnDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#OnDone. func() // optional
205 OnInternalErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#OnInternalError. func(error) // optional
206 }
207
208 // NewConnection creates a new [Connection] object and starts processing
209 // incoming messages.
210 func NewConnectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NewConnection().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, cfg ConnectionConfigscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#) *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection# {
211 ctx = contextscip-go gomod github.com/golang/go/src go1.25.0 context/.WithoutCancelscip-go gomod github.com/golang/go/src go1.25.0 context/WithoutCancel().(ctx)
212
213 c := &Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#{
214 statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#state.: inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#{closerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#closer.: cfg.Closerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#Closer.},
215 donescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#done.: make(chan struct{}),
216 writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#writer.: make(chan Writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Writer#, 1),
217 onDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#onDone.: cfg.OnDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#OnDone.,
218 onInternalErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#onInternalError.: cfg.OnInternalErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#OnInternalError.,
219 }
220 c.handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#handler. = cfg.Bindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#Bind.(c)
221 c.writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#writer. <- cfg.Writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#Writer.
222 c.startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#start().(ctx, cfg.Readerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#Reader., cfg.Preempterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionConfig#Preempter.)
223 return c
224 }
225
226 // bindConnection creates a new connection and runs it.
227 //
228 // This is used by the Dial and Serve functions to build the actual connection.
229 //
230 // The connection is closed automatically (and its resources cleaned up) when
231 // the last request has completed after the underlying ReadWriteCloser breaks,
232 // but it may be stopped earlier by calling Close (for a clean shutdown).
233 func bindConnectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/bindConnection().(bindCtx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, rwc ioscip-go gomod github.com/golang/go/src go1.25.0 io/.ReadWriteCloserscip-go gomod github.com/golang/go/src go1.25.0 io/ReadWriteCloser#, binder Binderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Binder#, onDone func()) *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection# {
234 // TODO: Should we create a new event span here?
235 // This will propagate cancellation from ctx; should it?
236 ctx := contextscip-go gomod github.com/golang/go/src go1.25.0 context/.WithoutCancelscip-go gomod github.com/golang/go/src go1.25.0 context/WithoutCancel().(bindCtx)
237
238 c := &Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#{
239 statescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#state.: inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#{closerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#closer.: rwc},
240 donescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#done.: make(chan struct{}),
241 writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#writer.: make(chan Writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Writer#, 1),
242 onDonescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#onDone.: onDone,
243 }
244 // It's tempting to set a finalizer on c to verify that the state has gone
245 // idle when the connection becomes unreachable. Unfortunately, the Binder
246 // interface makes that unsafe: it allows the Handler to close over the
247 // Connection, which could create a reference cycle that would cause the
248 // Connection to become uncollectable.
249
250 options := binder.Bindscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Binder#Bind.(bindCtx, c)
251 framer := options.Framerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#Framer.
252 if framer == nil {
253 framer = HeaderFramerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/HeaderFramer().()
254 }
255 c.handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#handler. = options.Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#Handler.
256 if c.handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#handler. == nil {
257 c.handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#handler. = defaultHandlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/defaultHandler#{}
258 }
259 c.onInternalErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#onInternalError. = options.OnInternalErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#OnInternalError.
260
261 c.writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#writer. <- framer.Writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Framer#Writer.(rwc)
262 reader := framer.Readerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Framer#Reader.(rwc)
263 c.startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#start().(ctx, reader, options.Preempterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ConnectionOptions#Preempter.)
264 return c
265 }
266
267 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) startscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#start().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, reader Readerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Reader#, preempter Preempterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Preempter#) {
268 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
269 select {
270 case <-c.donescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#done.:
271 // Bind already closed the connection; don't start a goroutine to read it.
272 return
273 default:
274 }
275
276 // The goroutine started here will continue until the underlying stream is closed.
277 //
278 // (If the Binder closed the Connection already, this should error out and
279 // return almost immediately.)
280 s.readingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#reading. = true
281 go c.readIncomingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#readIncoming().(ctx, reader, preempter)
282 })
283 }
284
285 // Notify invokes the target method but does not wait for a response.
286 // The params will be marshaled to JSON before sending over the wire, and will
287 // be handed to the method invoked.
288 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) Notifyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Notify().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, method string, params any) (err error) {
289 attempted := false
290
291 defer func() {
292 if attempted {
293 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
294 s.outgoingNotificationsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingNotifications.--
295 })
296 }
297 }()
298
299 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
300 // If the connection is shutting down, allow outgoing notifications only if
301 // there is at least one call still in flight. The number of calls in flight
302 // cannot increase once shutdown begins, and allowing outgoing notifications
303 // may permit notifications that will cancel in-flight calls.
304 if len(s.outgoingCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingCalls.) == 0 && len(s.incomingByIDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incomingByID.) == 0 {
305 err = s.shuttingDownscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#shuttingDown().(ErrClientClosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrClientClosing.)
306 if err != nil {
307 return
308 }
309 }
310 s.outgoingNotificationsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingNotifications.++
311 attempted = true
312 })
313 if err != nil {
314 return err
315 }
316
317 notify, err := NewNotificationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NewNotification().(method, params)
318 if err != nil {
319 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("marshaling notify parameters: %v", err)
320 }
321
322 return c.writescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#write().(ctx, notify)
323 }
324
325 // Call invokes the target method and returns an object that can be used to await the response.
326 // The params will be marshaled to JSON before sending over the wire, and will
327 // be handed to the method invoked.
328 // You do not have to wait for the response, it can just be ignored if not needed.
329 // If sending the call failed, the response will be ready and have the error in it.
330 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) Callscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Call().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, method string, params any) *AsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall# {
331 // Generate a new request identifier.
332 id := Int64IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Int64ID().(c.seqscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#seq..Addscip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/Int64#Add().(1))
333
334 ac := &AsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#{
335 idscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#id.: id,
336 readyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#ready.: make(chan struct{}),
337 }
338 // When this method returns, either ac is retired, or the request has been
339 // written successfully and the call is awaiting a response (to be provided by
340 // the readIncoming goroutine).
341
342 call, err := NewCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NewCall().(ac.idscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#id., method, params)
343 if err != nil {
344 ac.retirescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#retire().(&Responsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#{IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#ID.: id, Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#Error.: fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("marshaling call parameters: %w", err)})
345 return ac
346 }
347
348 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
349 err = s.shuttingDownscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#shuttingDown().(ErrClientClosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrClientClosing.)
350 if err != nil {
351 return
352 }
353 if s.outgoingCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingCalls. == nil {
354 s.outgoingCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingCalls. = make(map[IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ID#]*AsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#)
355 }
356 s.outgoingCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingCalls.[ac.idscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#id.] = ac
357 })
358 if err != nil {
359 ac.retirescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#retire().(&Responsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#{IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#ID.: id, Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#Error.: err})
360 return ac
361 }
362
363 if err := c.writescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#write().(ctx, call); err != nil {
364 // Sending failed. We will never get a response, so deliver a fake one if it
365 // wasn't already retired by the connection breaking.
366 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
367 if s.outgoingCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingCalls.[ac.idscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#id.] == ac {
368 delete(s.outgoingCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingCalls., ac.idscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#id.)
369 ac.retirescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#retire().(&Responsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#{IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#ID.: id, Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#Error.: err})
370 } else {
371 // ac was already retired by the readIncoming goroutine:
372 // perhaps our write raced with the Read side of the connection breaking.
373 }
374 })
375 }
376 return ac
377 }
378
379 type AsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall# struct {
380 idscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#id. IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ID#
381 readyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#ready. chan struct{} // closed after response has been set
382 responsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#response. *Responsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#
383 }
384
385 // ID used for this call.
386 // This can be used to cancel the call if needed.
387 func (ac *AsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#) IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#ID().() IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ID# { return ac.idscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#id. }
388
389 // IsReady can be used to check if the result is already prepared.
390 // This is guaranteed to return true on a result for which Await has already
391 // returned, or a call that failed to send in the first place.
392 func (ac *AsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#) IsReadyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#IsReady().() bool {
393 select {
394 case <-ac.readyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#ready.:
395 return true
396 default:
397 return false
398 }
399 }
400
401 // retire processes the response to the call.
402 func (ac *AsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#) retirescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#retire().(response *Responsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#) {
403 select {
404 case <-ac.readyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#ready.:
405 panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("jsonrpc2: retire called twice for ID %v", ac.idscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#id.))
406 default:
407 }
408
409 ac.responsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#response. = response
410 close(ac.readyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#ready.)
411 }
412
413 // Await waits for (and decodes) the results of a Call.
414 // The response will be unmarshaled from JSON into the result.
415 func (ac *AsyncCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#) Awaitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#Await().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, result any) error {
416 select {
417 case <-ctx.Donescip-go gomod github.com/golang/go/src go1.25.0 context/Context#Done().():
418 return ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().()
419 case <-ac.readyscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#ready.:
420 }
421 if ac.responsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#response..Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#Error. != nil {
422 return ac.responsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#response..Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#Error.
423 }
424 if result == nil {
425 return nil
426 }
427 return jsonscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.25.0 `encoding/json`/Unmarshal().(ac.responsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#response..Resultscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#Result., result)
428 }
429
430 // Respond delivers a response to an incoming Call.
431 //
432 // Respond must be called exactly once for any message for which a handler
433 // returns ErrAsyncResponse. It must not be called for any other message.
434 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) Respondscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Respond().(id IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ID#, result any, err error) error {
435 var req *incomingRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#
436 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
437 req = s.incomingByIDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incomingByID.[id]
438 })
439 if req == nil {
440 return c.internalErrorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#internalErrorf().("Request not found for ID %v", id)
441 }
442
443 if err == ErrAsyncResponsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrAsyncResponse. {
444 // Respond is supposed to supply the asynchronous response, so it would be
445 // confusing to call Respond with an error that promises to call Respond
446 // again.
447 err = c.internalErrorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#internalErrorf().("Respond called with ErrAsyncResponse for %q", req.Methodscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#Method.)
448 }
449 return c.processResultscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#processResult().("Respond", req, result, err)
450 }
451
452 // Cancel cancels the Context passed to the Handle call for the inbound message
453 // with the given ID.
454 //
455 // Cancel will not complain if the ID is not a currently active message, and it
456 // will not cause any messages that have not arrived yet with that ID to be
457 // cancelled.
458 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) Cancelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Cancel().(id IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ID#) {
459 var req *incomingRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#
460 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
461 req = s.incomingByIDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incomingByID.[id]
462 })
463 if req != nil {
464 req.cancelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#cancel.()
465 }
466 }
467
468 // Wait blocks until the connection is fully closed, but does not close it.
469 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) Waitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Wait().() error {
470 var err error
471 <-c.donescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#done.
472 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
473 err = s.closeErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#closeErr.
474 })
475 return err
476 }
477
478 // Close stops accepting new requests, waits for in-flight requests and enqueued
479 // Handle calls to complete, and then closes the underlying stream.
480 //
481 // After the start of a Close, notification requests (that lack IDs and do not
482 // receive responses) will continue to be passed to the Preempter, but calls
483 // with IDs will receive immediate responses with ErrServerClosing, and no new
484 // requests (not even notifications!) will be enqueued to the Handler.
485 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Close().() error {
486 // Stop handling new requests, and interrupt the reader (by closing the
487 // connection) as soon as the active requests finish.
488 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) { s.connClosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#connClosing. = true })
489
490 return c.Waitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#Wait().()
491 }
492
493 // readIncoming collects inbound messages from the reader and delivers them, either responding
494 // to outgoing calls or feeding requests to the queue.
495 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) readIncomingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#readIncoming().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, reader Readerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Reader#, preempter Preempterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Preempter#) {
496 var err error
497 for {
498 var msg Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Message#
499 msg, err = reader.Readscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Reader#Read.(ctx)
500 if err != nil {
501 break
502 }
503
504 switch msg := msg.(type) {
505 case *Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#:
506 c.acceptRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#acceptRequest().(ctx, msg, preempter)
507
508 case *Responsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#:
509 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
510 if ac, ok := s.outgoingCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingCalls.[msg.IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#ID.]; ok {
511 delete(s.outgoingCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingCalls., msg.IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#ID.)
512 ac.retirescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#retire().(msg)
513 } else {
514 // TODO: How should we report unexpected responses?
515 }
516 })
517
518 default:
519 c.internalErrorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#internalErrorf().("Read returned an unexpected message of type %T", msg)
520 }
521 }
522
523 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
524 s.readingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#reading. = false
525 s.readErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#readErr. = err
526
527 // Retire any outgoing requests that were still in flight: with the Reader no
528 // longer being processed, they necessarily cannot receive a response.
529 for id, ac := range s.outgoingCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingCalls. {
530 ac.retirescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/AsyncCall#retire().(&Responsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#{IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#ID.: id, Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Response#Error.: err})
531 }
532 s.outgoingCallsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#outgoingCalls. = nil
533 })
534 }
535
536 // acceptRequest either handles msg synchronously or enqueues it to be handled
537 // asynchronously.
538 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) acceptRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#acceptRequest().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, msg *Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#, preempter Preempterscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Preempter#) {
539 // In theory notifications cannot be cancelled, but we build them a cancel
540 // context anyway.
541 reqCtx, cancel := contextscip-go gomod github.com/golang/go/src go1.25.0 context/.WithCancelscip-go gomod github.com/golang/go/src go1.25.0 context/WithCancel().(ctx)
542 req := &incomingRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#{
543 Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#Request.: msg,
544 ctxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#ctx.: reqCtx,
545 cancelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#cancel.: cancel,
546 }
547
548 // If the request is a call, add it to the incoming map so it can be
549 // cancelled (or responded) by ID.
550 var err error
551 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
552 s.incomingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incoming.++
553
554 if req.IsCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#IsCall().() {
555 if s.incomingByIDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incomingByID.[req.IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#ID.] != nil {
556 err = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%w: request ID %v already in use", ErrInvalidRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrInvalidRequest., req.IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#ID.)
557 req.IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#ID. = IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ID#{} // Don't misattribute this error to the existing request.
558 return
559 }
560
561 if s.incomingByIDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incomingByID. == nil {
562 s.incomingByIDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incomingByID. = make(map[IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ID#]*incomingRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#)
563 }
564 s.incomingByIDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incomingByID.[req.IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#ID.] = req
565
566 // When shutting down, reject all new Call requests, even if they could
567 // theoretically be handled by the preempter. The preempter could return
568 // ErrAsyncResponse, which would increase the amount of work in flight
569 // when we're trying to ensure that it strictly decreases.
570 err = s.shuttingDownscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#shuttingDown().(ErrServerClosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrServerClosing.)
571 }
572 })
573 if err != nil {
574 c.processResultscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#processResult().("acceptRequest", req, nil, err)
575 return
576 }
577
578 if preempter != nil {
579 result, err := preempter.Preemptscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Preempter#Preempt.(req.ctxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#ctx., req.Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#Request.)
580
581 if req.IsCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#IsCall().() && errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Isscip-go gomod github.com/golang/go/src go1.25.0 errors/Is().(err, ErrAsyncResponsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrAsyncResponse.) {
582 // This request will remain in flight until Respond is called for it.
583 return
584 }
585
586 if !errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/.Isscip-go gomod github.com/golang/go/src go1.25.0 errors/Is().(err, ErrNotHandledscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrNotHandled.) {
587 c.processResultscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#processResult().("Preempt", req, result, err)
588 return
589 }
590 }
591
592 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
593 // If the connection is shutting down, don't enqueue anything to the
594 // handler — not even notifications. That ensures that if the handler
595 // continues to make progress, it will eventually become idle and
596 // close the connection.
597 err = s.shuttingDownscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#shuttingDown().(ErrServerClosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrServerClosing.)
598 if err != nil {
599 return
600 }
601
602 // We enqueue requests that have not been preempted to an unbounded slice.
603 // Unfortunately, we cannot in general limit the size of the handler
604 // queue: we have to read every response that comes in on the wire
605 // (because it may be responding to a request issued by, say, an
606 // asynchronous handler), and in order to get to that response we have
607 // to read all of the requests that came in ahead of it.
608 s.handlerQueuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#handlerQueue. = append(s.handlerQueuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#handlerQueue., req)
609 if !s.handlerRunningscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#handlerRunning. {
610 // We start the handleAsync goroutine when it has work to do, and let it
611 // exit when the queue empties.
612 //
613 // Otherwise, in order to synchronize the handler we would need some other
614 // goroutine (probably readIncoming?) to explicitly wait for handleAsync
615 // to finish, and that would complicate error reporting: either the error
616 // report from the goroutine would be blocked on the handler emptying its
617 // queue (which was tried, and introduced a deadlock detected by
618 // TestCloseCallRace), or the error would need to be reported separately
619 // from synchronizing completion. Allowing the handler goroutine to exit
620 // when idle seems simpler than trying to implement either of those
621 // alternatives correctly.
622 s.handlerRunningscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#handlerRunning. = true
623 go c.handleAsyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#handleAsync().()
624 }
625 })
626 if err != nil {
627 c.processResultscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#processResult().("acceptRequest", req, nil, err)
628 }
629 }
630
631 // handleAsync invokes the handler on the requests in the handler queue
632 // sequentially until the queue is empty.
633 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) handleAsyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#handleAsync().() {
634 for {
635 var req *incomingRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#
636 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
637 if len(s.handlerQueuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#handlerQueue.) > 0 {
638 req, s.handlerQueuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#handlerQueue. = s.handlerQueuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#handlerQueue.[0], s.handlerQueuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#handlerQueue.[1:]
639 } else {
640 s.handlerRunningscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#handlerRunning. = false
641 }
642 })
643 if req == nil {
644 return
645 }
646
647 // Only deliver to the Handler if not already canceled.
648 if err := req.ctxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#ctx..Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().(); err != nil {
649 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
650 if s.writeErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#writeErr. != nil {
651 // Assume that req.ctx was canceled due to s.writeErr.
652 // TODO(#51365): use a Context API to plumb this through req.ctx.
653 err = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%w: %v", ErrServerClosingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrServerClosing., s.writeErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#writeErr.)
654 }
655 })
656 c.processResultscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#processResult().("handleAsync", req, nil, err)
657 continue
658 }
659
660 result, err := c.handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#handler..Handlescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Handler#Handle.(req.ctxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#ctx., req.Requestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#Request.)
661 c.processResultscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#processResult().(c.handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#handler., req, result, err)
662 }
663 }
664
665 // processResult processes the result of a request and, if appropriate, sends a response.
666 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) processResultscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#processResult().(from any, req *incomingRequestscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#, result any, err error) error {
667 switch err {
668 case ErrAsyncResponsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrAsyncResponse.:
669 if !req.IsCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#IsCall().() {
670 return c.internalErrorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#internalErrorf().("%#v returned ErrAsyncResponse for a %q Request without an ID", from, req.Methodscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#Method.)
671 }
672 return nil // This request is still in flight, so don't record the result yet.
673 case ErrNotHandledscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrNotHandled., ErrMethodNotFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrMethodNotFound.:
674 // Add detail describing the unhandled method.
675 err = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%w: %q", ErrMethodNotFoundscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrMethodNotFound., req.Methodscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#Method.)
676 }
677
678 if result != nil && err != nil {
679 c.internalErrorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#internalErrorf().("%#v returned a non-nil result with a non-nil error for %s:\n%v\n%#v", from, req.Methodscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#Method., err, result)
680 result = nil // Discard the spurious result and respond with err.
681 }
682
683 if req.IsCallscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#IsCall().() {
684 if result == nil && err == nil {
685 err = c.internalErrorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#internalErrorf().("%#v returned a nil result and nil error for a %q Request that requires a Response", from, req.Methodscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#Method.)
686 }
687
688 response, respErr := NewResponsescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NewResponse().(req.IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#ID., result, err)
689
690 // The caller could theoretically reuse the request's ID as soon as we've
691 // sent the response, so ensure that it is removed from the incoming map
692 // before sending.
693 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
694 delete(s.incomingByIDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incomingByID., req.IDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#ID.)
695 })
696 if respErr == nil {
697 writeErr := c.writescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#write().(contextscip-go gomod github.com/golang/go/src go1.25.0 context/.WithoutCancelscip-go gomod github.com/golang/go/src go1.25.0 context/WithoutCancel().(req.ctxscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#ctx.), response)
698 if err == nil {
699 err = writeErr
700 }
701 } else {
702 err = c.internalErrorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#internalErrorf().("%#v returned a malformed result for %q: %w", from, req.Methodscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#Method., respErr)
703 }
704 } else { // req is a notification
705 if result != nil {
706 err = c.internalErrorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#internalErrorf().("%#v returned a non-nil result for a %q Request without an ID", from, req.Methodscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#Method.)
707 } else if err != nil {
708 err = fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%w: %q notification failed: %v", ErrInternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrInternal., req.Methodscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Request#Method., err)
709 }
710 if err != nil {
711 // TODO: can/should we do anything with this error beyond writing it to the event log?
712 // (Is this the right label to attach to the log?)
713 }
714 }
715
716 // Cancel the request to free any associated resources.
717 req.cancelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#cancel.()
718 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
719 if s.incomingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incoming. == 0 {
720 panic("jsonrpc2_v2: processResult called when incoming count is already zero")
721 }
722 s.incomingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incoming.--
723 })
724 return nil
725 }
726
727 // write is used by all things that write outgoing messages, including replies.
728 // it makes sure that writes are atomic
729 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) writescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#write().(ctx contextscip-go gomod github.com/golang/go/src go1.25.0 context/.Contextscip-go gomod github.com/golang/go/src go1.25.0 context/Context#, msg Messagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Message#) error {
730 writer := <-c.writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#writer.
731 defer func() { c.writerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#writer. <- writer }()
732 err := writer.Writescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Writer#Write.(ctx, msg)
733
734 if err != nil && ctx.Errscip-go gomod github.com/golang/go/src go1.25.0 context/Context#Err().() == nil {
735 // The call to Write failed, and since ctx.Err() is nil we can't attribute
736 // the failure (even indirectly) to Context cancellation. The writer appears
737 // to be broken, and future writes are likely to also fail.
738 //
739 // If the read side of the connection is also broken, we might not even be
740 // able to receive cancellation notifications. Since we can't reliably write
741 // the results of incoming calls and can't receive explicit cancellations,
742 // cancel the calls now.
743 c.updateInFlightscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#updateInFlight().(func(s *inFlightStatescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#) {
744 if s.writeErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#writeErr. == nil {
745 s.writeErrscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#writeErr. = err
746 for _, r := range s.incomingByIDscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/inFlightState#incomingByID. {
747 r.cancelscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/incomingRequest#cancel.()
748 }
749 }
750 })
751 }
752
753 return err
754 }
755
756 // internalErrorf reports an internal error. By default it panics, but if
757 // c.onInternalError is non-nil it instead calls that and returns an error
758 // wrapping ErrInternal.
759 func (c *Connectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#) internalErrorfscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#internalErrorf().(format string, args ...any) error {
760 err := fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().(format, args...)
761 if c.onInternalErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#onInternalError. == nil {
762 panic("jsonrpc2: " + err.Error())
763 }
764 c.onInternalErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Connection#onInternalError.(err)
765
766 return fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Errorf().("%w: %v", ErrInternalscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrInternal., err)
767 }