Debian Codegraph @

internal/jsonrpc2/handler.go

raw symbols SCIP
1 // Copyright 2019 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 jsonrpc2
6
7 import (
8 "context"
9 "fmt"
10 "sync"
11
12 "golang.org/x/tools/internal/event"
13 )
14
15 // Handler is invoked to handle incoming requests.
16 // The Replier sends a reply to the request and must be called exactly once.
17 type Handler func(ctx context.Context, reply Replier, req Request) error
18
19 // Replier is passed to handlers to allow them to reply to the request.
20 // If err is set then result will be ignored.
21 type Replier func(ctx context.Context, result any, err error) error
22
23 // MethodNotFound is a Handler that replies to all call requests with the
24 // standard method not found response.
25 // This should normally be the final handler in a chain.
26 func MethodNotFound(ctx context.Context, reply Replier, req Request) error {
27 return reply(ctx, nil, fmt.Errorf("%w: %q", ErrMethodNotFound, req.Method()))
28 }
29
30 // MustReplyHandler is a middleware that creates a Handler that panics if the
31 // wrapped handler does not call Reply for every request that it is passed.
32 func MustReplyHandler(handler Handler) Handler {
33 return func(ctx context.Context, reply Replier, req Request) error {
34 called := false
35 err := handler(ctx, func(ctx context.Context, result any, err error) error {
36 if called {
37 panic(fmt.Errorf("request %q replied to more than once", req.Method()))
38 }
39 called = true
40 return reply(ctx, result, err)
41 }, req)
42 if !called {
43 panic(fmt.Errorf("request %q was never replied to", req.Method()))
44 }
45 return err
46 }
47 }
48
49 // CancelHandler returns a handler that supports cancellation, and a function
50 // that can be used to trigger canceling in progress requests.
51 func CancelHandler(handler Handler) (Handler, func(id ID)) {
52 var mu sync.Mutex
53 handling := make(map[ID]context.CancelFunc)
54 wrapped := func(ctx context.Context, reply Replier, req Request) error {
55 if call, ok := req.(*Call); ok {
56 cancelCtx, cancel := context.WithCancel(ctx)
57 ctx = cancelCtx
58 mu.Lock()
59 handling[call.ID()] = cancel
60 mu.Unlock()
61 innerReply := reply
62 reply = func(ctx context.Context, result any, err error) error {
63 mu.Lock()
64 delete(handling, call.ID())
65 mu.Unlock()
66 return innerReply(ctx, result, err)
67 }
68 }
69 return handler(ctx, reply, req)
70 }
71 return wrapped, func(id ID) {
72 mu.Lock()
73 cancel, found := handling[id]
74 mu.Unlock()
75 if found {
76 cancel()
77 }
78 }
79 }
80
81 // AsyncHandler is a middleware that returns a handler that processes each
82 // request goes in its own goroutine.
83 // The handler returns immediately, without the request being processed.
84 // Each request then waits for the previous request to finish before it starts.
85 // This allows the stream to unblock at the cost of unbounded goroutines
86 // all stalled on the previous one.
87 func AsyncHandler(handler Handler) Handler {
88 nextRequest := make(chan struct{})
89 close(nextRequest)
90 return func(ctx context.Context, reply Replier, req Request) error {
91 waitForPrevious := nextRequest
92 nextRequest = make(chan struct{})
93 releaser := &releaser{ch: nextRequest}
94 innerReply := reply
95 reply = func(ctx context.Context, result any, err error) error {
96 releaser.release(true)
97 return innerReply(ctx, result, err)
98 }
99 _, queueDone := event.Start(ctx, "queued")
100 ctx = context.WithValue(ctx, asyncKey, releaser)
101 go func() {
102 <-waitForPrevious
103 queueDone()
104 if err := handler(ctx, reply, req); err != nil {
105 event.Error(ctx, "jsonrpc2 async message delivery failed", err)
106 }
107 }()
108 return nil
109 }
110 }
111
112 // Async, when used with the [AsyncHandler] middleware, indicates that the
113 // current jsonrpc2 request may be handled asynchronously to subsequent
114 // requests.
115 //
116 // When not used with an AsyncHandler, Async is a no-op.
117 //
118 // Async must be called at most once on each request's context (and its
119 // descendants).
120 func Async(ctx context.Context) {
121 if r, ok := ctx.Value(asyncKey).(*releaser); ok {
122 r.release(false)
123 }
124 }
125
126 type asyncKeyType struct{}
127
128 var asyncKey = asyncKeyType{}
129
130 // A releaser implements concurrency safe 'releasing' of async requests. (A
131 // request is released when it is allowed to run concurrent with other
132 // requests, via a call to [Async].)
133 type releaser struct {
134 mu sync.Mutex
135 ch chan struct{}
136 released bool
137 }
138
139 // release closes the associated channel. If soft is set, multiple calls to
140 // release are allowed.
141 func (r *releaser) release(soft bool) {
142 r.mu.Lock()
143 defer r.mu.Unlock()
144
145 if r.released {
146 if !soft {
147 panic("jsonrpc2.Async called multiple times")
148 }
149 } else {
150 close(r.ch)
151 r.released = true
152 }
153 }