File: client.go

package info (click to toggle)
golang-github-hashicorp-net-rpc-msgpackrpc 0.0~git20151116.0.a14192a-1.1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, sid, trixie
  • size: 72 kB
  • sloc: makefile: 2
file content (43 lines) | stat: -rw-r--r-- 1,136 bytes parent folder | download | duplicates (4)
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
package msgpackrpc

import (
	"errors"
	"net/rpc"
	"sync/atomic"

	"github.com/hashicorp/go-multierror"
)

var (
	// nextCallSeq is used to assign a unique sequence number
	// to each call made with CallWithCodec
	nextCallSeq uint64
)

// CallWithCodec is used to perform the same actions as rpc.Client.Call but
// in a much cheaper way. It assumes the underlying connection is not being
// shared with multiple concurrent RPCs. The request/response must be syncronous.
func CallWithCodec(cc rpc.ClientCodec, method string, args interface{}, resp interface{}) error {
	request := rpc.Request{
		Seq:           atomic.AddUint64(&nextCallSeq, 1),
		ServiceMethod: method,
	}
	if err := cc.WriteRequest(&request, args); err != nil {
		return err
	}
	var response rpc.Response
	if err := cc.ReadResponseHeader(&response); err != nil {
		return err
	}
	if response.Error != "" {
		err := errors.New(response.Error)
		if readErr := cc.ReadResponseBody(nil); readErr != nil {
			err = multierror.Append(err, readErr)
		}
		return rpc.ServerError(err.Error())
	}
	if err := cc.ReadResponseBody(resp); err != nil {
		return err
	}
	return nil
}