2019-07-11 10:57:35 -06:00
|
|
|
// Copyright 2019 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
package jsonrpc2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-04-01 07:35:06 -06:00
|
|
|
"fmt"
|
2020-03-31 19:00:00 -06:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"golang.org/x/tools/internal/telemetry/event"
|
2019-07-11 10:57:35 -06:00
|
|
|
)
|
|
|
|
|
2020-03-30 15:09:42 -06:00
|
|
|
// Handler is invoked to handle incoming requests.
|
2020-04-09 21:54:23 -06:00
|
|
|
// The Replier sends a reply to the request and must be called exactly once.
|
|
|
|
type Handler func(ctx context.Context, reply Replier, req *Request) error
|
|
|
|
|
|
|
|
// Replier is passed to handlers to allow them to reply to the request.
|
|
|
|
// If err is set then result will be ignored.
|
|
|
|
type Replier func(ctx context.Context, result interface{}, err error) error
|
2020-03-30 15:09:42 -06:00
|
|
|
|
|
|
|
// MethodNotFound is a Handler that replies to all call requests with the
|
|
|
|
// standard method not found response.
|
|
|
|
// This should normally be the final handler in a chain.
|
2020-04-09 21:54:23 -06:00
|
|
|
func MethodNotFound(ctx context.Context, reply Replier, r *Request) error {
|
|
|
|
return reply(ctx, nil, fmt.Errorf("%w: %q", ErrMethodNotFound, r.Method))
|
2020-04-01 07:35:06 -06:00
|
|
|
}
|
|
|
|
|
2020-04-09 21:54:23 -06:00
|
|
|
// MustReplyHandler creates a Handler that panics if the wrapped handler does
|
2020-04-01 07:35:06 -06:00
|
|
|
// not call Reply for every request that it is passed.
|
2020-04-09 21:54:23 -06:00
|
|
|
func MustReplyHandler(handler Handler) Handler {
|
|
|
|
return func(ctx context.Context, reply Replier, req *Request) error {
|
2020-04-10 08:56:00 -06:00
|
|
|
called := false
|
|
|
|
err := handler(ctx, func(ctx context.Context, result interface{}, err error) error {
|
|
|
|
if called {
|
|
|
|
panic(fmt.Errorf("request %q replied to more than once", req.Method))
|
|
|
|
}
|
|
|
|
called = true
|
|
|
|
return reply(ctx, result, err)
|
|
|
|
}, req)
|
|
|
|
if !called {
|
2020-04-01 07:35:06 -06:00
|
|
|
panic(fmt.Errorf("request %q was never replied to", req.Method))
|
|
|
|
}
|
|
|
|
return err
|
2019-07-11 10:57:35 -06:00
|
|
|
}
|
|
|
|
}
|
2020-03-31 19:00:00 -06:00
|
|
|
|
2020-04-07 19:35:47 -06:00
|
|
|
// CancelHandler returns a handler that supports cancellation, and a function
|
2020-03-31 19:00:00 -06:00
|
|
|
// that can be used to trigger canceling in progress requests.
|
2020-04-07 19:35:47 -06:00
|
|
|
func CancelHandler(handler Handler) (Handler, func(id ID)) {
|
2020-03-31 19:00:00 -06:00
|
|
|
var mu sync.Mutex
|
|
|
|
handling := make(map[ID]context.CancelFunc)
|
2020-04-09 21:54:23 -06:00
|
|
|
wrapped := func(ctx context.Context, reply Replier, req *Request) error {
|
2020-03-31 19:00:00 -06:00
|
|
|
if req.ID != nil {
|
|
|
|
cancelCtx, cancel := context.WithCancel(ctx)
|
|
|
|
ctx = cancelCtx
|
|
|
|
mu.Lock()
|
|
|
|
handling[*req.ID] = cancel
|
|
|
|
mu.Unlock()
|
2020-04-10 08:56:00 -06:00
|
|
|
innerReply := reply
|
|
|
|
reply = func(ctx context.Context, result interface{}, err error) error {
|
2020-03-31 19:00:00 -06:00
|
|
|
mu.Lock()
|
|
|
|
delete(handling, *req.ID)
|
|
|
|
mu.Unlock()
|
2020-04-10 08:56:00 -06:00
|
|
|
return innerReply(ctx, result, err)
|
|
|
|
}
|
2020-03-31 19:00:00 -06:00
|
|
|
}
|
2020-04-09 21:54:23 -06:00
|
|
|
return handler(ctx, reply, req)
|
2020-03-31 19:00:00 -06:00
|
|
|
}
|
|
|
|
return wrapped, func(id ID) {
|
|
|
|
mu.Lock()
|
|
|
|
cancel, found := handling[id]
|
|
|
|
mu.Unlock()
|
|
|
|
if found {
|
|
|
|
cancel()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AsyncHandler returns a handler that processes each request goes in its own
|
|
|
|
// goroutine.
|
|
|
|
// The handler returns immediately, without the request being processed.
|
|
|
|
// Each request then waits for the previous request to finish before it starts.
|
|
|
|
// This allows the stream to unblock at the cost of unbounded goroutines
|
|
|
|
// all stalled on the previous one.
|
|
|
|
func AsyncHandler(handler Handler) Handler {
|
|
|
|
nextRequest := make(chan struct{})
|
|
|
|
close(nextRequest)
|
2020-04-09 21:54:23 -06:00
|
|
|
return func(ctx context.Context, reply Replier, req *Request) error {
|
2020-03-31 19:00:00 -06:00
|
|
|
waitForPrevious := nextRequest
|
|
|
|
nextRequest = make(chan struct{})
|
|
|
|
unlockNext := nextRequest
|
2020-04-10 08:56:00 -06:00
|
|
|
innerReply := reply
|
|
|
|
reply = func(ctx context.Context, result interface{}, err error) error {
|
|
|
|
close(unlockNext)
|
|
|
|
return innerReply(ctx, result, err)
|
|
|
|
}
|
2020-03-31 19:00:00 -06:00
|
|
|
_, queueDone := event.StartSpan(ctx, "queued")
|
|
|
|
go func() {
|
|
|
|
<-waitForPrevious
|
|
|
|
queueDone()
|
2020-04-09 21:54:23 -06:00
|
|
|
if err := handler(ctx, reply, req); err != nil {
|
2020-03-31 19:00:00 -06:00
|
|
|
event.Error(ctx, "jsonrpc2 async message delivery failed", err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|