| 1 | // Copyright 2020 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 | "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/" |
| 10 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 11 | "runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/" |
| 12 | "syncscip-go gomod github.com/golang/go/src go1.25.0 sync/" |
| 13 | "sync/atomicscip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/" |
| 14 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 15 | ) |
| 16 | |
| 17 | // Listener is implemented by protocols to accept new inbound connections. |
| 18 | type Listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener# interface { |
| 19 | // Accept accepts an inbound connection to a server. |
| 20 | // It blocks until either an inbound connection is made, or the listener is closed. |
| 21 | Acceptscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Accept.(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#) (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#, error) |
| 22 | |
| 23 | // Close closes the listener. |
| 24 | // Any blocked Accept or Dial operations will unblock and return errors. |
| 25 | Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Close.() error |
| 26 | |
| 27 | // Dialer returns a dialer that can be used to connect to this listener |
| 28 | // locally. |
| 29 | // If a listener does not implement this it will return nil. |
| 30 | Dialerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Dialer.() Dialerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Dialer# |
| 31 | } |
| 32 | |
| 33 | // Dialer is used by clients to dial a server. |
| 34 | type Dialerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Dialer# interface { |
| 35 | // Dial returns a new communication byte stream to a listening server. |
| 36 | Dialscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Dialer#Dial.(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#) (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#, error) |
| 37 | } |
| 38 | |
| 39 | // Server is a running server that is accepting incoming connections. |
| 40 | type Serverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server# struct { |
| 41 | listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#listener. Listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener# |
| 42 | binderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#binder. Binderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Binder# |
| 43 | asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#async. *asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async# |
| 44 | |
| 45 | shutdownOncescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#shutdownOnce. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Oncescip-go gomod github.com/golang/go/src go1.25.0 sync/Once# |
| 46 | closingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#closing. atomicscip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/.Int32scip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/Int32# // atomic: set to nonzero when Shutdown is called |
| 47 | } |
| 48 | |
| 49 | // Dial uses the dialer to make a new connection, wraps the returned |
| 50 | // reader and writer using the framer to make a stream, and then builds |
| 51 | // a connection on top of that stream using the binder. |
| 52 | // |
| 53 | // The returned Connection will operate independently using the Preempter and/or |
| 54 | // Handler provided by the Binder, and will release its own resources when the |
| 55 | // connection is broken, but the caller may Close it earlier to stop accepting |
| 56 | // (or sending) new requests. |
| 57 | // |
| 58 | // If non-nil, the onDone function is called when the connection is closed. |
| 59 | func Dialscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Dial().(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#, dialer Dialerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Dialer#, 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#, error) { |
| 60 | // dial a server |
| 61 | rwc, err := dialer.Dialscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Dialer#Dial.(ctx) |
| 62 | if err != nil { |
| 63 | return nil, err |
| 64 | } |
| 65 | return bindConnectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/bindConnection().(ctx, rwc, binder, onDone), nil |
| 66 | } |
| 67 | |
| 68 | // NewServer starts a new server listening for incoming connections and returns |
| 69 | // it. |
| 70 | // This returns a fully running and connected server, it does not block on |
| 71 | // the listener. |
| 72 | // You can call Wait to block on the server, or Shutdown to get the sever to |
| 73 | // terminate gracefully. |
| 74 | // To notice incoming connections, use an intercepting Binder. |
| 75 | func NewServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NewServer().(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#, listener Listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#, binder Binderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Binder#) *Serverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server# { |
| 76 | server := &Serverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#{ |
| 77 | listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#listener.: listener, |
| 78 | binderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#binder.: binder, |
| 79 | asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#async.: newAsyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/newAsync().(), |
| 80 | } |
| 81 | go server.runscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#run().(ctx) |
| 82 | return server |
| 83 | } |
| 84 | |
| 85 | // Wait returns only when the server has shut down. |
| 86 | func (s *Serverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#) Waitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#Wait().() error { |
| 87 | return s.asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#async..waitscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#wait().() |
| 88 | } |
| 89 | |
| 90 | // Shutdown informs the server to stop accepting new connections. |
| 91 | func (s *Serverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#) Shutdownscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#Shutdown().() { |
| 92 | s.shutdownOncescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#shutdownOnce..Doscip-go gomod github.com/golang/go/src go1.25.0 sync/Once#Do().(func() { |
| 93 | s.closingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#closing..Storescip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/Int32#Store().(1) |
| 94 | s.listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#listener..Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Close.() |
| 95 | }) |
| 96 | } |
| 97 | |
| 98 | // run accepts incoming connections from the listener, |
| 99 | // If IdleTimeout is non-zero, run exits after there are no clients for this |
| 100 | // duration, otherwise it exits only on error. |
| 101 | func (s *Serverscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#) runscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#run().(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#) { |
| 102 | defer s.asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#async..donescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#done().() |
| 103 | |
| 104 | var activeConns syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.WaitGroupscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup# |
| 105 | for { |
| 106 | rwc, err := s.listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#listener..Acceptscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Accept.(ctx) |
| 107 | if err != nil { |
| 108 | // Only Shutdown closes the listener. If we get an error after Shutdown is |
| 109 | // called, assume that was the cause and don't report the error; |
| 110 | // otherwise, report the error in case it is unexpected. |
| 111 | if s.closingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#closing..Loadscip-go gomod github.com/golang/go/src go1.25.0 `sync/atomic`/Int32#Load().() == 0 { |
| 112 | s.asyncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#async..setErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/async#setError().(err) |
| 113 | } |
| 114 | // We are done generating new connections for good. |
| 115 | break |
| 116 | } |
| 117 | |
| 118 | // A new inbound connection. |
| 119 | activeConns.Addscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Add().(1) |
| 120 | _ = bindConnectionscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/bindConnection().(ctx, rwc, s.binderscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Server#binder., activeConns.Donescip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Done().) // unregisters itself when done |
| 121 | } |
| 122 | activeConns.Waitscip-go gomod github.com/golang/go/src go1.25.0 sync/WaitGroup#Wait().() |
| 123 | } |
| 124 | |
| 125 | // NewIdleListener wraps a listener with an idle timeout. |
| 126 | // |
| 127 | // When there are no active connections for at least the timeout duration, |
| 128 | // calls to Accept will fail with ErrIdleTimeout. |
| 129 | // |
| 130 | // A connection is considered inactive as soon as its Close method is called. |
| 131 | func NewIdleListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/NewIdleListener().(timeout timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration#, wrap Listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#) Listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener# { |
| 132 | l := &idleListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#{ |
| 133 | wrappedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#wrapped.: wrap, |
| 134 | timeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timeout.: timeout, |
| 135 | activescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#active.: make(chan int, 1), |
| 136 | timedOutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timedOut.: make(chan struct{}), |
| 137 | idleTimerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#idleTimer.: make(chan *timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timerscip-go gomod github.com/golang/go/src go1.25.0 time/Timer#, 1), |
| 138 | } |
| 139 | l.idleTimerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#idleTimer. <- timescip-go gomod github.com/golang/go/src go1.25.0 time/.AfterFuncscip-go gomod github.com/golang/go/src go1.25.0 time/AfterFunc().(l.timeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timeout., l.timerExpiredscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timerExpired().) |
| 140 | return l |
| 141 | } |
| 142 | |
| 143 | type idleListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener# struct { |
| 144 | wrappedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#wrapped. Listenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener# |
| 145 | timeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timeout. timescip-go gomod github.com/golang/go/src go1.25.0 time/.Durationscip-go gomod github.com/golang/go/src go1.25.0 time/Duration# |
| 146 | |
| 147 | // Only one of these channels is receivable at any given time. |
| 148 | activescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#active. chan int // count of active connections; closed when Close is called if not timed out |
| 149 | timedOutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timedOut. chan struct{} // closed when the idle timer expires |
| 150 | idleTimerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#idleTimer. chan *timescip-go gomod github.com/golang/go/src go1.25.0 time/.Timerscip-go gomod github.com/golang/go/src go1.25.0 time/Timer# // holds the timer only when idle |
| 151 | } |
| 152 | |
| 153 | // Accept accepts an incoming connection. |
| 154 | // |
| 155 | // If an incoming connection is accepted concurrent to the listener being closed |
| 156 | // due to idleness, the new connection is immediately closed. |
| 157 | func (l *idleListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#) Acceptscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#Accept().(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#) (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#, error) { |
| 158 | rwc, err := l.wrappedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#wrapped..Acceptscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Accept.(ctx) |
| 159 | |
| 160 | select { |
| 161 | case n, ok := <-l.activescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#active.: |
| 162 | if err != nil { |
| 163 | if ok { |
| 164 | l.activescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#active. <- n |
| 165 | } |
| 166 | return nil, err |
| 167 | } |
| 168 | if ok { |
| 169 | l.activescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#active. <- n + 1 |
| 170 | } else { |
| 171 | // l.wrapped.Close Close has been called, but Accept returned a |
| 172 | // connection. This race can occur with concurrent Accept and Close calls |
| 173 | // with any net.Listener, and it is benign: since the listener was closed |
| 174 | // explicitly, it can't have also timed out. |
| 175 | } |
| 176 | return l.newConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#newConn().(rwc), nil |
| 177 | |
| 178 | case <-l.timedOutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timedOut.: |
| 179 | if err == nil { |
| 180 | // Keeping the connection open would leave the listener simultaneously |
| 181 | // active and closed due to idleness, which would be contradictory and |
| 182 | // confusing. Close the connection and pretend that it never happened. |
| 183 | rwc.Closescip-go gomod github.com/golang/go/src go1.25.0 io/Closer#Close().() |
| 184 | } else { |
| 185 | // In theory the timeout could have raced with an unrelated error return |
| 186 | // from Accept. However, ErrIdleTimeout is arguably still valid (since we |
| 187 | // would have closed due to the timeout independent of the error), and the |
| 188 | // harm from returning a spurious ErrIdleTimeout is negligible anyway. |
| 189 | } |
| 190 | return nil, ErrIdleTimeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrIdleTimeout. |
| 191 | |
| 192 | case timer := <-l.idleTimerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#idleTimer.: |
| 193 | if err != nil { |
| 194 | // The idle timer doesn't run until it receives itself from the idleTimer |
| 195 | // channel, so it can't have called l.wrapped.Close yet and thus err can't |
| 196 | // be ErrIdleTimeout. Leave the idle timer as it was and return whatever |
| 197 | // error we got. |
| 198 | l.idleTimerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#idleTimer. <- timer |
| 199 | return nil, err |
| 200 | } |
| 201 | |
| 202 | if !timer.Stopscip-go gomod github.com/golang/go/src go1.25.0 time/Timer#Stop().() { |
| 203 | // Failed to stop the timer — the timer goroutine is in the process of |
| 204 | // firing. Send the timer back to the timer goroutine so that it can |
| 205 | // safely close the timedOut channel, and then wait for the listener to |
| 206 | // actually be closed before we return ErrIdleTimeout. |
| 207 | l.idleTimerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#idleTimer. <- timer |
| 208 | rwc.Closescip-go gomod github.com/golang/go/src go1.25.0 io/Closer#Close().() |
| 209 | <-l.timedOutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timedOut. |
| 210 | return nil, ErrIdleTimeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrIdleTimeout. |
| 211 | } |
| 212 | |
| 213 | l.activescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#active. <- 1 |
| 214 | return l.newConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#newConn().(rwc), nil |
| 215 | } |
| 216 | } |
| 217 | |
| 218 | func (l *idleListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#) Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#Close().() error { |
| 219 | select { |
| 220 | case _, ok := <-l.activescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#active.: |
| 221 | if ok { |
| 222 | close(l.activescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#active.) |
| 223 | } |
| 224 | |
| 225 | case <-l.timedOutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timedOut.: |
| 226 | // Already closed by the timer; take care not to double-close if the caller |
| 227 | // only explicitly invokes this Close method once, since the io.Closer |
| 228 | // interface explicitly leaves doubled Close calls undefined. |
| 229 | return ErrIdleTimeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrIdleTimeout. |
| 230 | |
| 231 | case timer := <-l.idleTimerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#idleTimer.: |
| 232 | if !timer.Stopscip-go gomod github.com/golang/go/src go1.25.0 time/Timer#Stop().() { |
| 233 | // Couldn't stop the timer. It shouldn't take long to run, so just wait |
| 234 | // (so that the Listener is guaranteed to be closed before we return) |
| 235 | // and pretend that this call happened afterward. |
| 236 | // That way we won't leak any timers or goroutines when Close returns. |
| 237 | l.idleTimerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#idleTimer. <- timer |
| 238 | <-l.timedOutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timedOut. |
| 239 | return ErrIdleTimeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/ErrIdleTimeout. |
| 240 | } |
| 241 | close(l.activescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#active.) |
| 242 | } |
| 243 | |
| 244 | return l.wrappedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#wrapped..Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Close.() |
| 245 | } |
| 246 | |
| 247 | func (l *idleListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#) Dialerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#Dialer().() Dialerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Dialer# { |
| 248 | return l.wrappedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#wrapped..Dialerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Dialer.() |
| 249 | } |
| 250 | |
| 251 | func (l *idleListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#) timerExpiredscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timerExpired().() { |
| 252 | select { |
| 253 | case n, ok := <-l.activescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#active.: |
| 254 | if ok { |
| 255 | 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: idleListener idle timer fired with %d connections still active", n)) |
| 256 | } else { |
| 257 | panic("jsonrpc2: Close finished with idle timer still running") |
| 258 | } |
| 259 | |
| 260 | case <-l.timedOutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timedOut.: |
| 261 | panic("jsonrpc2: idleListener idle timer fired more than once") |
| 262 | |
| 263 | case <-l.idleTimerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#idleTimer.: |
| 264 | // The timer for this very call! |
| 265 | } |
| 266 | |
| 267 | // Close the Listener with all channels still blocked to ensure that this call |
| 268 | // to l.wrapped.Close doesn't race with the one in l.Close. |
| 269 | defer close(l.timedOutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timedOut.) |
| 270 | l.wrappedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#wrapped..Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/Listener#Close.() |
| 271 | } |
| 272 | |
| 273 | func (l *idleListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#) connClosedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#connClosed().() { |
| 274 | select { |
| 275 | case n, ok := <-l.activescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#active.: |
| 276 | if !ok { |
| 277 | // l is already closed, so it can't close due to idleness, |
| 278 | // and we don't need to track the number of active connections any more. |
| 279 | return |
| 280 | } |
| 281 | n-- |
| 282 | if n == 0 { |
| 283 | l.idleTimerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#idleTimer. <- timescip-go gomod github.com/golang/go/src go1.25.0 time/.AfterFuncscip-go gomod github.com/golang/go/src go1.25.0 time/AfterFunc().(l.timeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timeout., l.timerExpiredscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timerExpired().) |
| 284 | } else { |
| 285 | l.activescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#active. <- n |
| 286 | } |
| 287 | |
| 288 | case <-l.timedOutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#timedOut.: |
| 289 | panic("jsonrpc2: idleListener idle timer fired before last active connection was closed") |
| 290 | |
| 291 | case <-l.idleTimerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#idleTimer.: |
| 292 | panic("jsonrpc2: idleListener idle timer active before last active connection was closed") |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | type idleListenerConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn# struct { |
| 297 | wrappedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#wrapped. 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# |
| 298 | lscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#l. *idleListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener# |
| 299 | closeOncescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#closeOnce. syncscip-go gomod github.com/golang/go/src go1.25.0 sync/.Oncescip-go gomod github.com/golang/go/src go1.25.0 sync/Once# |
| 300 | } |
| 301 | |
| 302 | func (l *idleListenerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#) newConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#newConn().(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#) *idleListenerConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn# { |
| 303 | c := &idleListenerConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#{ |
| 304 | wrappedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#wrapped.: rwc, |
| 305 | lscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#l.: l, |
| 306 | } |
| 307 | |
| 308 | // A caller that forgets to call Close may disrupt the idleListener's |
| 309 | // accounting, even though the file descriptor for the underlying connection |
| 310 | // may eventually be garbage-collected anyway. |
| 311 | // |
| 312 | // Set a (best-effort) finalizer to verify that a Close call always occurs. |
| 313 | // (We will clear the finalizer explicitly in Close.) |
| 314 | runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.SetFinalizerscip-go gomod github.com/golang/go/src go1.25.0 runtime/SetFinalizer().(c, func(c *idleListenerConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#) { |
| 315 | panic("jsonrpc2: IdleListener connection became unreachable without a call to Close") |
| 316 | }) |
| 317 | |
| 318 | return c |
| 319 | } |
| 320 | |
| 321 | func (c *idleListenerConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#) Readscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#Read().(p []byte) (int, error) { return c.wrappedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#wrapped..Readscip-go gomod github.com/golang/go/src go1.25.0 io/Reader#Read().(p) } |
| 322 | func (c *idleListenerConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#) Writescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#Write().(p []byte) (int, error) { return c.wrappedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#wrapped..Writescip-go gomod github.com/golang/go/src go1.25.0 io/Writer#Write().(p) } |
| 323 | |
| 324 | func (c *idleListenerConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#) Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#Close().() error { |
| 325 | defer c.closeOncescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#closeOnce..Doscip-go gomod github.com/golang/go/src go1.25.0 sync/Once#Do().(func() { |
| 326 | c.lscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#l..connClosedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListener#connClosed().() |
| 327 | runtimescip-go gomod github.com/golang/go/src go1.25.0 runtime/.SetFinalizerscip-go gomod github.com/golang/go/src go1.25.0 runtime/SetFinalizer().(c, nil) |
| 328 | }) |
| 329 | return c.wrappedscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2_v2`/idleListenerConn#wrapped..Closescip-go gomod github.com/golang/go/src go1.25.0 io/Closer#Close().() |
| 330 | } |