statsmc.c
int
sp_memcache_write(backend_t *bep, const char *cmd)
{
    int i = strlen(cmd);
    int err = 0;
    int num_chars = 0;
    struct pollfd pfd;
    struct sp_memcache_socket_state session_info;
    struct sp_memcache_socket_state *sk_state = &session_info;

    memset(&session_info, 0, sizeof session_info);
    gettimeofday(&session_info.start_time, NULL);
    session_info.timeout = session_info.time_remaining = bep->settings.write_ms;

    for (; i > 0; i -= num_chars) {
        cmd += num_chars;
        num_chars = write(bep->fd, cmd, strlen(cmd));
        if (num_chars < 0) {
            err = errno;
            if (errno == EWOULDBLOCK && sk_state->time_remaining > 0) {
                num_chars = 0;
                pfd.fd = bep->fd;
                pfd.events = POLLOUT | POLLERR;
                pfd.revents = 0;
                
                /* if poll times out, the socket is still not ready to write.
                 * This is unusual, but is OK.  The next write will return
                 * EWOULDBLOCK, and we will poll some more.
                 */
                if (poll(&pfd, 1, sk_state->timeout) < 0) {
                    err = errno;
                    bail_force_msg("poll failed for %s:%d, %s",
                                   bep->settings.backhost,
                                   bep->settings.backport,
                                   strerror(errno));
                }
            } else {
                if (errno == EWOULDBLOCK) {
                    /* we have reached the timelimit
                     */
                    bail_force_msg("write failed for %s:%d",
                                   bep->settings.backhost,
                                   bep->settings.backport);
                } else {
                    /* socket connection is broken
                     */
                    bail_force_msg("write failed for %s:%d, %s",
                                   bep->settings.backhost,
                                   bep->settings.backport,
                                   strerror(errno));
                }
            }
        }

        sp_memcache_stats_update_time_remaining(sk_state);

        if ((i - num_chars) > 0 && sk_state->time_remaining < 0) {
            /* we have reached the retry timeout
             */
            bail_force_msg("write timed out after %dms", sk_state->timeout);
        }
    }
    return err;

 bail:
    if (bep->fd >= 0) {
        close(bep->fd);
        bep->fd = -1;
    }
    bep->last_error = err;
    return err;
}
// send a command to a memcache server socket
send a command to a memcache server socket
send a command to a memcache server socket
int
sp_memcache_write(backend_t *bep, const char *cmd)
{
    int i = strlen(cmd);
    int err = 0;
    int num_chars = 0;
    struct pollfd pfd;
    struct sp_memcache_socket_state session_info;
    struct sp_memcache_socket_state *sk_state = &session_info;
    memset(&session_info, 0, sizeof session_info);
    gettimeofday(&session_info.start_time, NULL);
    session_info.timeout = session_info.time_remaining = bep->settings.write_ms;
    for (; i > 0; i -= num_chars) {
        cmd += num_chars;
        num_chars = write(bep->fd, cmd, strlen(cmd));
        if (num_chars < 0) {
            err = errno;
            if (errno == EWOULDBLOCK && sk_state->time_remaining > 0) {
                num_chars = 0;
                pfd.fd = bep->fd;
                pfd.events = POLLOUT | POLLERR;
                pfd.revents = 0;
                if (poll(&pfd, 1, sk_state->timeout) < 0) {
                    err = errno;
                    bail_force_msg("poll failed for %s:%d, %s",
                                   bep->settings.backhost,
                                   bep->settings.backport,
                                   strerror(errno));
                }
            } else {
                if (errno == EWOULDBLOCK) {
                    bail_force_msg("write failed for %s:%d",
                                   bep->settings.backhost,
                                   bep->settings.backport);
                } else {
                    bail_force_msg("write failed for %s:%d, %s",
                                   bep->settings.backhost,
                                   bep->settings.backport,
                                   strerror(errno));
                }
            }
        }
        sp_memcache_stats_update_time_remaining(sk_state);
        if ((i - num_chars) > 0 && sk_state->time_remaining < 0) {
            bail_force_msg("write timed out after %dms", sk_state->timeout);
        }
    }
    return err;
 bail:
    if (bep->fd >= 0) {
        close(bep->fd);
        bep->fd = -1;
    }
    bep->last_error = err;
    return err;
}
int sp_memcache_write ( backend_t * bep , const char * cmd ) { int i = strlen ( cmd ) ; int err = 0 ; int num_chars = 0 ; struct pollfd pfd ; struct sp_memcache_socket_state session_info ; struct sp_memcache_socket_state * sk_state = & session_info ; memset ( & session_info , 0 , sizeof session_info ) ; gettimeofday ( & session_info . start_time , NULL ) ; session_info . timeout = session_info . time_remaining = bep -> settings . write_ms ; for ( ; i > 0 ; i -= num_chars ) { cmd += num_chars ; num_chars = write ( bep -> fd , cmd , strlen ( cmd ) ) ; if ( num_chars < 0 ) { err = errno ; if ( errno == EWOULDBLOCK && sk_state -> time_remaining > 0 ) { num_chars = 0 ; pfd . fd = bep -> fd ; pfd . events = POLLOUT | POLLERR ; pfd . revents = 0 ; if ( poll ( & pfd , 1 , sk_state -> timeout ) < 0 ) { err = errno ; bail_force_msg ( " " , bep -> settings . backhost , bep -> settings . backport , strerror ( errno ) ) ; } } else { if ( errno == EWOULDBLOCK ) { bail_force_msg ( " " , bep -> settings . backhost , bep -> settings . backport ) ; } else { bail_force_msg ( " " , bep -> settings . backhost , bep -> settings . backport , strerror ( errno ) ) ; } } } sp_memcache_stats_update_time_remaining ( sk_state ) ; if ( ( i - num_chars ) > 0 && sk_state -> time_remaining < 0 ) { bail_force_msg ( " " , sk_state -> timeout ) ; } } return err ; bail : if ( bep -> fd >= 0 ) { close ( bep -> fd ) ; bep -> fd = -1 ; } bep -> last_error = err ; return err ; }
send a command to a memcache server socket
send a command to a memcache server socket
/* if poll times out, the socket is still not ready to write.
                 * This is unusual, but is OK.  The next write will return
                 * EWOULDBLOCK, and we will poll some more.
                 */ /* we have reached the timelimit
                     */ /* socket connection is broken
                     */ /* we have reached the retry timeout
             */