-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
46 lines (38 loc) · 1.16 KB
/
client.go
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
package mrpc
import (
"context"
"io"
"time"
"github.com/mprot/msgpack-go"
)
// Caller defines an interface for calling remote functions.
type Caller interface {
Call(ctx context.Context, req Request) (Response, error)
}
// Client is mrpc client to call service methods. A client is transport
// independent.
type Client struct {
rw io.ReadWriter
}
// NewClient creates a new mrpc client. When calling a method with Call, the
// request will be written to the writing part and the response will be read
// from the reading part of rw.
func NewClient(rw io.ReadWriter) *Client {
return &Client{rw: rw}
}
// Call calls a remote method by writing the request to the client's writer
// and reading the response from the client's reader.
func (c *Client) Call(ctx context.Context, req Request) (Response, error) {
if deadline, ok := ctx.Deadline(); ok {
timeout := time.Until(deadline)
if timeout > 0 && (req.Headers.Timeout == 0 || uint64(timeout) < req.Headers.Timeout) {
req.Headers.Timeout = uint64(timeout)
}
}
if err := msgpack.Encode(c.rw, &req); err != nil {
return Response{}, err
}
var resp Response
err := msgpack.Decode(c.rw, &resp)
return resp, err
}