forked from anqin/trident
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_client_impl.h
131 lines (93 loc) · 3.24 KB
/
rpc_client_impl.h
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
// Copyright (c) 2014 The Trident Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
//
#ifndef _TRIDENT_RPC_CLIENT_IMPL_H_
#define _TRIDENT_RPC_CLIENT_IMPL_H_
#include <list>
#include <map>
#include <trident/common_internal.h>
#include <trident/rpc_client.h>
#include <trident/rpc_client_stream.h>
#include <trident/rpc_endpoint.h>
#include <trident/thread_group_impl.h>
#include <trident/timer_worker.h>
#include <trident/rpc_timeout_manager.h>
namespace trident {
class RpcClientImpl: public trident::enable_shared_from_this<RpcClientImpl>
{
public:
static const int MAINTAIN_INTERVAL_IN_MS = 100;
public:
explicit RpcClientImpl(const RpcClientOptions& options);
virtual ~RpcClientImpl();
void Start();
void Stop();
RpcClientOptions GetOptions();
void ResetOptions(const RpcClientOptions& options);
int ConnectionCount();
// Rpc call method to remote endpoint.
//
// The call can be done in following cases:
// * send failed
// * timeouted
// * response received
void CallMethod(const google::protobuf::Message* request,
google::protobuf::Message* response,
const RpcControllerImplPtr& cntl);
const ThreadGroupImplPtr& GetCallbackThreadGroup() const;
bool ResolveAddress(const std::string& address,
RpcEndpoint* endpoint);
private:
// Get stream for "remote_endpoint". Return null ptr if failed.
RpcClientStreamPtr FindOrCreateStream(const RpcEndpoint& remote_endpoint);
void StopStreams();
void ClearStreams();
void DoneCallback(google::protobuf::Message* response,
const RpcControllerImplPtr& cntl);
bool ShouldStreamRemoved(const RpcClientStreamPtr& stream);
void TimerMaintain(const PTime& now);
uint64 GenerateSequenceId();
private:
struct FlowControlItem
{
int token; // always <= 0
RpcClientStream* stream;
FlowControlItem(int t, RpcClientStream* s) : token(t), stream(s) {}
// comparator: nearer to zero, higher priority
bool operator< (const FlowControlItem& o) const
{
return token > o.token;
}
};
private:
RpcClientOptions _options;
volatile bool _is_running;
MutexLock _start_stop_lock;
AtomicCounter64 _next_request_id;
PTime _epoch_time;
int64 _ticks_per_second;
int64 _last_maintain_ticks;
int64 _last_print_connection_ticks;
int64 _slice_count;
int64 _slice_quota_in;
int64 _slice_quota_out;
int64 _max_pending_buffer_size;
int64 _keep_alive_ticks;
int64 _print_connection_interval_ticks;
FlowControllerPtr _flow_controller;
ThreadGroupImplPtr _maintain_thread_group;
ThreadGroupImplPtr _callback_thread_group;
ThreadGroupImplPtr _work_thread_group;
TimerWorkerPtr _timer_worker;
RpcTimeoutManagerPtr _timeout_manager;
typedef std::map<RpcEndpoint, RpcClientStreamPtr> StreamMap;
StreamMap _stream_map;
FastLock _stream_map_lock;
volatile int _live_stream_count;
TRIDENT_DISALLOW_EVIL_CONSTRUCTORS(RpcClientImpl);
}; // class RpcClientImpl
} // namespace trident
#endif // _TRIDENT_RPC_CLIENT_IMPL_H_
/* vim: set ts=4 sw=4 sts=4 tw=100 */