完善日志系统,修复读取配置文件时的内存泄漏,初步添加熔断机制与指数退避机制

This commit is contained in:
2025-10-11 16:57:27 +08:00
parent f518bf5064
commit 331c6b9f89
14 changed files with 104 additions and 43 deletions

View File

@ -6,6 +6,11 @@ int give_upjobs(indiector *self)
{
if(self == NULL)
{
return -1;
}
}
int init_indector(indiector *self)
{
}

View File

@ -1,5 +1,10 @@
#ifndef ERROPROCESS
#define EROPROCESS
#define ERROPROCESS
#define BASE_INDIECTOR 2
#define MAX_index 5
#define CIR_TIME 20
typedef struct jobs
{
struct jobs* next;
@ -13,8 +18,7 @@ typedef struct indiector
int retreat_index;//退避指数
jobs *head_job;
jobs *rear_job;
int (*give_upjobs)(struct indiector *);
}indiector;
int give_upjobs(indiector *self);
#endif

View File

@ -2,14 +2,16 @@
#include "network.h"
#include "swap.h"
#include "http_rel.h"
#include "http/http_rel.h"
#include "cJSON.h"
#include "tools/log/log.h"
#include "tools/quit/quit.h"
#include "erroprocess/erroprocess.h"
#include <semaphore.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <string.h>
#include <stddef.h>
#include <errno.h>
@ -75,9 +77,10 @@ int rbt_parse_json(const char *json_text, rbt_msg *out)
return 0; // 成功
}
int init_network(int port)
int init_http_network(int port)
{
int fd = socket(AF_INET, SOCK_STREAM, 0);
fcntl(fd, F_GETFL);
struct sockaddr_in addr = {0};
addr.sin_family = AF_INET; // 和 socket() 一致
addr.sin_port = htons(port); // 端口号必须网络字节序
@ -92,7 +95,7 @@ ssize_t read_req(int fd, void *buf)
// TODO 修改读取任务函数
ssize_t n = read(fd, buf, MAX_MESSAGE_BUF);
if (n == 0) /* 写端已关闭,管道永不会再有数据 */
return -1;
return 0;
return (n > 0) ? n : -1;
}
@ -163,7 +166,7 @@ void *pth_module(void *args_p)
for(;;){
//线程池中,单个线程模型
char req[64*1024];
char req[64];
//从管道中读取请求,并解析,无内容时休眠
int n = read_req(pmd->fifo_fd[0],req);
//管道关闭时退出;
@ -219,43 +222,48 @@ int server_run(int port,int fifo_fd,netm *self)
ev.data.fd = fifo_fd;
epoll_ctl(epfd, EPOLL_CTL_ADD, fifo_fd, &ev);
char iss_buf[256];
self->http_fd = init_network(port);
self->http_fd = init_http_network(port);
ev.data.fd = self->http_fd;
epoll_ctl(epfd, EPOLL_CTL_ADD, self->http_fd, &ev);
struct epoll_event events;
struct epoll_event events[10];
self->epoll_fd = epfd;
for(;;)
{
/*工作循环-----------------------------*/
int nf = epoll_wait(epfd,&events,1,-1);
int nf = epoll_wait(epfd,events,10,-1);
printf("%d\n",nf);
if (nf == -1) {
perror("epoll_wait");
break;
perror("epoll_wait");
break;
}
if(events.data.fd ==self->http_fd)
{
int nt_fd = accept4(self->http_fd,NULL,NULL,SOCK_NONBLOCK | SOCK_CLOEXEC);
if(nt_fd == -1)
continue;
sprintf(iss_buf,"s/%d/e",nt_fd);
self->iss_work(self,iss_buf);
}
if(events.data.fd == fifo_fd)
{
char command;
while(read(fifo_fd,&command,1)==1)
{
switch(command){
case 'q':
//退出逻辑
quit_server(self);
return 1;
break;
case 'u':
//插件更新逻辑
for(int i = 0; i<nf;i++){
break;
if(events[i].data.fd ==self->http_fd)
{
int nt_fd = accept4(self->http_fd,NULL,NULL,SOCK_NONBLOCK | SOCK_CLOEXEC);
printf("%d\n",nt_fd);
if(nt_fd == -1)
continue;
sprintf(iss_buf,"s/%d/e",nt_fd);
self->iss_work(self,iss_buf);
}
if(events[i].data.fd == fifo_fd)
{
char command;
while(read(fifo_fd,&command,1)==1)
{
switch(command){
case 'q':
//退出逻辑
quit_server(self);
return 1;
break;
case 'u':
//插件更新逻辑
break;
}
}
}
}
@ -283,5 +291,6 @@ int init_networkmanager(netm *self,int *fifo,log_manager *logmanager,int port)
self->last_alc = 0;
//初始化参数
self->logmanager = logmanager;
self->err_indictor = (indiector*)malloc(sizeof(indiector));
self->port = port;
}

View File

@ -4,7 +4,8 @@
#define MAX_POOL 10
#define MAX_MESSAGE_BUF 10240
#include <pthread.h>
#include<tools/log/log.h>
#include "tools/log/log.h"
#include "erroprocess/erroprocess.h"
#include <stdatomic.h>
//单个线程模型
typedef struct pthread_module
@ -29,6 +30,7 @@ typedef struct network_manager
pth_m pool[MAX_POOL];
int fifo_fd[2];
log_manager *logmanager;
indiector *err_indictor;
int last_alc;
int port;
int epoll_fd;