| 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 cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/ |
| 6 | |
| 7 | import ( |
| 8 | "contextscip-go gomod github.com/golang/go/src go1.24.0 context/" |
| 9 | "encoding/jsonscip-go gomod github.com/golang/go/src go1.24.0 `encoding/json`/" |
| 10 | "fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/" |
| 11 | "syncscip-go gomod github.com/golang/go/src go1.24.0 sync/" |
| 12 | "sync/atomicscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/" |
| 13 | |
| 14 | "cuelang.org/go/internal/golangorgx/tools/eventscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/" |
| 15 | "cuelang.org/go/internal/golangorgx/tools/event/labelscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/label`/" |
| 16 | "cuelang.org/go/internal/golangorgx/tools/event/tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/" |
| 17 | ) |
| 18 | |
| 19 | // Conn is the common interface to jsonrpc clients and servers. |
| 20 | // Conn is bidirectional; it does not have a designated server or client end. |
| 21 | // It manages the jsonrpc2 protocol, connecting responses back to their calls. |
| 22 | type Connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Conn# interface { |
| 23 | // Call invokes the target method and waits for a response. |
| 24 | // The params will be marshaled to JSON before sending over the wire, and will |
| 25 | // be handed to the method invoked. |
| 26 | // The response will be unmarshaled from JSON into the result. |
| 27 | // The id returned will be unique from this connection, and can be used for |
| 28 | // logging or tracking. |
| 29 | Callscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Conn#Call.(ctx contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Contextscip-go gomod github.com/golang/go/src go1.24.0 context/Context#, method string, params, result any) (IDscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/ID#, error) |
| 30 | |
| 31 | // Notify invokes the target method but does not wait for a response. |
| 32 | // The params will be marshaled to JSON before sending over the wire, and will |
| 33 | // be handed to the method invoked. |
| 34 | Notifyscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Conn#Notify.(ctx contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Contextscip-go gomod github.com/golang/go/src go1.24.0 context/Context#, method string, params any) error |
| 35 | |
| 36 | // Go starts a goroutine to handle the connection. |
| 37 | // It must be called exactly once for each Conn. |
| 38 | // It returns immediately. |
| 39 | // You must block on Done() to wait for the connection to shut down. |
| 40 | // This is a temporary measure, this should be started automatically in the |
| 41 | // future. |
| 42 | Goscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Conn#Go.(ctx contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Contextscip-go gomod github.com/golang/go/src go1.24.0 context/Context#, handler Handlerscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Handler#) |
| 43 | |
| 44 | // Close closes the connection and it's underlying stream. |
| 45 | // It does not wait for the close to complete, use the Done() channel for |
| 46 | // that. |
| 47 | Closescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Conn#Close.() error |
| 48 | |
| 49 | // Done returns a channel that will be closed when the processing goroutine |
| 50 | // has terminated, which will happen if Close() is called or an underlying |
| 51 | // stream is closed. |
| 52 | Donescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Conn#Done.() <-chan struct{} |
| 53 | |
| 54 | // Err returns an error if there was one from within the processing goroutine. |
| 55 | // If err returns non nil, the connection will be already closed or closing. |
| 56 | Errscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Conn#Err.() error |
| 57 | } |
| 58 | |
| 59 | type connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn# struct { |
| 60 | seqscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#seq. int64 // must only be accessed using atomic operations |
| 61 | writeMuscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#writeMu. syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex# // protects writes to the stream |
| 62 | streamscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#stream. Streamscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Stream# |
| 63 | pendingMuscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#pendingMu. syncscip-go gomod github.com/golang/go/src go1.24.0 sync/.Mutexscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex# // protects the pending map |
| 64 | pendingscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#pending. map[IDscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/ID#]chan *Responsescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Response# |
| 65 | |
| 66 | donescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#done. chan struct{} |
| 67 | errscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#err. atomicscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/.Valuescip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Value# |
| 68 | } |
| 69 | |
| 70 | // NewConn creates a new connection object around the supplied stream. |
| 71 | func NewConnscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/NewConn().(s Streamscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Stream#) Connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Conn# { |
| 72 | conn := &connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#{ |
| 73 | streamscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#stream.: s, |
| 74 | pendingscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#pending.: make(map[IDscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/ID#]chan *Responsescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Response#), |
| 75 | donescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#done.: make(chan struct{}), |
| 76 | } |
| 77 | return conn |
| 78 | } |
| 79 | |
| 80 | func (c *connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#) Notifyscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#Notify().(ctx contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Contextscip-go gomod github.com/golang/go/src go1.24.0 context/Context#, method string, params any) (err error) { |
| 81 | notify, err := NewNotificationscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/NewNotification().(method, params) |
| 82 | if err != nil { |
| 83 | return fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("marshaling notify parameters: %v", err) |
| 84 | } |
| 85 | ctx, done := eventscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/.Startscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/Start().(ctx, method, |
| 86 | tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.Methodscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/Method..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/String#Of().(method), |
| 87 | tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.RPCDirectionscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/RPCDirection..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/String#Of().(tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.Outboundscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/Outbound.), |
| 88 | ) |
| 89 | defer func() { |
| 90 | recordStatusscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/recordStatus().(ctx, err) |
| 91 | done() |
| 92 | }() |
| 93 | |
| 94 | eventscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/.Metricscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/Metric().(ctx, tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.Startedscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/Started..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/Int64#Of().(1)) |
| 95 | n, err := c.writescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#write().(ctx, notify) |
| 96 | eventscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/.Metricscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/Metric().(ctx, tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.SentBytesscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/SentBytes..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/Int64#Of().(n)) |
| 97 | return err |
| 98 | } |
| 99 | |
| 100 | func (c *connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#) Callscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#Call().(ctx contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Contextscip-go gomod github.com/golang/go/src go1.24.0 context/Context#, method string, params, result any) (_ IDscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/ID#, err error) { |
| 101 | // generate a new request identifier |
| 102 | id := IDscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/ID#{numberscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/ID#number.: atomicscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/.AddInt64scip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/AddInt64().(&c.seqscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#seq., 1)} |
| 103 | call, err := NewCallscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/NewCall().(id, method, params) |
| 104 | if err != nil { |
| 105 | return id, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("marshaling call parameters: %v", err) |
| 106 | } |
| 107 | ctx, done := eventscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/.Startscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/Start().(ctx, method, |
| 108 | tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.Methodscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/Method..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/String#Of().(method), |
| 109 | tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.RPCDirectionscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/RPCDirection..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/String#Of().(tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.Outboundscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/Outbound.), |
| 110 | tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.RPCIDscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/RPCID..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/String#Of().(fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("%q", id)), |
| 111 | ) |
| 112 | defer func() { |
| 113 | recordStatusscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/recordStatus().(ctx, err) |
| 114 | done() |
| 115 | }() |
| 116 | eventscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/.Metricscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/Metric().(ctx, tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.Startedscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/Started..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/Int64#Of().(1)) |
| 117 | // We have to add ourselves to the pending map before we send, otherwise we |
| 118 | // are racing the response. Also add a buffer to rchan, so that if we get a |
| 119 | // wire response between the time this call is cancelled and id is deleted |
| 120 | // from c.pending, the send to rchan will not block. |
| 121 | rchan := make(chan *Responsescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Response#, 1) |
| 122 | c.pendingMuscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#pendingMu..Lockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Lock().() |
| 123 | c.pendingscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#pending.[id] = rchan |
| 124 | c.pendingMuscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#pendingMu..Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Unlock().() |
| 125 | defer func() { |
| 126 | c.pendingMuscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#pendingMu..Lockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Lock().() |
| 127 | delete(c.pendingscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#pending., id) |
| 128 | c.pendingMuscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#pendingMu..Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Unlock().() |
| 129 | }() |
| 130 | // now we are ready to send |
| 131 | n, err := c.writescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#write().(ctx, call) |
| 132 | eventscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/.Metricscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/Metric().(ctx, tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.SentBytesscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/SentBytes..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/Int64#Of().(n)) |
| 133 | if err != nil { |
| 134 | // sending failed, we will never get a response, so don't leave it pending |
| 135 | return id, err |
| 136 | } |
| 137 | // now wait for the response |
| 138 | select { |
| 139 | case response := <-rchan: |
| 140 | // is it an error response? |
| 141 | if response.errscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Response#err. != nil { |
| 142 | return id, response.errscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Response#err. |
| 143 | } |
| 144 | if result == nil || len(response.resultscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Response#result.) == 0 { |
| 145 | return id, nil |
| 146 | } |
| 147 | if err := jsonscip-go gomod github.com/golang/go/src go1.24.0 `encoding/json`/.Unmarshalscip-go gomod github.com/golang/go/src go1.24.0 `encoding/json`/Unmarshal().(response.resultscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Response#result., result); err != nil { |
| 148 | return id, fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Errorfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Errorf().("unmarshaling result: %v", err) |
| 149 | } |
| 150 | return id, nil |
| 151 | case <-ctx.Donescip-go gomod github.com/golang/go/src go1.24.0 context/Context#Done().(): |
| 152 | return id, ctx.Errscip-go gomod github.com/golang/go/src go1.24.0 context/Context#Err().() |
| 153 | } |
| 154 | } |
| 155 | |
| 156 | func (c *connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#) replierscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#replier().(req Requestscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Request#, spanDone func()) Replierscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Replier# { |
| 157 | return func(ctx contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Contextscip-go gomod github.com/golang/go/src go1.24.0 context/Context#, result any, err error) error { |
| 158 | defer func() { |
| 159 | recordStatusscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/recordStatus().(ctx, err) |
| 160 | spanDone() |
| 161 | }() |
| 162 | call, ok := req.(*Callscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Call#) |
| 163 | if !ok { |
| 164 | // request was a notify, no need to respond |
| 165 | return nil |
| 166 | } |
| 167 | response, err := NewResponsescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/NewResponse().(call.idscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Call#id., result, err) |
| 168 | if err != nil { |
| 169 | return err |
| 170 | } |
| 171 | n, err := c.writescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#write().(ctx, response) |
| 172 | eventscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/.Metricscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/Metric().(ctx, tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.SentBytesscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/SentBytes..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/Int64#Of().(n)) |
| 173 | if err != nil { |
| 174 | // TODO(iancottrell): if a stream write fails, we really need to shut down |
| 175 | // the whole stream |
| 176 | return err |
| 177 | } |
| 178 | return nil |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | func (c *connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#) writescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#write().(ctx contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Contextscip-go gomod github.com/golang/go/src go1.24.0 context/Context#, msg Messagescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Message#) (int64, error) { |
| 183 | c.writeMuscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#writeMu..Lockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Lock().() |
| 184 | defer c.writeMuscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#writeMu..Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Unlock().() |
| 185 | return c.streamscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#stream..Writescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Stream#Write.(ctx, msg) |
| 186 | } |
| 187 | |
| 188 | func (c *connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#) Goscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#Go().(ctx contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Contextscip-go gomod github.com/golang/go/src go1.24.0 context/Context#, handler Handlerscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Handler#) { |
| 189 | go c.runscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#run().(ctx, handler) |
| 190 | } |
| 191 | |
| 192 | func (c *connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#) runscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#run().(ctx contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Contextscip-go gomod github.com/golang/go/src go1.24.0 context/Context#, handler Handlerscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Handler#) { |
| 193 | defer close(c.donescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#done.) |
| 194 | for { |
| 195 | // get the next message |
| 196 | msg, n, err := c.streamscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#stream..Readscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Stream#Read.(ctx) |
| 197 | if err != nil { |
| 198 | // The stream failed, we cannot continue. |
| 199 | c.failscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#fail().(err) |
| 200 | return |
| 201 | } |
| 202 | switch msg := msg.(type) { |
| 203 | case Requestscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Request#: |
| 204 | labels := []labelscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/label`/.Labelscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/label`/Label#{ |
| 205 | tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.Methodscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/Method..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/String#Of().(msg.Methodscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Request#Method.()), |
| 206 | tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.RPCDirectionscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/RPCDirection..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/String#Of().(tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.Inboundscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/Inbound.), |
| 207 | {}, // reserved for ID if present |
| 208 | } |
| 209 | if call, ok := msg.(*Callscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Call#); ok { |
| 210 | labels[len(labels)-1] = tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.RPCIDscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/RPCID..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/String#Of().(fmtscip-go gomod github.com/golang/go/src go1.24.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.24.0 fmt/Sprintf().("%q", call.IDscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Call#ID().())) |
| 211 | } else { |
| 212 | labels = labels[:len(labels)-1] |
| 213 | } |
| 214 | reqCtx, spanDone := eventscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/.Startscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/Start().(ctx, msg.Methodscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Request#Method.(), labels...) |
| 215 | eventscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/.Metricscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/Metric().(reqCtx, |
| 216 | tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.Startedscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/Started..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/Int64#Of().(1), |
| 217 | tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.ReceivedBytesscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/ReceivedBytes..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/Int64#Of().(n)) |
| 218 | if err := handler(reqCtx, c.replierscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#replier().(msg, spanDone), msg); err != nil { |
| 219 | // delivery failed, not much we can do |
| 220 | eventscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/.Errorscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/Error().(reqCtx, "jsonrpc2 message delivery failed", err) |
| 221 | } |
| 222 | case *Responsescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Response#: |
| 223 | // If method is not set, this should be a response, in which case we must |
| 224 | // have an id to send the response back to the caller. |
| 225 | c.pendingMuscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#pendingMu..Lockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Lock().() |
| 226 | rchan, ok := c.pendingscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#pending.[msg.idscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Response#id.] |
| 227 | c.pendingMuscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#pendingMu..Unlockscip-go gomod github.com/golang/go/src go1.24.0 sync/Mutex#Unlock().() |
| 228 | if ok { |
| 229 | rchan <- msg |
| 230 | } |
| 231 | } |
| 232 | } |
| 233 | } |
| 234 | |
| 235 | func (c *connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#) Closescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#Close().() error { |
| 236 | return c.streamscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#stream..Closescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Stream#Close.() |
| 237 | } |
| 238 | |
| 239 | func (c *connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#) Donescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#Done().() <-chan struct{} { |
| 240 | return c.donescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#done. |
| 241 | } |
| 242 | |
| 243 | func (c *connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#) Errscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#Err().() error { |
| 244 | if err := c.errscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#err..Loadscip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Value#Load().(); err != nil { |
| 245 | return err.(error) |
| 246 | } |
| 247 | return nil |
| 248 | } |
| 249 | |
| 250 | // fail sets a failure condition on the stream and closes it. |
| 251 | func (c *connscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#) failscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#fail().(err error) { |
| 252 | c.errscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#err..Storescip-go gomod github.com/golang/go/src go1.24.0 `sync/atomic`/Value#Store().(err) |
| 253 | c.streamscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/conn#stream..Closescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/Stream#Close.() |
| 254 | } |
| 255 | |
| 256 | func recordStatusscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/jsonrpc2`/recordStatus().(ctx contextscip-go gomod github.com/golang/go/src go1.24.0 context/.Contextscip-go gomod github.com/golang/go/src go1.24.0 context/Context#, err error) { |
| 257 | if err != nil { |
| 258 | eventscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/.Labelscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/Label().(ctx, tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.StatusCodescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/StatusCode..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/String#Of().("ERROR")) |
| 259 | } else { |
| 260 | eventscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/.Labelscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event`/Label().(ctx, tagscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/.StatusCodescip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/tag`/StatusCode..Ofscip-go gomod cuelang.org/go . `cuelang.org/go/internal/golangorgx/tools/event/keys`/String#Of().("OK")) |
| 261 | } |
| 262 | } |