~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/conn.c

  • Committer: Evan Jones
  • Date: 2011-01-02 18:30:08 UTC
  • mto: (2055.1.2 build)
  • mto: This revision was merged to the branch mainline in revision 2056.
  • Revision ID: evanj@mit.edu-20110102183008-ytvoaig5zxdck3d5
libdrizzle: drizzle_state_read: only call recv() if data is available.

This uses the "read ready" flag that already exists to avoid excess system calls. Without this patch, in non-blocking mode after sending a command, libdrizzle immediately calls recv(), which almost always returns EAGAIN. This patch avoids that, instead returning IO_WAIT. This causes the caller to wait for data on the connection using poll() or epoll() before calling recv().

Show diffs side-by-side

added added

removed removed

Lines of Context:
950
950
    con->buffer_ptr= con->buffer;
951
951
  }
952
952
 
 
953
  if ((con->revents & POLLIN) == 0 &&
 
954
      (con->drizzle->options & DRIZZLE_NON_BLOCKING))
 
955
  {
 
956
    /* non-blocking mode: return IO_WAIT instead of attempting to read. This
 
957
     * avoids reading immediately after writing a command, which typically
 
958
     * returns EAGAIN. This improves performance. */
 
959
    ret= drizzle_con_set_events(con, POLLIN);
 
960
    if (ret != DRIZZLE_RETURN_OK)
 
961
      return ret;
 
962
    return DRIZZLE_RETURN_IO_WAIT;
 
963
  }
 
964
 
953
965
  while (1)
954
966
  {
 
967
    size_t available_buffer= (size_t)DRIZZLE_MAX_BUFFER_SIZE -
 
968
        ((size_t)(con->buffer_ptr - con->buffer) + con->buffer_size);
955
969
    read_size = recv(con->fd, (char *)con->buffer_ptr + con->buffer_size,
956
 
                     (size_t)DRIZZLE_MAX_BUFFER_SIZE -
957
 
                     ((size_t)(con->buffer_ptr - con->buffer) +
958
 
                      con->buffer_size),0); 
 
970
                     available_buffer, 0);
959
971
#ifdef _WIN32
960
972
    /*Get windows error codes and map it to Posix*/
961
973
    errno = WSAGetLastError();
981
993
    {
982
994
      if (errno == EAGAIN)
983
995
      {
 
996
        /* clear the read ready flag */
 
997
        con->revents&= ~POLLIN;
984
998
        ret= drizzle_con_set_events(con, POLLIN);
985
999
        if (ret != DRIZZLE_RETURN_OK)
986
1000
          return ret;
1016
1030
      return DRIZZLE_RETURN_ERRNO;
1017
1031
    }
1018
1032
 
 
1033
    /* clear the "read ready" flag if we read all available data. */
 
1034
    if ((size_t) read_size < available_buffer) con->revents&= ~POLLIN;
1019
1035
    con->buffer_size+= (size_t)read_size;
1020
1036
    break;
1021
1037
  }