Changeset 249:9afe2fc1bab1 for uwsgi.c

Show
Ignore:
Timestamp:
03/04/10 16:48:33 (5 months ago)
Author:
roberto@…
Branch:
default
Message:

the uWSGI proxy

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • uwsgi.c

    r248 r249  
    462462int main (int argc, char *argv[], char *envp[]) { 
    463463 
     464        uint64_t master_cycles = 0 ; 
    464465        struct timeval check_interval = {.tv_sec = 1,.tv_usec = 0 }; 
    465466 
     
    520521        int socket_type = 0; 
    521522        socklen_t socket_type_len; 
    522  
    523         fprintf(stderr,"ARGV0 = %s %s\n", argv[0], argv[1]); 
    524523 
    525524        /* anti signal bombing */ 
     
    605604                {"nagios", no_argument, &nagios, 1}, 
    606605                {"binary-path", required_argument, 0, LONG_ARGS_BINARY_PATH}, 
     606                {"proxy", required_argument, 0, LONG_ARGS_PROXY}, 
     607                {"proxy-node", required_argument, 0, LONG_ARGS_PROXY_NODE}, 
     608                {"proxy-max-connections", required_argument, 0, LONG_ARGS_PROXY_MAX_CONNECTIONS}, 
    607609                {0, 0, 0, 0} 
    608610        }; 
     
    10691071                socket_type_len = sizeof (int); 
    10701072                if (getsockopt (uwsgi.serverfd, SOL_SOCKET, SO_TYPE, &socket_type, &socket_type_len)) { 
    1071                         perror ("getsockopt()"); 
    1072                         fprintf(stderr, "The -s/--socket option is missing and stdin is not a socket.\n"); 
    1073                         exit (1); 
    1074                 } 
    1075  
    1076         } 
     1073                        //perror ("getsockopt()"); 
     1074                        uwsgi.numproc = 0 ; 
     1075                } 
     1076 
     1077#ifdef UWSGI_PROXY 
     1078                if (uwsgi.proxy_socket_name) { 
     1079                        uwsgi.shared->proxy_pid = proxy_start(uwsgi.master_process); 
     1080                } 
     1081#endif 
     1082 
     1083 
     1084        } 
     1085 
     1086#ifdef UWSGI_PROXY 
     1087        if (uwsgi.numproc == 0 && (!uwsgi.proxy_socket_name || uwsgi.shared->proxy_pid <= 0)) { 
     1088#else 
     1089        if (uwsgi.numproc == 0) { 
     1090#endif 
     1091                fprintf(stderr, "The -s/--socket option is missing and stdin is not a socket.\n"); 
     1092                exit (1); 
     1093        } 
     1094 
    10771095 
    10781096 
     
    11851203#endif 
    11861204 
     1205// is this a proxy only worker ? 
     1206 
     1207        if (!uwsgi.master_process && uwsgi.numproc == 0) { 
     1208                exit(0); 
     1209        } 
     1210 
    11871211        if (!uwsgi.single_interpreter) { 
    11881212                fprintf(stderr,"*** uWSGI is running in multiple interpreter mode !!! ***\n"); 
     
    12151239 
    12161240#ifdef UWSGI_SPOOLER 
    1217         if (spool_dir != NULL) { 
     1241        if (spool_dir != NULL && uwsgi.numproc > 0) { 
    12181242                uwsgi.shared->spooler_pid = spooler_start (uwsgi.serverfd, uwsgi_module); 
    12191243        } 
     
    13021326 
    13031327#endif 
     1328 
     1329#ifdef UWSGI_PROXY 
     1330                                if (uwsgi.proxy_socket_name && uwsgi.shared->proxy_pid > 0) { 
     1331                                        kill (uwsgi.shared->proxy_pid, SIGKILL); 
     1332                                        fprintf(stderr,"killed proxy with pid %d\n", uwsgi.shared->proxy_pid); 
     1333                                } 
     1334#endif 
    13041335                                fprintf (stderr, "goodbye to uWSGI.\n"); 
    13051336                                exit (0); 
     
    13111342                                        fprintf(stderr,"wait4() the spooler with pid %d...", uwsgi.shared->spooler_pid); 
    13121343                                        diedpid = waitpid(uwsgi.shared->spooler_pid, &waitpid_status, 0); 
     1344                                        fprintf(stderr,"done."); 
     1345                                } 
     1346#endif 
     1347 
     1348#ifdef UWSGI_PROXY 
     1349                                if (uwsgi.proxy_socket_name && uwsgi.shared->proxy_pid > 0) { 
     1350                                        kill (uwsgi.shared->proxy_pid, SIGKILL); 
     1351                                        fprintf(stderr,"wait4() the proxy with pid %d...", uwsgi.shared->proxy_pid); 
     1352                                        diedpid = waitpid(uwsgi.shared->proxy_pid, &waitpid_status, 0); 
    13131353                                        fprintf(stderr,"done."); 
    13141354                                } 
     
    13811421                                                        if (inet_ntop (AF_INET, &udp_client.sin_addr.s_addr, udp_client_addr, 16)) { 
    13821422                                                                fprintf (stderr, "received udp packet of %d bytes from %s:%d\n", rlen, udp_client_addr, ntohs (udp_client.sin_port)); 
     1423#ifdef UWSGI_SNMP 
    13831424                                                                if (uwsgi.buffer[0] == 0x30) { 
    13841425                                                                        manage_snmp (uwsgi_poll.fd, (uint8_t *) uwsgi.buffer, rlen, &udp_client); 
    13851426                                                                } 
     1427#endif 
    13861428                                                        } 
    13871429                                                        else { 
     
    13941436                                        select (0, NULL, NULL, NULL, &check_interval); 
    13951437                                } 
     1438                                master_cycles++; 
    13961439                                working_workers = 0; 
    13971440                                blocking_workers = 0; 
     
    14251468                                } 
    14261469 
     1470                                // check for cluster nodes 
     1471                                for(i=0;i<MAX_CLUSTER_NODES;i++) { 
     1472                                        struct uwsgi_cluster_node *ucn = &uwsgi.shared->nodes[i]; 
     1473                                         
     1474                                        if (ucn->name[0] != 0 && ucn->status == UWSGI_NODE_FAILED) { 
     1475                                                // should i retry ? 
     1476                                                if (master_cycles % ucn->errors == 0) { 
     1477                                                        if (!uwsgi_ping_node(i, &wsgi_req)) { 
     1478                                                                ucn->status = UWSGI_NODE_OK ; 
     1479                                                                fprintf(stderr,"re-enabled cluster node %d/%s\n", i, ucn->name); 
     1480                                                        } 
     1481                                                        else { 
     1482                                                                ucn->errors++; 
     1483                                                        } 
     1484                                                } 
     1485                                        } 
     1486                                } 
     1487 
    14271488                                continue; 
    14281489 
     
    14351496                                        uwsgi.shared->spooler_pid = spooler_start (uwsgi.serverfd, uwsgi_module); 
    14361497                                        continue; 
     1498                                } 
     1499                        } 
     1500#endif 
     1501 
     1502#ifdef UWSGI_SPOOLER 
     1503                        /* reload the proxy (can be the only process running) */ 
     1504                        if (uwsgi.proxy_socket_name && uwsgi.shared->proxy_pid > 0) { 
     1505                                if (diedpid == uwsgi.shared->proxy_pid) { 
     1506                                        if (WIFEXITED (waitpid_status)) { 
     1507                                                if (WEXITSTATUS (waitpid_status) != UWSGI_END_CODE) {    
     1508                                                        fprintf(stderr,"OOOPS the proxy is no more...trying respawn...\n"); 
     1509                                                        uwsgi.shared->spooler_pid = proxy_start(1); 
     1510                                                        continue; 
     1511                                                } 
     1512                                        } 
    14371513                                } 
    14381514                        } 
     
    14831559                                if (uwsgi.mywid <= 0) { 
    14841560#endif 
    1485                                         fprintf (stderr, "warning the died pid was not in the workers list. Probably you hit a BUG of uWSGI\n"); 
     1561 
     1562#ifdef UWSGI_PROXY 
     1563                                        if (diedpid != uwsgi.shared->proxy_pid) { 
     1564#endif 
     1565                                                fprintf (stderr, "warning the died pid was not in the workers list. Probably you hit a BUG of uWSGI\n"); 
     1566#ifdef UWSGI_PROXY 
     1567                                        } 
     1568#endif 
    14861569                                } 
    14871570                        } 
     
    22872370#endif 
    22882371 
     2372#ifdef UWSGI_PROXY 
     2373pid_t proxy_start (has_master) { 
     2374         
     2375        pid_t pid ; 
     2376 
     2377        char *tcp_port = strchr (uwsgi.proxy_socket_name, ':'); 
     2378 
     2379        if (tcp_port == NULL) { 
     2380                uwsgi.proxyfd = bind_to_unix (uwsgi.proxy_socket_name, UWSGI_LISTEN_QUEUE, uwsgi.chmod_socket, uwsgi.abstract_socket); 
     2381        } 
     2382        else { 
     2383                uwsgi.proxyfd = bind_to_tcp (uwsgi.proxy_socket_name, UWSGI_LISTEN_QUEUE, tcp_port); 
     2384                tcp_port[0] = ':'; 
     2385        } 
     2386 
     2387        if (uwsgi.proxyfd < 0) { 
     2388                fprintf (stderr, "unable to create the server socket.\n"); 
     2389                exit (1); 
     2390        } 
     2391 
     2392        if (!has_master && uwsgi.numproc == 0) { 
     2393                uwsgi_proxy(uwsgi.proxyfd); 
     2394                // never here 
     2395                exit(1); 
     2396        } 
     2397        else { 
     2398                pid = fork() ; 
     2399                if (pid < 0) { 
     2400                        perror("fork()"); 
     2401                        exit(1); 
     2402                } 
     2403                else if (pid > 0) { 
     2404                        close(uwsgi.proxyfd); 
     2405                        return pid ; 
     2406                        // continue with uWSGI spawn... 
     2407                } 
     2408                else { 
     2409                        uwsgi_proxy(uwsgi.proxyfd); 
     2410                        // never here 
     2411                        exit(1); 
     2412                } 
     2413        } 
     2414} 
     2415#endif 
     2416 
    22892417#ifdef UWSGI_SPOOLER 
    22902418pid_t spooler_start (int serverfd, PyObject * uwsgi_module) { 
     
    23322460                        uwsgi.binary_path = optarg; 
    23332461                        break; 
     2462#ifdef UWSGI_PROXY 
     2463                case LONG_ARGS_PROXY_NODE: 
     2464                        uwsgi_cluster_add_node(optarg, 1); 
     2465                        break; 
     2466                case LONG_ARGS_PROXY: 
     2467                        uwsgi.proxy_socket_name = optarg; 
     2468                        break; 
     2469#endif 
    23342470#ifdef UWSGI_ERLANG 
    23352471                case LONG_ARGS_ERLANG: 
     
    25442680        } 
    25452681 
     2682 
     2683void uwsgi_cluster_add_node(char *nodename, int workers) { 
     2684 
     2685        int i ; 
     2686        struct uwsgi_cluster_node *ucn ; 
     2687        char *tcp_port ; 
     2688 
     2689        if (strlen(nodename) > 100) { 
     2690                fprintf(stderr,"invalid cluster node name %s\n", nodename); 
     2691                return; 
     2692        } 
     2693 
     2694        tcp_port = strchr (nodename, ':'); 
     2695        if (tcp_port == NULL) { 
     2696                fprintf(stdout,"invalid cluster node name %s\n", nodename); 
     2697                return ; 
     2698        } 
     2699 
     2700        for(i=0;i<MAX_CLUSTER_NODES; i++) { 
     2701                ucn = &uwsgi.shared->nodes[i] ; 
     2702 
     2703                if (ucn->name[0] == 0) { 
     2704                        strcpy(ucn->name, nodename); 
     2705                        ucn->workers = workers ; 
     2706                        ucn->ucn_addr.sin_family = AF_INET ; 
     2707                        ucn->ucn_addr.sin_port = htons(atoi(tcp_port+1)); 
     2708                        tcp_port[0] = 0 ; 
     2709                        if (nodename[0] == 0) { 
     2710                                ucn->ucn_addr.sin_addr.s_addr = INADDR_ANY ; 
     2711                        } 
     2712                        else { 
     2713                                fprintf(stderr,"%s\n", nodename); 
     2714                                ucn->ucn_addr.sin_addr.s_addr = inet_addr(nodename); 
     2715                        } 
     2716 
     2717                        ucn->last_seen = time(NULL); 
     2718 
     2719                        return ; 
     2720                } 
     2721        } 
     2722         
     2723        fprintf(stderr,"unable to add node %s\n", nodename); 
     2724}