fcgi Function is a C/CPP language based interface, which build on top of FastCGI with built in functional handler support, it also provided useful collection tools to enhance the facilities. It can be integrated with Nginx Fastcgi module
Nginx -- https://www.nginx.com/
fcgi library -- https://github.com/FastCGI-Archives/FastCGI.com
cmake and make
git clone --recursive https://github.com/Taymindis/fcgi-function.git
Just Include ffunc.h and ffunc.c in your project, simplest way ( applicable to Linux, Darwin and Windows platforms )
gcc profile_service.c ffunc.c ffunc.h -lfcgi -pthread -ldl -rdynamic
g++ profile_service.cpp ffunc.c ffunc.h -lfcgi -pthread -ldl -rdynamic
g++ -std=c++11 -I/home/xxx/cpplib/spdlog-0.17.0/include -DSPDLOG_FMT_PRINTF service_with_spdlog.cpp ffunc.c ffunc.h -lfcgi -pthread -ldl -rdynamic
./simple_service
it will result:-
Service starting
sock_port=2005, backlog=16
total function = 3
64 threads in process
Socket on hook 2005
Running on background by setting ffunc_conf->daemon = 1
, see example for mode details
int ffunc_main(int argc, char *argv[], ffunc_config_t *ffunc_conf) {
ffunc_conf->sock_port = 2005;
ffunc_conf->backlog = 160;
ffunc_conf->max_thread = 64;
ffunc_conf->daemon = 1; // run as daemon
ffunc_parse_function(ffunc_conf, "getProfile", "postError", "postProfile");
return 0;
}
valgrind --leak-check=full --show-leak-kinds=all --trace-children=yes ./simple_service
location /getProfile {
add_header Allow "GET, POST, HEAD" always;
if ( $request_method !~ ^(GET|HEAD)$ ) {
return 405;
}
include /etc/nginx/fastcgi_params;
fastcgi_param FN_HANDLER getProfile;
fastcgi_pass 127.0.0.1:2005;
}
location /postProfile {
add_header Allow "GET, POST, HEAD" always;
if ( $request_method !~ ^(POST)$ ) {
return 405;
}
include /etc/nginx/fastcgi_params;
fastcgi_param FN_HANDLER postProfile;
fastcgi_pass 127.0.0.1:2005;
}
<Location "/getProfile">
# Timeout values are in seconds
ProxyPass "fcgi://127.0.0.1:2005" connectiontimeout=300 timeout=300
ProxyPassReverse "fcgi://127.0.0.1:2005"
SetEnv FN_HANDLER "getProfile"
</Location>
You will see the FN_HANDLER is function name mapping with the function inside simple_service code, the fastcgi port 2005 is the service you start with(please look at step 10 for more details.
ab -c 100 -n 10000 http://127.0.0.1:80/getProfile
ab -p "payload.txt" -T application/json -c 100 -n 10000 http://127.0.0.1:80/postProfile
the payload.txt is inside the root directory
kill the process by using kill -2 <pid>
, please do not use -9 as it will direct kill the process, unless -2 is not working
cd DockerExample
docker build -t nginx_ffunc -f Dockerfile_ngx_ffunc .
docker run -d -v ~/fcgi-function/DockerExample:/tmp -p 80:80 --name testffunc nginx_ffunc
curl "http://127.0.0.1/getProfile"
Due to built in logging mechanism will slow down the process speed, suggested to use third party logging mechanism for your application layer such as:-