CGI-SpeedyCGI

 view release on metacpan or  search on metacpan

src/speedy_frontend.c  view on Meta::CPAN

    /* Check the frontend previous to us.  This may remove it */
    frontend_check_prev(gslotnum, fslotnum);

    /* If we're not the head of the list, then all done */
    if (speedy_slot_prev(fslotnum))
	return 1;

    /* Do a check of backends.  Returns false if we cannot start be */
    return backend_check(gslotnum, did_spawn);
}


/* Get a backend the hard-way - by queueing up
*/
static int get_a_backend_hard
    (slotnum_t gslotnum, slotnum_t fslotnum, slotnum_t *bslotnum)
{
    int file_changed, did_spawn = 0, spawn_working = 1, sent_sig;
    *bslotnum = 0;

    /* Install sig handlers */
    sig_handler_setup();

    /* Put ourself at the end of the fe queue */
    speedy_slot_append(fslotnum,
	&(FILE_SLOT(gr_slot, gslotnum).fe_head),
	&(FILE_SLOT(gr_slot, gslotnum).fe_tail));

    while (1) {
	/* Send signals to frontends */
	speedy_group_sendsigs(gslotnum);

	sent_sig = FILE_SLOT(fe_slot, fslotnum).sent_sig;
	FILE_SLOT(fe_slot, fslotnum).sent_sig = 0;

	/* If our sent_sig flag is set, and there are be's for us to use ,
	 * then all done.
	*/
	if (sent_sig &&
	    (*bslotnum = speedy_backend_be_wait_get(gslotnum)))
	{
	    break;
	}

	/* Check on frontends/backends running */
	spawn_working = frontend_ping(gslotnum, fslotnum, &did_spawn);

	/* Frontend ping may have invalidated our group */
	if (!spawn_working || !speedy_group_isvalid(gslotnum))
	    break;

	/* Unlock the file */
	speedy_file_set_state(FS_HAVESLOTS);

	/* Set an alarm for one-second or so. */
	alarm(OPTVAL_BECHECKTIMEOUT);

	/* Wait for a timeout or signal from backend */
	speedy_sig_wait(&sl);

	/* Find out if our file changed.  Do this while unlocked */
	file_changed = speedy_script_changed();

	/* Acquire lock.  If group bad or file changed, then done */
	if (!speedy_group_lock(gslotnum) || file_changed)
	    break;
    }

    /* Remove our FE slot from the queue.  */
    speedy_slot_remove(fslotnum,
	&(FILE_SLOT(gr_slot, gslotnum).fe_head),
	&(FILE_SLOT(gr_slot, gslotnum).fe_tail));

    /* Put sighandlers back to their original state */
    sig_handler_teardown(1);

    return spawn_working;
}

static int get_a_backend(slotnum_t fslotnum, slotnum_t *gslotnum) {
    slotnum_t bslotnum = 0;
    int spawn_working = 1;

    /* Locate the group for our script */
    *gslotnum = speedy_script_find();

    /* Try to quickly grab a backend without queueing */
    if (!FILE_SLOT(gr_slot, *gslotnum).fe_head)
	bslotnum = speedy_backend_be_wait_get(*gslotnum);

    /* If that failed, use the queue */
    if (!bslotnum)
	spawn_working = get_a_backend_hard(*gslotnum, fslotnum, &bslotnum);
    
    /* Clean up the group if necessary */
    speedy_group_cleanup(*gslotnum);

    FILE_SLOT(fe_slot, fslotnum).backend = bslotnum;
    return spawn_working;
}


int speedy_frontend_connect(int socks[NUMFDS], slotnum_t *fslotnum_p) {
    static int did_clean;
    int connected = 0, spawn_working = 1, sockets_open = 0;

    /* May need options from the #! line in the script.  This also
     * opens the script file
     */
    speedy_opt_read_shbang();

    while (spawn_working && !connected) {
	slotnum_t gslotnum, bslotnum, fslotnum;

	/* Create sockets in preparation for connect.  This may take a while,
	 * esp on FreeBSD, when it's out of sockets.
	 */
	if (!sockets_open++)
	    speedy_ipc_connect_prepare(socks);

	/* Lock temp file */



( run in 0.974 second using v1.01-cache-2.11-cpan-800906f7e73 )