/
clientconn.go
114 lines (92 loc) · 2.82 KB
/
clientconn.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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* 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 thrift
import (
"context"
"fmt"
)
// ClientConn holds all the connection information for a thrift client
type ClientConn struct {
proto Protocol
seqID int32
}
// NewClientConn creates a new ClientConn object using a protocol
func NewClientConn(proto Protocol) ClientConn {
return ClientConn{
proto: proto,
}
}
// IRequest represents a request to be sent to a thrift endpoint
type IRequest interface {
Write(p Format) error
}
// IResponse represents a response received from a thrift call
type IResponse interface {
Read(p Format) error
}
// Close closes the client connection
func (cc *ClientConn) Close() error {
return cc.proto.Close()
}
// SendMsg sends a request to a given thrift endpoint
func (cc *ClientConn) SendMsg(ctx context.Context, method string, req IRequest, msgType MessageType) error {
cc.seqID++
if err := setRequestHeaders(ctx, cc.proto); err != nil {
return err
}
if err := cc.proto.WriteMessageBegin(method, msgType, cc.seqID); err != nil {
return err
}
if err := req.Write(cc.proto); err != nil {
return err
}
if err := cc.proto.WriteMessageEnd(); err != nil {
return err
}
return cc.proto.Flush()
}
// RecvMsg receives the response from a call to a thrift endpoint
func (cc *ClientConn) RecvMsg(ctx context.Context, method string, res IResponse) error {
recvMethod, mTypeID, seqID, err := cc.proto.ReadMessageBegin()
if err != nil {
return err
}
if method != recvMethod {
return NewApplicationException(WRONG_METHOD_NAME, fmt.Sprintf("%s failed: wrong method name", method))
}
if cc.seqID != seqID {
return NewApplicationException(BAD_SEQUENCE_ID, fmt.Sprintf("%s failed: out of sequence response", method))
}
switch mTypeID {
case REPLY:
if err := res.Read(cc.proto); err != nil {
return err
}
return cc.proto.ReadMessageEnd()
case EXCEPTION:
err := NewApplicationException(UNKNOWN_APPLICATION_EXCEPTION, "Unknown exception")
recvdErr, readErr := err.Read(cc.proto)
if readErr != nil {
return readErr
}
if msgEndErr := cc.proto.ReadMessageEnd(); msgEndErr != nil {
return msgEndErr
}
return recvdErr
default:
return NewApplicationException(INVALID_MESSAGE_TYPE_EXCEPTION, fmt.Sprintf("%s failed: invalid message type", method))
}
}