Mysterious Delay

Hi,
I have a database/socket program that tends to sit & wait too much. Most of the time, if I send one line over the socket connection, it will handle it, and read 2 lines out of the database. Once in a while, it will process around 50 lines in the database. But then it goes back to 2 lines. I don't understand why it is waiting for receiving something on the socket. I have the program set to start at msg# 8000. There are over 9000 messages. I see no reason it shouldn't be able to process them all without waiting. I am including the main code. If you want to see the entire code in the check out http://www.yungblood.com/gateway.tgz

If you need to know anything about the code, please let me know. I believe this issue is the last major problem in getting my code running.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
int main(int argc, char** argv) {
  int i,j,res, sql;
  int acfd;
  struct sockaddr_in acsa;
  char readbuf[1000],relaystring[1000];
  struct in_addr targetaddr;

  MYSQL_ROW row;
  long int msgid=8000;
  char user[32], msg[2048], dest[32];

  if(logmsg("Starting gateway daemon.")<0) {
    fprintf(stderr, "Failed to start logging. Exiting.\n");
    return 0;
  }

/* Connect to database */
  if((mysql_init(&mysql))==NULL) {
    logmsg("Failed to initate MySQL connection. Exiting.");
    return 0;
  }

  if(!mysql_real_connect(&mysql, MYSQL_HOST, MYSQL_USERNAME, MYSQL_PASSWD, MYSQL_DATABASE, 0, NULL, 0)) {
    logmsg("Failed to connect to MySQL: Error: %s. Exiting.", mysql_error(&mysql));
    return 0;
  }

  close(0);
  close(1);
  signal(SIGPIPE,SIG_IGN);
  strcpy(relaystring,FAILMESSAGE);
  if (-1==(acfd=socket(PF_INET,SOCK_STREAM,0))) {
    logmsg("socket(accept_socket)");
    exit(1);
  }

  acsa.sin_family=AF_INET;
  acsa.sin_port=htons(SERV_PORT);
  acsa.sin_addr.s_addr=INADDR_ANY;
#ifdef SO_REUSEADDR
  {
    int reuseit=1;
    if (-1==setsockopt(acfd,SOL_SOCKET,SO_REUSEADDR,(char*)&reuseit,sizeof(reuseit))) logmsg("setsockopt SOL_SOCKET SO_REUSEADDR");
  }
#endif
  if (-1==bind(acfd,(struct sockaddr*)&acsa,sizeof(struct sockaddr_in))) {
    logmsg("bind");
    exit(1);
  }
  /* 5 is usual the maximum anyway */
  if (-1==listen(acfd,5)) {
    logmsg("listen");
    exit(1);
  }
  while (1) {
    fd_set readfds,writefds;
    int width;

    width=3;
    if (acfd>=width) width=acfd+1;
restart_select:
    FD_ZERO(&readfds);
    FD_ZERO(&writefds);
    FD_SET(acfd,&readfds);

    sql=0;
    sprintf(readbuf, "SELECT * from chat where msgid > '%d' order by time limit 1", msgid);
    if (mysql_query(&mysql, readbuf) == 0) {
      if (result = mysql_store_result(&mysql)) {
        if (mysql_num_rows(result)) {
          row = mysql_fetch_row(result);
          msgid=atol(row[0]);
          strcpy(user,  row[1]);
          strcpy(msg,   row[2]);
          strcpy(dest,  row[4]);
          sp2nb(user);
          sp2nb(dest);
          logmsg("SQL >> %ld %s %s %s\r\n", msgid, user, msg, dest);
          sql = 1;
        }
        mysql_free_result(result);
      }
    }

    for (i=numofclients;i--;) {
      struct client *client = clients+i;

      /* Do we have a message in the database waiting? */
      if (sql) {
        if (strcmp(user, "System") == 0) {
          sendline(client, ":%s!%s@%s PRIVMSG %s :%s\r\n", user, user, USER_DOMAIN, CHAN_NAME, msg);
        } 
        else if (dest[0] == 0)
          sendline(client, ":%s!%s@%s PRIVMSG %s :%s\r\n", user, user, USER_DOMAIN, CHAN_NAME, msg);
        else if (strcmp(dest, client->nick) == 0)
          sendline(client, ":%s!%s@%s PRIVMSG %s :%s\r\n", user, user, USER_DOMAIN, client->nick, msg);
      }

      /* Clean up excess allocated memory */
      while ((client->insize>DEFAULTSIZE) && (client->incur<client->insize/2)) {
        client->inbuf=xrealloc(client->inbuf,client->insize/2);
        client->insize/=2;
      } 
      while ((client->outsize>DEFAULTSIZE) && (client->outcur<client->outsize/2)) {
        client->outbuf=xrealloc(client->outbuf,client->outsize/2);
        client->outsize/=2;
      } 

      /* Closed? -> clean */
      if (client->flags & FLAG_CLOSED) {
      	clean_connection(client);
	continue;
      }
      /* Quit & transmitted all stuff left to user? -> clean */
      if ((client->flags&FLAG_QUIT)&&(!client->outcur)) {
	clean_connection(client);
	continue;
      }

      if (client->sockfd>=0) {
        /*need to do that... else it will cause load 1*/
        if (client->outcur) FD_SET(client->sockfd,&writefds);
	if (!(client->flags & FLAG_EOF)) FD_SET(client->sockfd,&readfds);
        if (client->sockfd>=width) width=client->sockfd+1;
      }
    }
    if (-1==select(width, (FD_CAST*)&readfds, (FD_CAST*)&writefds, NULL,/*no exceptfds.*/ 0)) {
      if (errno!=EINTR) logmsg("select");
      else goto restart_select;
    }
    if (FD_ISSET(acfd,&readfds)) {
      int afd;
      int aclen;
      struct sockaddr_in conaddr;
      struct client *client = NULL;

      aclen=sizeof(struct sockaddr_in);
      if (-1==(afd=accept(acfd,(struct sockaddr*)&conaddr,&aclen))) logmsg("accept");
      if (clients) clients=(struct client*)xrealloc(clients,sizeof(struct client)*(numofclients+1));
      else clients=(struct client*)xmalloc(sizeof(struct client));
      numofclients++;
      if (numofclients > maxclients) maxclients = numofclients;
      client = clients+(numofclients-1);
      client->inbuf	= xmalloc(DEFAULTSIZE);
      client->outbuf	= xmalloc(DEFAULTSIZE);
      client->insize	= DEFAULTSIZE;
      client->outsize	= DEFAULTSIZE;
      client->flags	= 0;
      client->incur	= 0;
      client->outcur	= 0;
      client->login	= 0;
      client->sockfd	= afd;
      client->state	= STATE_ACCEPTED;
      memcpy(&client->addr,&conaddr,sizeof(struct sockaddr_in));
      if (numofclients>=MAXUSERS) {
        strcpy(client->outbuf,relaystring);
	client->outcur = strlen(relaystring)+1;
	client->state = STATE_OK;
	client->flags = FLAG_QUIT;
      }
#ifdef SO_LINGER
      {
	struct linger sol;
	sol.l_linger = 5;
	sol.l_onoff = 1;
	if (-1==setsockopt(acfd,SOL_SOCKET,SO_LINGER,(char*)&sol,sizeof(sol))) logmsg("setsockopt SOL_SOCKET SO_LINGER");
      }
#endif
    }
    for (i=numofclients;i--;) {
      struct client *client = clients+i;

      if ((client->sockfd>=0) && FD_ISSET(client->sockfd,&readfds)) {
        do {
          if (-1==(res=read(client->sockfd,readbuf,1000))) {
            if (errno==EINTR) break;
            /* user side has broken the connection */
	    close(client->sockfd);
	    client->sockfd=-1;
	    client->flags |= FLAG_CLOSED;
            break;
          }
          break;
        } while (1);
        if (res==0) {
	  /* we read the End Of File marker. but we still have to write the rest of the text */
	  client->flags |= FLAG_EOF;
        }
        if (res>0) {
          readbuf[res]='\0';
          while (client->incur+res>=client->insize) {
            client->inbuf=xrealloc(client->inbuf,client->insize*2);
            client->insize*=2;
          }
          memcpy(client->inbuf+client->incur,readbuf,res+1);
          client->incur+=res;
        }
      }
      /* Do we have any lines in the buffer? Process them. */
      while (gotline(client)) getircmsg(client);
      
      if ((client->sockfd>=0) && FD_ISSET(client->sockfd,&writefds)) {
        j=client->outcur;
        if (-1==(res=write(client->sockfd,client->outbuf,j))) {
          if (errno!=EINTR) {
            close(client->sockfd);
            client->sockfd=-1;
	    client->flags |= FLAG_CLOSED;
          }
        }
        if (res>0) {
          memcpy(client->outbuf,client->outbuf+res,client->outcur-res);
          client->outcur-=res;
        }
      }
    }
  }
}
Have you tried profiling the code to see where the bottleneck is actually occuring?

