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