首页 技术 正文
技术 2022年11月9日
0 收藏 574 点赞 3,400 浏览 2782 个字

昨天需要一个线下脚本进行单播推送,大约有1kw个用户,考虑到推送速度就临时搞了个请求线上的一个脚本

/**
* 临时支持invoke单播推送
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include "ghttp.h"#include "log.h"//调试模式
#define DEBUGS
//最大线程数
#define MAX_THREADS 30#ifndef DEBUGS
char *global_uri = "xxx";
#else
char *global_uri = "xxx";
#endifstruct active_t{
pthread_mutex_t active_mutex;
pthread_cond_t active_cond;
int active;
}active_struct;const char *global_request_conf = "conf/request.conf";void *work_fun(void *arg);static void _init_main(){
open_log();
print_log(DEBUG, "%s", "start...");
}static void _shut_down(){
print_log(DEBUG, "%s", "end...");
close_log();
}static void error_die(const char *msg){
perror(msg);
exit();
}/**
* 请求mapi
*/
int mapi_push(char *data, char *ret, int ret_len){
ghttp_request *request = NULL;
request = ghttp_request_new();
ghttp_set_uri(request, global_uri);
ghttp_set_type(request, ghttp_type_post);
ghttp_set_header(request, http_hdr_Connection, "close");
ghttp_set_header(request, http_hdr_Content_Type, "application/x-www-form-urlencoded");
ghttp_set_body(request, data, strlen(data));
ghttp_prepare(request);
int status = ghttp_process(request);
if(status == ghttp_error){
return -;
}
memset(ret, , ret_len);
strncpy(ret, ghttp_get_body(request), ghttp_get_body_len(request));
ret[strlen(ret)] = '\0';
int http_code = ghttp_status_code(request);
ghttp_request_destroy(request);
return http_code;
}int main(){
_init_main(); FILE *fp = fopen(global_request_conf, "r");
if(!fp){
error_die("fopen error");
}
char buf[];
memset(buf, , );
pthread_t thid;
while(!feof(fp) && fgets(buf, , fp) != NULL){
//max thread
pthread_mutex_lock(&active_struct.active_mutex);
while(active_struct.active >= MAX_THREADS){
pthread_cond_wait(&active_struct.active_cond, &active_struct.active_mutex);
}
pthread_mutex_unlock(&active_struct.active_mutex);
//run
pthread_create(&thid, NULL, work_fun, (void *)buf);
if(!thid){
printf("create thread error");
continue;
}
/*active+1*/
pthread_mutex_lock(&active_struct.active_mutex);
active_struct.active++;
pthread_mutex_unlock(&active_struct.active_mutex);
}
//wait for all thread done
pthread_mutex_lock(&active_struct.active_mutex);
while(active_struct.active != ){
pthread_cond_wait(&active_struct.active_cond, &active_struct.active_mutex);
}
pthread_mutex_unlock(&active_struct.active_mutex);
//clear
_shut_down();
}/**
* thread fun
*/
void *work_fun(void *arg){
char *data = (char *)arg;
pthread_detach(pthread_self());
char ret[];
int http_code = mapi_push(data, ret, );
if(http_code == ){
if(strncmp(ret, "{\"errno\":0", ) != ){
print_log(DEBUG, "errno:[2] http_cdoe:[%d] data:[%s] ret:[%s]", http_code, data, ret);
}
}else{
print_log(DEBUG, "errno:[1] http_cdoe:[%d] data:[%s] ret:[%s]", http_code, data, ret);
} //printf("%d, %s, %s\n", http_code, data, ret);
//notice main thread
pthread_mutex_lock(&active_struct.active_mutex);
active_struct.active--;
pthread_cond_signal(&active_struct.active_cond);
pthread_mutex_unlock(&active_struct.active_mutex);
}

其实还有好多可以优化的点,线下执行了一下,效果和速度还行

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,492
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,907
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,740
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,495
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,133
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,297