/
rpc.go
120 lines (99 loc) · 2.43 KB
/
rpc.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
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package rpc
import (
"context"
"encoding/binary"
"errors"
"sync"
// "sync"
pb "github.com/MemeLabs/protobuf/pkg/apis/rpc"
"google.golang.org/protobuf/proto"
)
const (
callbackMethod = "CALLBACK"
cancelMethod = "CANCEL"
anyURLPrefix = "strims.gg/"
)
func recoverError(v interface{}) error {
switch err := v.(type) {
case nil:
return nil
case error:
return err
case string:
return errors.New(err)
default:
return errors.New("unknown error")
}
}
var typeOfError = (&pb.Error{}).ProtoReflect().Type()
var typeOfClose = (&pb.Close{}).ProtoReflect().Type()
// ErrClose returned when the the server closes a streaming response
var ErrClose = errors.New("response closed")
// ErrInvalidType returned when the request or response type doesn't match the
// expected value
var ErrInvalidType = errors.New("invaild type")
func unmarshalResponse(kind pb.Call_Kind, b []byte, v proto.Message) error {
switch kind {
case pb.Call_CALL_KIND_DEFAULT:
return proto.Unmarshal(b, v)
case pb.Call_CALL_KIND_ERROR:
ev := &pb.Error{}
if err := proto.Unmarshal(b, ev); err != nil {
return err
}
return ev
case pb.Call_CALL_KIND_CLOSE:
return ErrClose
default:
return ErrInvalidType
}
}
var bufPool = sync.Pool{
New: func() interface{} {
return make([]byte, 1024)
},
}
// SendFunc ...
type SendFunc func(context.Context, *pb.Call) error
func send(ctx context.Context, id, parentID uint64, method string, kind pb.Call_Kind, arg proto.Message, fn SendFunc) error {
b := bufPool.Get().([]byte)[:0]
b, err := proto.MarshalOptions{}.MarshalAppend(b, arg)
defer bufPool.Put(b)
if err != nil {
return err
}
rc := &pb.Call{
Id: id,
ParentId: parentID,
Method: method,
Kind: kind,
Argument: b,
Headers: map[string][]byte{},
}
return fn(ctx, rc)
}
func marshalAppendLengthDelimited(b []byte, m proto.Message) ([]byte, error) {
opt := proto.MarshalOptions{}
ms := opt.Size(m)
if cap(b) < binary.MaxVarintLen32 {
b = make([]byte, binary.MaxVarintLen32+ms)
}
n := binary.PutUvarint(b[:binary.MaxVarintLen32], uint64(ms))
return opt.MarshalAppend(b[:n], m)
}
// ResponseFunc ...
type ResponseFunc func() error
// Transport ...
type Transport interface {
Call(*CallOut, ResponseFunc) error
Listen() error
}
// Dialer ...
type Dialer interface {
Dial(context.Context, Dispatcher) (Transport, error)
}
// ParentCallAccessor ...
type ParentCallAccessor interface {
ParentCallIn() *CallIn
ParentCallOut() *CallOut
}