| 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`/ |
| 6 | |
| 7 | import ( |
| 8 | "contextscip-go gomod github.com/golang/go/src go1.25.0 context/" |
| 9 | "errorsscip-go gomod github.com/golang/go/src go1.25.0 errors/" |
| 10 | "ioscip-go gomod github.com/golang/go/src go1.25.0 io/" |
| 11 | "mathscip-go gomod github.com/golang/go/src go1.25.0 math/" |
| 12 | "netscip-go gomod github.com/golang/go/src go1.25.0 net/" |
| 13 | "osscip-go gomod github.com/golang/go/src go1.25.0 os/" |
| 14 | "timescip-go gomod github.com/golang/go/src go1.25.0 time/" |
| 15 | |
| 16 | "golang.org/x/tools/internal/eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event`/" |
| 17 | ) |
| 18 | |
| 19 | // NOTE: This file provides an experimental API for serving multiple remote |
| 20 | // jsonrpc2 clients over the network. For now, it is intentionally similar to |
| 21 | // net/http, but that may change in the future as we figure out the correct |
| 22 | // semantics. |
| 23 | |
| 24 | // A StreamServer is used to serve incoming jsonrpc2 clients communicating over |
| 25 | // a newly created connection. |
| 26 | type StreamServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/StreamServer# interface { |
| 27 | ServeStreamscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/StreamServer#ServeStream.(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#, Connscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Conn#) error |
| 28 | } |
| 29 | |
| 30 | // The ServerFunc type is an adapter that implements the StreamServer interface |
| 31 | // using an ordinary function. |
| 32 | type ServerFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/ServerFunc# 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#, Connscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Conn#) error |
| 33 | |
| 34 | // ServeStream calls f(ctx, s). |
| 35 | func (f ServerFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/ServerFunc#) ServeStreamscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/ServerFunc#ServeStream().(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 Connscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Conn#) error { |
| 36 | return f(ctx, c) |
| 37 | } |
| 38 | |
| 39 | // HandlerServer returns a StreamServer that handles incoming streams using the |
| 40 | // provided handler. |
| 41 | func HandlerServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/HandlerServer().(h Handlerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Handler#) StreamServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/StreamServer# { |
| 42 | return ServerFuncscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/ServerFunc#(func(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#, conn Connscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Conn#) error { |
| 43 | conn.Goscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Conn#Go.(ctx, h) |
| 44 | <-conn.Donescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Conn#Done.() |
| 45 | return conn.Errscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Conn#Err.() |
| 46 | }) |
| 47 | } |
| 48 | |
| 49 | // ListenAndServe starts a jsonrpc2 server on the given address. If |
| 50 | // idleTimeout is non-zero, ListenAndServe exits after there are no clients for |
| 51 | // this duration, otherwise it exits only on error. |
| 52 | func ListenAndServescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/ListenAndServe().(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#, network, addr string, server StreamServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/StreamServer#, idleTimeout 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#) error { |
| 53 | ln, err := netscip-go gomod github.com/golang/go/src go1.25.0 net/.Listenscip-go gomod github.com/golang/go/src go1.25.0 net/Listen().(network, addr) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | defer ln.Closescip-go gomod github.com/golang/go/src go1.25.0 net/Listener#Close().() |
| 58 | if network == "unix" { |
| 59 | defer osscip-go gomod github.com/golang/go/src go1.25.0 os/.Removescip-go gomod github.com/golang/go/src go1.25.0 os/Remove().(addr) |
| 60 | } |
| 61 | return Servescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Serve().(ctx, ln, server, idleTimeout) |
| 62 | } |
| 63 | |
| 64 | // Serve accepts incoming connections from the network, and handles them using |
| 65 | // the provided server. If idleTimeout is non-zero, ListenAndServe exits after |
| 66 | // there are no clients for this duration, otherwise it exits only on error. |
| 67 | func Servescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Serve().(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#, ln netscip-go gomod github.com/golang/go/src go1.25.0 net/.Listenerscip-go gomod github.com/golang/go/src go1.25.0 net/Listener#, server StreamServerscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/StreamServer#, idleTimeout 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#) error { |
| 68 | newConns := make(chan netscip-go gomod github.com/golang/go/src go1.25.0 net/.Connscip-go gomod github.com/golang/go/src go1.25.0 net/Conn#) |
| 69 | closedConns := make(chan error) |
| 70 | activeConns := 0 |
| 71 | var acceptErr error |
| 72 | go func() { |
| 73 | defer close(newConns) |
| 74 | for { |
| 75 | var nc netscip-go gomod github.com/golang/go/src go1.25.0 net/.Connscip-go gomod github.com/golang/go/src go1.25.0 net/Conn# |
| 76 | nc, acceptErr = ln.Acceptscip-go gomod github.com/golang/go/src go1.25.0 net/Listener#Accept().() |
| 77 | if acceptErr != nil { |
| 78 | return |
| 79 | } |
| 80 | newConns <- nc |
| 81 | } |
| 82 | }() |
| 83 | |
| 84 | ctx, 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) |
| 85 | defer func() { |
| 86 | // Signal the Accept goroutine to stop immediately |
| 87 | // and terminate all newly-accepted connections until it returns. |
| 88 | ln.Closescip-go gomod github.com/golang/go/src go1.25.0 net/Listener#Close().() |
| 89 | for nc := range newConns { |
| 90 | nc.Closescip-go gomod github.com/golang/go/src go1.25.0 net/Conn#Close().() |
| 91 | } |
| 92 | // Cancel pending ServeStream callbacks and wait for them to finish. |
| 93 | cancel() |
| 94 | for activeConns > 0 { |
| 95 | err := <-closedConns |
| 96 | if !isClosingErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/isClosingError().(err) { |
| 97 | eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event`/.Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event`/Error().(ctx, "closed a connection", err) |
| 98 | } |
| 99 | activeConns-- |
| 100 | } |
| 101 | }() |
| 102 | |
| 103 | // Max duration: ~290 years; surely that's long enough. |
| 104 | const forever = mathscip-go gomod github.com/golang/go/src go1.25.0 math/.MaxInt64scip-go gomod github.com/golang/go/src go1.25.0 math/MaxInt64. |
| 105 | if idleTimeout <= 0 { |
| 106 | idleTimeout = forever |
| 107 | } |
| 108 | connTimer := timescip-go gomod github.com/golang/go/src go1.25.0 time/.NewTimerscip-go gomod github.com/golang/go/src go1.25.0 time/NewTimer().(idleTimeout) |
| 109 | defer connTimer.Stopscip-go gomod github.com/golang/go/src go1.25.0 time/Timer#Stop().() |
| 110 | |
| 111 | for { |
| 112 | select { |
| 113 | case netConn, ok := <-newConns: |
| 114 | if !ok { |
| 115 | return acceptErr |
| 116 | } |
| 117 | if activeConns == 0 && !connTimer.Stopscip-go gomod github.com/golang/go/src go1.25.0 time/Timer#Stop().() { |
| 118 | // connTimer.C may receive a value even after Stop returns. |
| 119 | // (See https://golang.org/issue/37196.) |
| 120 | <-connTimer.Cscip-go gomod github.com/golang/go/src go1.25.0 time/Timer#C. |
| 121 | } |
| 122 | activeConns++ |
| 123 | stream := NewHeaderStreamscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/NewHeaderStream().(netConn) |
| 124 | go func() { |
| 125 | conn := NewConnscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/NewConn().(stream) |
| 126 | err := server.ServeStreamscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/StreamServer#ServeStream.(ctx, conn) |
| 127 | stream.Closescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/Stream#Close.() |
| 128 | closedConns <- err |
| 129 | }() |
| 130 | |
| 131 | case err := <-closedConns: |
| 132 | if !isClosingErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/isClosingError().(err) { |
| 133 | eventscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event`/.Errorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/event`/Error().(ctx, "closed a connection", err) |
| 134 | } |
| 135 | activeConns-- |
| 136 | if activeConns == 0 { |
| 137 | connTimer.Resetscip-go gomod github.com/golang/go/src go1.25.0 time/Timer#Reset().(idleTimeout) |
| 138 | } |
| 139 | |
| 140 | case <-connTimer.Cscip-go gomod github.com/golang/go/src go1.25.0 time/Timer#C.: |
| 141 | return ErrIdleTimeoutscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/ErrIdleTimeout. |
| 142 | |
| 143 | case <-ctx.Donescip-go gomod github.com/golang/go/src go1.25.0 context/Context#Done().(): |
| 144 | return nil |
| 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
| 149 | // isClosingError reports if the error occurs normally during the process of |
| 150 | // closing a network connection. It uses imperfect heuristics that err on the |
| 151 | // side of false negatives, and should not be used for anything critical. |
| 152 | func isClosingErrorscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/jsonrpc2`/isClosingError().(err error) bool { |
| 153 | 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, ioscip-go gomod github.com/golang/go/src go1.25.0 io/.EOFscip-go gomod github.com/golang/go/src go1.25.0 io/EOF.) { |
| 154 | return true |
| 155 | } |
| 156 | // Per https://github.com/golang/go/issues/4373, this error string should not |
| 157 | // change. This is not ideal, but since the worst that could happen here is |
| 158 | // some superfluous logging, it is acceptable. |
| 159 | if err.Error() == "use of closed network connection" { |
| 160 | return true |
| 161 | } |
| 162 | return false |
| 163 | } |