You can also profile the database to ensure it's not a database related issue.
After I posted this question, I did more research into it myself. I did discover something that should help. The place it delays is in the select block of code.
1
2
3
4
    if (-1==select(width, (FD_CAST*)&readfds, (FD_CAST*)&writefds, NULL,/*no exceptfds.*/ 0)) {
      if (errno!=EINTR) logmsg("select");
      else goto restart_select;
    }

But that only makes partial sense to me. I may need to look deeper into the database code so see what actual data is getting pulled. I know that needs more work. I know one way of testing that, which I will be doing shortly.

Setting the possible database issue to the side, since there is no actual delay there. How do I stop the select statement from waiting so long? I really don't want the select to wait more than 1 second. This program is a chatroom gateway. Some messages will come in through the socket, and some will come in through a simple web page and get dropped directly into the database. The way the code is currently working, the person connecting in through the socket will not be able to keep up with a conversation. Especially if there are more web users than socket users.

However, if there are no socket users, I wouldn't care how long the select statement is waiting.
Ok, I found my own answer... :)

I had to change it to:
1
2
  struct timeval  nowait; 
  memset((char *)&nowait,0,sizeof(nowait)); 

...
1
2
3
4
    if (-1==select(width, (FD_CAST*)&readfds, (FD_CAST*)&writefds, NULL, &nowait)) {
      if (errno!=EINTR) logmsg("select");
      else goto restart_select;
    }

Thanks for looking. Maybe this will help someone else... :)
Last edited on
Topic archived. No new replies allowed.