forked from livekit/psrpc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
122 lines (102 loc) · 3.56 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
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
121
122
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package psrpc
import (
"context"
"time"
"google.golang.org/protobuf/proto"
)
const (
DefaultClientTimeout = time.Second * 3
DefaultAffinityTimeout = time.Second
DefaultAffinityShortCircuit = time.Millisecond * 200
)
type ClientOption func(*ClientOpts)
type ClientOpts struct {
ClientID string
Timeout time.Duration
SelectionTimeout time.Duration
ChannelSize int
EnableStreams bool
RequestHooks []ClientRequestHook
ResponseHooks []ClientResponseHook
RpcInterceptors []ClientRPCInterceptor
MultiRPCInterceptors []ClientMultiRPCInterceptor
StreamInterceptors []StreamInterceptor
}
func WithClientID(id string) ClientOption {
return func(o *ClientOpts) {
o.ClientID = id
}
}
func WithClientTimeout(timeout time.Duration) ClientOption {
return func(o *ClientOpts) {
o.Timeout = timeout
}
}
func WithClientSelectTimeout(timeout time.Duration) ClientOption {
return func(o *ClientOpts) {
o.SelectionTimeout = timeout
}
}
func WithClientChannelSize(size int) ClientOption {
return func(o *ClientOpts) {
o.ChannelSize = size
}
}
// Request hooks are called as soon as the request is made
type ClientRequestHook func(ctx context.Context, req proto.Message, info RPCInfo)
func WithClientRequestHooks(hooks ...ClientRequestHook) ClientOption {
return func(o *ClientOpts) {
o.RequestHooks = append(o.RequestHooks, hooks...)
}
}
// Response hooks are called just before responses are returned
// For multi-requests, response hooks are called on every response, and block while executing
type ClientResponseHook func(ctx context.Context, req proto.Message, info RPCInfo, res proto.Message, err error)
func WithClientResponseHooks(hooks ...ClientResponseHook) ClientOption {
return func(o *ClientOpts) {
o.ResponseHooks = append(o.ResponseHooks, hooks...)
}
}
type ClientRPCInterceptor func(info RPCInfo, next ClientRPCHandler) ClientRPCHandler
type ClientRPCHandler func(ctx context.Context, req proto.Message, opts ...RequestOption) (proto.Message, error)
func WithClientRPCInterceptors(interceptors ...ClientRPCInterceptor) ClientOption {
return func(o *ClientOpts) {
o.RpcInterceptors = append(o.RpcInterceptors, interceptors...)
}
}
type ClientMultiRPCInterceptor func(info RPCInfo, next ClientMultiRPCHandler) ClientMultiRPCHandler
type ClientMultiRPCHandler interface {
Send(ctx context.Context, msg proto.Message, opts ...RequestOption) error
Recv(msg proto.Message, err error)
Close()
}
func WithClientMultiRPCInterceptors(interceptors ...ClientMultiRPCInterceptor) ClientOption {
return func(o *ClientOpts) {
o.MultiRPCInterceptors = append(o.MultiRPCInterceptors, interceptors...)
}
}
func WithClientStreamInterceptors(interceptors ...StreamInterceptor) ClientOption {
return func(o *ClientOpts) {
o.StreamInterceptors = append(o.StreamInterceptors, interceptors...)
}
}
func WithClientOptions(opts ...ClientOption) ClientOption {
return func(o *ClientOpts) {
for _, opt := range opts {
opt(o)
}
}
}