fcgi原理和異步模型

1. 介紹

由于CGI解釋器的反復(fù)加載會(huì)使CGI性能低下,F(xiàn)astCGI可以將CGI解釋器保持在內(nèi)存中, 提高性能
相關(guān)地址:https://fastcgi-archives.github.io

2. Fastcgi特點(diǎn):

  • 性能
  • 簡單,容易移植
  • 語言無關(guān)
  • 進(jìn)程隔離
  • 通用性,主流WebServer都支持,nginx、apache都支持
  • 支持分布式計(jì)算,F(xiàn)astCGI提供遠(yuǎn)程運(yùn)行應(yīng)用程序的功能,這對(duì)于分發(fā)負(fù)載和管理外部Web站點(diǎn)非常有用(這點(diǎn)待驗(yàn)證是否好用,我希望的能支持異步處理)

3. 原理

  1. 簡單來說,利用后面說的spawn-fcgi(fastcgi的管理程序)啟動(dòng)fastcgi,支持啟動(dòng)多實(shí)例,并通過socket進(jìn)行數(shù)據(jù)綁定
  2. WebServer(如:nginx)在收到請(qǐng)求后,根據(jù)配置中配置要轉(zhuǎn)發(fā)的fastcgi地址,發(fā)到固定的地址,例如:fastcgi綁定的是127.0.0.1:8088
  3. fastcgi Accept并處理數(shù)據(jù)后,將響應(yīng)數(shù)據(jù)通過Socket返回給WebServer,并斷開連接,這時(shí)表示一次處理完成

管理程序 -- spawn-fcgi

下載地址:http://redmine.lighttpd.net/projects/spawn-fcgi/wiki
-f 指定調(diào)用 FastCGI的進(jìn)程的執(zhí)行程序位置
-a 綁定到 地址addr。
-p 綁定到 端口 port。
-s 綁定到 unix domain socket
-C 指定產(chǎn) 生的FastCGI的進(jìn)程數(shù), 默認(rèn)為 5。 ( 僅用 于PHP)
-P 指定產(chǎn) 生的進(jìn)程的PID文件路徑。
-F 指定產(chǎn) 生的FastCGI的進(jìn)程數(shù)( C的CGI用 這個(gè))
例子:./spawn-fcgi -a 127.0.0.1 -p 8088 -F 500 -f cgi(啟動(dòng)500個(gè)cgi程序,監(jiān)聽的端口為8088,綁定地址為127.0.0.1)

4. nginx如何配置

  1. fastcgi_pass
    fastcgi_pass address;
    address為后端的fastcgi server的地址
    可用位置:location,if in location
  2. fastcgi_index
    fastcgi_index name;
    fastcgi默認(rèn)的主頁資源
    示例:fastcgi_index index.php;
  3. fastcgi_param
    fastcgi_param parameter value [if_not_empty];
    設(shè)置傳遞給FastCGI服務(wù)器的參數(shù)值,可以是文本,變量或組合

5. 阻塞與非阻塞模式

fastcgi示例 -- 阻塞模式

#include "fcgi_stdio.h"
#include <stdlib.h>

void main(void)
{
    int count = 0;
    while(FCGI_Accept() >= 0)
        printf("Content-type: text/html\r\n"
               "\r\n"
               "<title>FastCGI Hello!</title>"
               "<h1>FastCGI Hello!</h1>"
               "Request number %d running on host <i>%s</i>\n",
                ++count, getenv("SERVER_NAME"));
}

fastcgi示例 -- 生產(chǎn)者,消費(fèi)者模式(可實(shí)現(xiàn)非阻塞)

非阻塞的實(shí)現(xiàn),將會(huì)話狀態(tài)與requests進(jìn)行binding即可

ZEN_Message_Queue_Deque<ZEN_MT_SYNCH, uint32_t> t_que(100000);

// 這步非常關(guān)鍵,因?yàn)镕CGX_Accept_r中的數(shù)據(jù)流綁定是直接和request的ptr進(jìn)行綁定的
// 所以在未處理完數(shù)據(jù)的前提下,requests的生命周期要和會(huì)話的生命周期一致
FCGX_Request requests[1000];
uint32_t use_idx[1000];

void *do_session(void *arg)
{
    int ret = 0;

    while (1)
    {
        uint32_t idx = 0;
        ret = t_que.try_dequeue(idx);

        if (ret != 0)
        {
            // no data sleep 1ms
            usleep(1000);
            continue;
        }

        FCGX_Request &request = requests[idx];
        std::string out = "Content-type:application/json\r\n\r\n";
        Json::Value root;
        root["ret"] = 1000;
        root["t_id"] = (int)gettid();
        out.append(root.toStyledString());
        FCGX_FPrintF(request.out, out.c_str());
        FCGX_Finish_r(&request);
        use_idx[idx] = 0;
    }

    return NULL;
}

int get_free()
{
    static uint32_t idx = 0;

    for (; idx < 1000; idx ++)
    {
        if (use_idx[idx] == 0)
        {
            use_idx[idx] = 1;
            return idx;
        }
    }

    return -1;
}

int main(int argc, char **argv)
{
    pthread_t pthread_id;
    int iThreadNum = 10;

    for (int index = 0; index != iThreadNum; ++ index)
    {
       pthread_create(&pthread_id, NULL, do_session, NULL);
    }

    memset(use_idx, 0, sizeof(use_idx));
    int ret = FCGX_Init();

    if (ret != 0)
    {
        printf("init fail\n");
        return -1;
    }

    while (1)
    {
        int idx = get_free();

        if (idx < 0)
        {
            continue;
        }

        FCGX_Request &request = requests[idx];
        ret = FCGX_InitRequest(&request, 0, 0);

        if (ret != 0)
        {
            printf("init fail\n");
            return -1;
        }

        ret = FCGX_Accept_r(&request);

        if (ret < 0)
        {
            continue;
        }

        t_que.enqueue(idx);
    }

    return 0;
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容