MCPcopy Create free account
hub / github.com/Snapchat/KeyDB / syncRead

Function syncRead

src/syncio.cpp:85–118  ·  view source on GitHub ↗

Read the specified amount of bytes from 'fd'. If all the bytes are read * within 'timeout' milliseconds the operation succeed and 'size' is returned. * Otherwise the operation fails, -1 is returned, and an unspecified amount of * data could be read from the file descriptor. */

Source from the content-addressed store, hash-verified

83 * Otherwise the operation fails, -1 is returned, and an unspecified amount of
84 * data could be read from the file descriptor. */
85ssize_t syncRead(int fd, char *ptr, ssize_t size, long long timeout) {
86 ssize_t nread, totread = 0;
87 long long start = mstime();
88 long long remaining = timeout;
89
90 if (size == 0) return 0;
91 while(1) {
92 long long wait = (remaining > SYNCIO__RESOLUTION) ?
93 remaining : SYNCIO__RESOLUTION;
94 long long elapsed;
95
96 /* Optimistically try to read before checking if the file descriptor
97 * is actually readable. At worst we get EAGAIN. */
98 nread = read(fd,ptr,size);
99 if (nread == 0) return -1; /* short read. */
100 if (nread == -1) {
101 if (errno != EAGAIN) return -1;
102 } else {
103 ptr += nread;
104 size -= nread;
105 totread += nread;
106 }
107 if (size == 0) return totread;
108
109 /* Wait */
110 aeWait(fd,AE_READABLE,wait);
111 elapsed = mstime() - start;
112 if (elapsed >= timeout) {
113 errno = ETIMEDOUT;
114 return -1;
115 }
116 remaining = timeout - elapsed;
117 }
118}
119
120/* Read a line making sure that every char will not require more than 'timeout'
121 * milliseconds to be read.

Callers 3

connSocketSyncReadFunction · 0.85
rewriteAppendOnlyFileFunction · 0.85
syncReadLineFunction · 0.85

Calls 2

aeWaitFunction · 0.85
mstimeFunction · 0.70

Tested by

no test coverage detected