Skip to content

Commit b315104

Browse files
author
blog.s135.com
committed
增加JSON方式查看队列状态,方便客服端程序处理。支持实时修改定时更新内存内容到磁盘的间隔时间。
1 parent 75af193 commit b315104

2 files changed

Lines changed: 45 additions & 13 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Makefile for httpsqs
22
CC=gcc
3-
CFLAGS=-O2 -Wall -levent -ltokyocabinet -lz -lbz2 -lrt -lpthread -lm -lc
3+
CFLAGS=-O2 -Wall -levent -ltokyocabinet -lz -lrt -lpthread -lm -lc
44

55
httpsqs: httpsqs.c
66
$(CC) $(CFLAGS) httpsqs.c -o httpsqs

httpsqs.c

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
HTTP Simple Queue Service - httpsqs v1.3.20100617
2+
HTTP Simple Queue Service - httpsqs v1.3.20100621
33
Author: Zhang Yan (http://blog.s135.com), E-mail: [email protected]
44
This is free software, and you are welcome to modify and redistribute it under the New BSD License
55
*/
@@ -41,7 +41,6 @@ This is free software, and you are welcome to modify and redistribute it under t
4141
/* 全局设置 */
4242
TCBDB *httpsqs_db_tcbdb; /* 数据表 */
4343
int httpsqs_settings_syncinterval; /* 同步更新内容到磁盘的间隔时间 */
44-
char *httpsqs_settings_keepalive; /* Keep-Alive 时间 */
4544
char *httpsqs_settings_pidfile; /* PID文件 */
4645

4746
/* 创建多层目录的函数 */
@@ -117,15 +116,14 @@ char *urldecode(char *input_str)
117116
static void show_help(void)
118117
{
119118
char *b = "--------------------------------------------------------------------------------------------------\n"
120-
"HTTP Simple Queue Service - httpsqs v" VERSION " (June 19, 2010)\n\n"
119+
"HTTP Simple Queue Service - httpsqs v" VERSION " (June 22, 2010)\n\n"
121120
"Author: Zhang Yan (http://blog.s135.com), E-mail: [email protected]\n"
122121
"This is free software, and you are welcome to modify and redistribute it under the New BSD License\n"
123122
"\n"
124123
"-l <ip_addr> interface to listen on, default is 0.0.0.0\n"
125124
"-p <num> TCP port number to listen on (default: 1218)\n"
126125
"-x <path> database directory (example: /opt/httpsqs/data)\n"
127126
"-t <second> timeout for an http request (default: 3)\n"
128-
"-k <second> keep-alive time (default: 15)\n"
129127
"-s <second> the interval to sync updated contents to the disk (default: 5)\n"
130128
"-c <num> the maximum number of non-leaf nodes to be cached (default: 10000)\n"
131129
"-m <size> database memory cache size in MB (default: 100)\n"
@@ -271,6 +269,15 @@ char *httpsqs_view(const char* httpsqs_input_name, int pos)
271269
return queue_value;
272270
}
273271

272+
/* 修改定时更新内存内容到磁盘的间隔时间,返回间隔时间(秒) */
273+
static int httpsqs_synctime(int httpsqs_input_num)
274+
{
275+
if (httpsqs_input_num >= 1) {
276+
httpsqs_settings_syncinterval = httpsqs_input_num;
277+
}
278+
return httpsqs_settings_syncinterval;
279+
}
280+
274281
/* 获取本次“入队列”操作的队列写入点 */
275282
static int httpsqs_now_putpos(const char* httpsqs_input_name)
276283
{
@@ -401,7 +408,7 @@ void httpsqs_handler(struct evhttp_request *req, void *arg)
401408
} else {
402409
evhttp_add_header(req->output_headers, "Content-Type", "text/plain");
403410
}
404-
evhttp_add_header(req->output_headers, "Keep-Alive", httpsqs_settings_keepalive);
411+
evhttp_add_header(req->output_headers, "Connection", "keep-alive");
405412
evhttp_add_header(req->output_headers, "Cache-Control", "no-cache");
406413
//evhttp_add_header(req->output_headers, "Cneonction", "close");
407414

@@ -483,7 +490,7 @@ void httpsqs_handler(struct evhttp_request *req, void *arg)
483490
}
484491
}
485492
}
486-
/* 查看队列状态 */
493+
/* 查看队列状态(普通浏览方式) */
487494
else if (strcmp(httpsqs_input_opt, "status") == 0) {
488495
int maxqueue = httpsqs_read_maxqueue((char *)httpsqs_input_name); /* 最大队列数量 */
489496
int putpos = httpsqs_read_putpos((char *)httpsqs_input_name); /* 入队列写入位置 */
@@ -508,6 +515,25 @@ void httpsqs_handler(struct evhttp_request *req, void *arg)
508515
evbuffer_add_printf(buf, "Get position of queue (%s): %d\n", get_times, getpos);
509516
evbuffer_add_printf(buf, "Number of unread queue: %d\n", ungetnum);
510517
}
518+
/* 查看队列状态(JSON方式,方便客服端程序处理) */
519+
else if (strcmp(httpsqs_input_opt, "status_json") == 0) {
520+
int maxqueue = httpsqs_read_maxqueue((char *)httpsqs_input_name); /* 最大队列数量 */
521+
int putpos = httpsqs_read_putpos((char *)httpsqs_input_name); /* 入队列写入位置 */
522+
int getpos = httpsqs_read_getpos((char *)httpsqs_input_name); /* 出队列读取位置 */
523+
int ungetnum;
524+
const char *put_times;
525+
const char *get_times;
526+
if (putpos >= getpos) {
527+
ungetnum = abs(putpos - getpos); /* 尚未出队列条数 */
528+
put_times = "1";
529+
get_times = "1";
530+
} else if (putpos < getpos) {
531+
ungetnum = abs(maxqueue - getpos + putpos); /* 尚未出队列条数 */
532+
put_times = "2";
533+
get_times = "1";
534+
}
535+
evbuffer_add_printf(buf, "{\"name\":\"%s\",\"maxqueue\":%d,\"putpos\":%d,\"puttimes\":%s,\"getpos\":%d,\"gettimes\":%s,\"unread\":%d}\n", httpsqs_input_name, maxqueue, putpos, put_times, getpos, get_times, ungetnum);
536+
}
511537
/* 查看单条队列内容 */
512538
else if (strcmp(httpsqs_input_opt, "view") == 0 && httpsqs_input_pos >= 1 && httpsqs_input_pos <= 1000000000) {
513539
char *httpsqs_output_value;
@@ -535,6 +561,16 @@ void httpsqs_handler(struct evhttp_request *req, void *arg)
535561
/* 设置取消 */
536562
evbuffer_add_printf(buf, "%s", "HTTPSQS_MAXQUEUE_CANCEL");
537563
}
564+
}
565+
/* 设置定时更新内存内容到磁盘的间隔时间,最小值为1秒,最大值为10亿秒 */
566+
else if (strcmp(httpsqs_input_opt, "synctime") == 0 && httpsqs_input_num >= 1 && httpsqs_input_num <= 1000000000) {
567+
if (httpsqs_synctime(httpsqs_input_num) >= 1) {
568+
/* 设置成功 */
569+
evbuffer_add_printf(buf, "%s", "HTTPSQS_SYNCTIME_OK");
570+
} else {
571+
/* 设置取消 */
572+
evbuffer_add_printf(buf, "%s", "HTTPSQS_SYNCTIME_CANCEL");
573+
}
538574
} else {
539575
/* 命令错误 */
540576
evbuffer_add_printf(buf, "%s", "HTTPSQS_ERROR");
@@ -581,15 +617,14 @@ int main(int argc, char **argv)
581617
char *httpsqs_settings_datapath = NULL;
582618
bool httpsqs_settings_daemon = false;
583619
int httpsqs_settings_timeout = 3; /* 单位:秒 */
584-
httpsqs_settings_keepalive = "15"; /* 单位:秒 */
585620
httpsqs_settings_syncinterval = 5; /* 单位:秒 */
586621
int httpsqs_settings_cachenonleaf = 10000; /* 缓存非叶子节点数。单位:条 */
587622
int httpsqs_settings_cacheleaf = 20000; /* 缓存叶子节点数。叶子节点缓存数为非叶子节点数的两倍。单位:条 */
588623
int httpsqs_settings_mappedmemory = 104857600; /* 单位:字节 */
589624
httpsqs_settings_pidfile = "/tmp/httpsqs.pid";
590625

591626
/* process arguments */
592-
while ((c = getopt(argc, argv, "l:p:x:t:k:s:c:m:i:dh")) != -1) {
627+
while ((c = getopt(argc, argv, "l:p:x:t:s:c:m:i:dh")) != -1) {
593628
switch (c) {
594629
case 'l':
595630
httpsqs_settings_listen = strdup(optarg);
@@ -613,10 +648,7 @@ int main(int argc, char **argv)
613648
break;
614649
case 't':
615650
httpsqs_settings_timeout = atoi(optarg);
616-
break;
617-
case 'k':
618-
httpsqs_settings_keepalive = strdup(optarg);
619-
break;
651+
break;
620652
case 's':
621653
httpsqs_settings_syncinterval = atoi(optarg);
622654
break;

0 commit comments

Comments
 (0)