-
-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathsync_adapter.go
More file actions
82 lines (72 loc) · 2.89 KB
/
Copy pathsync_adapter.go
File metadata and controls
82 lines (72 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
// Copyright 2026 The Go Language Server Authors. All rights reserved.
// SPDX-License-Identifier: BSD-3-Clause
package benchmark
import (
"context"
"fmt"
"net"
"go.lsp.dev/jsonrpc2"
)
// jsonrpc2SyncAdapter drives the jsonrpc2 SyncClient — the synchronous-client
// mode — against an ordinary jsonrpc2 Conn server over a net.Pipe with NDJSON
// framing.
//
// MODE DISCLOSURE (do not quote the number without this qualifier): the
// SyncClient removes the CLIENT's background read goroutine; the calling
// goroutine owns the read loop and reads its own response directly, collapsing
// the dedicated-reader-to-Call hand-off (the third goroutine hop) that the
// concurrent Conn pays. The server is a normal Conn with a background reader, so
// this is a real RPC over a real net.Pipe transport — but it is NOT the same
// execution model as the concurrent Conn (jsonrpc2/native) or as jrpc2's
// channel.Direct, which keep a client-side reader. The tradeoff is that a
// SyncClient cannot receive server-initiated requests and serializes its calls
// (one outstanding at a time). This row is therefore reported as a distinct
// mode, analogous to how the batch rows are excluded from the strict
// apples-to-apples claim; it is "jsonrpc2's fastest in-process request path," not a
// statement that the concurrent Conn got faster.
type jsonrpc2SyncAdapter struct {
client *jsonrpc2.SyncClient
server jsonrpc2.Conn
}
func newJSONRPC2SyncAdapter(ctx context.Context) (*jsonrpc2SyncAdapter, error) {
ca, cb := net.Pipe()
client, err := jsonrpc2.NewSyncClient(jsonrpc2.NewNDJSONStream(ca))
if err != nil {
return nil, fmt.Errorf("jsonrpc2 sync adapter: %w", err)
}
server := jsonrpc2.NewConn(jsonrpc2.NewNDJSONStream(cb))
server.Go(ctx, func(ctx context.Context, req *jsonrpc2.Request) (any, error) {
return nil, nil
})
a := &jsonrpc2SyncAdapter{client: client, server: server}
if err := sanityCheck(ctx, a); err != nil {
_ = a.Close()
return nil, fmt.Errorf("jsonrpc2 sync adapter: %w", err)
}
return a, nil
}
func (a *jsonrpc2SyncAdapter) Call(ctx context.Context, method string, params []byte) ([]byte, error) {
var result jsonrpc2.RawMessage
if _, err := a.client.Call(ctx, method, rawParams(params), &result); err != nil {
return nil, err
}
return result, nil
}
func (a *jsonrpc2SyncAdapter) Notify(ctx context.Context, method string, params []byte) error {
return a.client.Notify(ctx, method, rawParams(params))
}
// Batch is unsupported by the synchronous-client mode (it serializes single
// calls and has no batch-send API); the sync row is not part of the batch
// comparison.
func (a *jsonrpc2SyncAdapter) Batch(ctx context.Context, n int) error {
return fmt.Errorf("jsonrpc2 sync adapter: Batch is not supported by SyncClient")
}
func (a *jsonrpc2SyncAdapter) Close() error {
errc := a.client.Close()
errs := a.server.Close()
<-a.server.Done()
if errc != nil {
return errc
}
return errs
}