-
Notifications
You must be signed in to change notification settings - Fork 110
/
Copy pathrpc.go
148 lines (124 loc) · 4.17 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package lksdk
import (
"fmt"
"time"
"github.com/livekit/protocol/livekit"
)
type RpcErrorCode uint32
const (
RpcApplicationError RpcErrorCode = 1500 + iota
RpcConnectionTimeout
RpcResponseTimeout
RpcRecipientDisconnected
RpcResponsePayloadTooLarge
RpcSendFailed
)
const (
RpcUnsupportedMethod RpcErrorCode = 1400 + iota
RpcRecipientNotFound
RpcRequestPayloadTooLarge
RpcUnsupportedServer
RpcUnsupportedVersion
)
const (
MaxMessageBytes = 256
MaxDataBytes = 15360 // 15KiB
// Maximum payload size for RPC requests and responses. If a payload exceeds this size,
// the RPC call will fail with a RpcRequestPayloadTooLarge(1402) or RpcResponsePayloadTooLarge(1504) error.
MaxPayloadBytes = 15360 // 15KiB
)
var rpcErrorMessages = map[RpcErrorCode]string{
RpcApplicationError: "Application error in method handler",
RpcConnectionTimeout: "Connection timeout",
RpcResponseTimeout: "Response timeout",
RpcRecipientDisconnected: "Recipient disconnected",
RpcResponsePayloadTooLarge: "Response payload too large",
RpcSendFailed: "Failed to send",
RpcUnsupportedMethod: "Method not supported at destination",
RpcRecipientNotFound: "Recipient not found",
RpcRequestPayloadTooLarge: "Request payload too large",
RpcUnsupportedServer: "RPC not supported by server",
RpcUnsupportedVersion: "Unsupported RPC version",
}
// Parameters for initiating an RPC call
type PerformRpcParams struct {
// The identity of the destination participant
DestinationIdentity string
// The name of the method to call
Method string
// The method payload
Payload string
// Timeout for receiving a response after initial connection. Default: 10000ms
ResponseTimeout *time.Duration
}
// Data passed to method handler for incoming RPC invocations
type RpcInvocationData struct {
// The unique request ID. Will match at both sides of the call, useful for debugging or logging.
RequestID string
// The unique participant identity of the caller.
CallerIdentity string
// The payload of the request. User-definable format, could be JSON for example.
Payload string
// The maximum time the caller will wait for a response.
ResponseTimeout time.Duration
}
// Specialized error handling for RPC methods.
//
// Instances of this type, when thrown in a method handler, will have their `message`
// serialized and sent across the wire. The sender will receive an equivalent error on the other side.
//
// Built-in types are included but developers may use any string, with a max length of 256 bytes.
type RpcError struct {
Code RpcErrorCode
Message string
Data *string
}
// Creates an error object with the given code and message, plus an optional data payload.
//
// If thrown in an RPC method handler, the error will be sent back to the caller.
//
// Error codes 1001-1999 are reserved for built-in errors.
//
// Maximum message length is 256 bytes, and maximum data payload length is 15KiB.
// If a payload exceeds these limits, it will be truncated.
func NewRpcError(code RpcErrorCode, message string, data *string) *RpcError {
err := &RpcError{Code: code, Message: truncateBytes(message, MaxMessageBytes)}
if data != nil {
truncatedData := truncateBytes(*data, MaxDataBytes)
err.Data = &truncatedData
}
return err
}
func fromProto(proto *livekit.RpcError) *RpcError {
return &RpcError{
Code: RpcErrorCode(proto.Code),
Message: proto.Message,
Data: &proto.Data,
}
}
func (e RpcError) toProto() *livekit.RpcError {
err := &livekit.RpcError{
Code: uint32(e.Code),
Message: e.Message,
}
if e.Data != nil {
err.Data = *e.Data
}
return err
}
func (e *RpcError) Error() string {
return fmt.Sprintf("RpcError %d: %s", e.Code, e.Message)
}
// Creates an error object with a built-in (or reserved) code and optional data payload.
func rpcErrorFromBuiltInCodes(code RpcErrorCode, data *string) *RpcError {
return NewRpcError(code, rpcErrorMessages[code], data)
}
type rpcPendingAckHandler struct {
resolve func()
participantIdentity string
}
type rpcPendingResponseHandler struct {
resolve func(payload *string, error *RpcError)
participantIdentity string
}
type RpcHandlerFunc func(data RpcInvocationData) (string, error)