MOD aaa-12 增加rpc的超时判断

SOL 增加rpc的超时判断
修改人:zhangliang
检视人:zhangliang
This commit is contained in:
zhanglianghy 2019-06-20 09:57:07 +08:00
parent cfc95c66c9
commit 0ad879c584
4 changed files with 24 additions and 1 deletions

View File

@ -16,6 +16,7 @@
#define BUFFER_SIZE 2048 #define BUFFER_SIZE 2048
#define RPC_VERSION "RPC/1.0" #define RPC_VERSION "RPC/1.0"
#define TIMEOUT 120 #define TIMEOUT 120
#define TIMEWARN 200
typedef enum { typedef enum {
/* RPC ret code */ /* RPC ret code */

View File

@ -118,4 +118,6 @@ void rpc_sleep(int ms);
void rpc_set_non_block(int fd); void rpc_set_non_block(int fd);
long rpc_time_msec(void);
#endif /* RPC_UTIL_H_ */ #endif /* RPC_UTIL_H_ */

View File

@ -11,6 +11,7 @@
#include "rpc_thread.h" #include "rpc_thread.h"
#include "rpc_module.h" #include "rpc_module.h"
#include "rpc_conn.h" #include "rpc_conn.h"
#include "rpc_util.h"
#include <assert.h> #include <assert.h>
#include <sys/socket.h> #include <sys/socket.h>
@ -22,6 +23,7 @@
#include <unistd.h> #include <unistd.h>
#include <time.h> #include <time.h>
#include <sys/eventfd.h> #include <sys/eventfd.h>
#include <sys/time.h>
//server: one thread multiply connection. a connection would block a thread //server: one thread multiply connection. a connection would block a thread
//client: one thread one connection //client: one thread one connection
@ -137,9 +139,11 @@ ret_code rpc_client_call(rpc_client* client, char* service_name,
int* output_len) { int* output_len) {
int n_fd = eventfd(0, 0); int n_fd = eventfd(0, 0);
rpc_response rsp; rpc_response rsp;
long time_diff;
assert(client!=NULL); assert(client!=NULL);
rsp.data = (INT_TO_POINTER(n_fd)); rsp.data = (INT_TO_POINTER(n_fd));
time_diff = rpc_time_msec();
rpc_client_call_async(client, service_name, method_name, input, input_len, rpc_client_call_async(client, service_name, method_name, input, input_len,
cb_call_ex, &rsp); cb_call_ex, &rsp);
@ -154,6 +158,13 @@ ret_code rpc_client_call(rpc_client* client, char* service_name,
int rt = select(n_fd + 1, &r_set, NULL, NULL, &tm); int rt = select(n_fd + 1, &r_set, NULL, NULL, &tm);
close(n_fd); close(n_fd);
time_diff = rpc_time_msec()-time_diff;
if(time_diff > TIMEWARN)
{
rpc_log_warn("call %s@%s cost %ld\n",service_name, method_name, time_diff);
}
if (rt == -1) { if (rt == -1) {
return RET_UNKNOWN; return RET_UNKNOWN;
} else if (rt == 0) { } else if (rt == 0) {

View File

@ -62,4 +62,13 @@ uint rpc_str_hash(constpointer v) {
return h; return h;
} }
long rpc_time_msec(void)
{
struct timespec tp = {0};
clock_gettime (CLOCK_MONOTONIC, &tp);
return(tp.tv_sec * 1000 + tp.tv_nsec / (1000 *1000));
}