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"
|
|
|
|
|
2020-04-17 07:32:56 -06:00
|
|
|
"golang.org/x/tools/internal/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.
|
2020-04-12 20:47:10 -06:00
|
|
|
type Handler func(ctx context.Context, reply Replier, req Request) error
|
2020-04-09 21:54:23 -06:00
|
|
|
|
|
|
|
// 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-12 20:47:10 -06:00
|
|
|
func MethodNotFound(ctx context.Context, reply Replier, req Request) error {
|
|
|
|
return reply(ctx, nil, fmt.Errorf("%w: %q", ErrMethodNotFound, req.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 {
|
2020-04-12 20:47:10 -06:00
|
|
|
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 {
|
2020-04-12 20:47:10 -06:00
|
|
|
panic(fmt.Errorf("request %q replied to more than once", req.Method()))
|
2020-04-10 08:56:00 -06:00
|
|
|
}
|
|
|
|
called = true
|
|
|
|
return reply(ctx, result, err)
|
|
|
|
}, req)
|
|
|
|
if !called {
|
2020-04-12 20:47:10 -06:00
|
|
|
panic(fmt.Errorf("request %q was never replied to", req.Method()))
|
2020-04-01 07:35:06 -06:00
|
|
|
}
|
|
|
|
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-12 20:47:10 -06:00
|
|
|
wrapped := func(ctx context.Context, reply Replier, req Request) error {
|
|
|
|
if call, ok := req.(*Call); ok {
|
2020-03-31 19:00:00 -06:00
|
|
|
cancelCtx, cancel := context.WithCancel(ctx)
|
|
|
|
ctx = cancelCtx
|
|
|
|
mu.Lock()
|
2020-04-12 20:47:10 -06:00
|
|
|
handling[call.ID()] = cancel
|
2020-03-31 19:00:00 -06:00
|
|
|
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()
|
2020-04-12 20:47:10 -06:00
|
|
|
delete(handling, call.ID())
|
2020-03-31 19:00:00 -06:00
|
|
|
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-12 20:47:10 -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-04-20 10:14:12 -06:00
|
|
|
_, queueDone := event.Start(ctx, "queued")
|
2020-03-31 19:00:00 -06:00
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|