|
9 | 9 | "sync"
|
10 | 10 | )
|
11 | 11 |
|
| 12 | +// Interface represents an interface for issuing requests that speak the JSON-RPC 2 protocol. |
12 | 13 | type Interface interface {
|
13 | 14 | Call(ctx context.Context, method string, params, result interface{}) error
|
14 | 15 |
|
@@ -40,26 +41,31 @@ type Canceler func(context.Context, *Conn, *Request)
|
40 | 41 | // Conn is a JSON RPC 2 client server connection.
|
41 | 42 | // Conn is bidirectional; it does not have a designated server or client end.
|
42 | 43 | type Conn struct {
|
43 |
| - handle Handler |
44 |
| - cancel Canceler |
45 |
| - stream Stream |
46 |
| - done chan struct{} |
47 |
| - err error |
48 |
| - seq int64 // must only be accessed using atomic operations |
49 |
| - pendingMu sync.Mutex // protects the pending map |
50 |
| - pending map[ID]chan *Response |
| 44 | + handle Handler |
| 45 | + cancel Canceler |
| 46 | + stream Stream |
| 47 | + done chan struct{} |
| 48 | + err error |
| 49 | + //nolint:structcheck |
| 50 | + seq int64 // must only be accessed using atomic operations |
| 51 | + //nolint:structcheck |
| 52 | + pendingMu sync.Mutex // protects the pending map |
| 53 | + pending map[ID]chan *Response //nolint:structcheck |
51 | 54 | }
|
52 | 55 |
|
53 | 56 | var _ Interface = (*Conn)(nil)
|
54 | 57 |
|
| 58 | +// Options represents a functional options. |
55 | 59 | type Options func(*Conn)
|
56 | 60 |
|
| 61 | +// WithHandler apply custom hander to Conn. |
57 | 62 | func WithHandler(h Handler) Options {
|
58 | 63 | return func(c *Conn) {
|
59 | 64 | c.handle = h
|
60 | 65 | }
|
61 | 66 | }
|
62 | 67 |
|
| 68 | +// WithCanceler apply custom canceler to Conn. |
63 | 69 | func WithCanceler(cancel Canceler) Options {
|
64 | 70 | return func(c *Conn) {
|
65 | 71 | c.cancel = cancel
|
@@ -101,14 +107,19 @@ func NewConn(ctx context.Context, s Stream, options ...Options) *Conn {
|
101 | 107 |
|
102 | 108 | func (c *Conn) run(ctx context.Context) error { return nil }
|
103 | 109 |
|
| 110 | +// Call sends a request over the connection and then waits for a response. |
104 | 111 | func (c *Conn) Call(ctx context.Context, method string, params, result interface{}) error { return nil }
|
105 | 112 |
|
| 113 | +// Reply sends a reply to the given request. |
106 | 114 | func (c *Conn) Reply(ctx context.Context, req *Request, result interface{}, err error) error {
|
107 | 115 | return nil
|
108 | 116 | }
|
109 | 117 |
|
| 118 | +// Notify is called to send a notification request over the connection. |
110 | 119 | func (c *Conn) Notify(ctx context.Context, method string, params interface{}) error { return nil }
|
111 | 120 |
|
| 121 | +// Cancel cancels a pending Call on the server side. |
112 | 122 | func (c *Conn) Cancel(id ID) {}
|
113 | 123 |
|
| 124 | +// Wait blocks until the connection is terminated, and returns any error that cause the termination. |
114 | 125 | func (c *Conn) Wait(ctx context.Context) error { return nil }
|
0 commit comments