FS-9810 fix ws write fail on slow network

This commit is contained in:
Seven Du 2016-12-04 12:49:46 +08:00
parent 89063a1a4c
commit b26fa6e17c
4 changed files with 84 additions and 21 deletions

View File

@ -42,6 +42,9 @@
#define WS_BLOCK 1 #define WS_BLOCK 1
#define WS_NOBLOCK 0 #define WS_NOBLOCK 0
#define WS_INIT_SANITY 5000
#define WS_WRITE_SANITY 2000
#define SHA1_HASH_SIZE 20 #define SHA1_HASH_SIZE 20
static const char c64[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; static const char c64[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@ -412,7 +415,7 @@ KS_DECLARE(ks_ssize_t) kws_raw_read(kws_t *kws, void *data, ks_size_t bytes, int
KS_DECLARE(ks_ssize_t) kws_raw_write(kws_t *kws, void *data, ks_size_t bytes) KS_DECLARE(ks_ssize_t) kws_raw_write(kws_t *kws, void *data, ks_size_t bytes)
{ {
ks_ssize_t r; ks_ssize_t r;
int sanity = 2000; int sanity = WS_WRITE_SANITY;
int ssl_err = 0; int ssl_err = 0;
ks_size_t wrote = 0; ks_size_t wrote = 0;
@ -424,8 +427,17 @@ KS_DECLARE(ks_ssize_t) kws_raw_write(kws_t *kws, void *data, ks_size_t bytes)
wrote += r; wrote += r;
} }
if (sanity < 2000) { if (sanity < WS_WRITE_SANITY) {
ks_sleep_ms(1); int ms = 1;
if (kws->block) {
if (sanity < WS_WRITE_SANITY * 3 / 4) {
ms = 60;
} else if (sanity < WS_WRITE_SANITY / 2) {
ms = 10;
}
}
ks_sleep_ms(ms);
} }
if (r == -1) { if (r == -1) {
@ -448,8 +460,17 @@ KS_DECLARE(ks_ssize_t) kws_raw_write(kws_t *kws, void *data, ks_size_t bytes)
wrote += r; wrote += r;
} }
if (sanity < 2000) { if (sanity < WS_WRITE_SANITY) {
ks_sleep_ms(1); int ms = 1;
if (kws->block) {
if (sanity < WS_WRITE_SANITY * 3 / 4) {
ms = 60;
} else if (sanity < WS_WRITE_SANITY / 2) {
ms = 10;
}
}
ks_sleep_ms(ms);
} }
} while (--sanity > 0 && ((r == -1 && ks_errno_is_blocking(ks_errno())) || (kws->block && wrote < bytes))); } while (--sanity > 0 && ((r == -1 && ks_errno_is_blocking(ks_errno())) || (kws->block && wrote < bytes)));
@ -674,7 +695,7 @@ KS_DECLARE(ks_status_t) kws_init(kws_t **kwsP, ks_socket_t sock, SSL_CTX *ssl_ct
} }
kws->sock = sock; kws->sock = sock;
kws->sanity = 5000; kws->sanity = WS_INIT_SANITY;
kws->ssl_ctx = ssl_ctx; kws->ssl_ctx = ssl_ctx;
kws->buflen = 1024 * 64; kws->buflen = 1024 * 64;

View File

@ -1 +1 @@
Mon Oct 10 15:54:37 CDT 2016 Sun Dec 4 12:46:08 CST 2016

View File

@ -20,6 +20,9 @@
#define WS_BLOCK 1 #define WS_BLOCK 1
#define WS_NOBLOCK 0 #define WS_NOBLOCK 0
#define WS_INIT_SANITY 5000
#define WS_WRITE_SANITY 2000
#define SHA1_HASH_SIZE 20 #define SHA1_HASH_SIZE 20
struct ws_globals_s ws_globals; struct ws_globals_s ws_globals;
@ -407,7 +410,7 @@ ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes, int block)
ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes) ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes)
{ {
ssize_t r; ssize_t r;
int sanity = 2000; int sanity = WS_WRITE_SANITY;
int ssl_err = 0; int ssl_err = 0;
size_t wrote = 0; size_t wrote = 0;
@ -419,8 +422,17 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes)
wrote += r; wrote += r;
} }
if (sanity < 2000) { if (sanity < WS_WRITE_SANITY) {
ms_sleep(1); int ms = 1;
if (wsh->block) {
if (sanity < WS_WRITE_SANITY * 3 / 4) {
ms = 60;
} else if (sanity < WS_WRITE_SANITY / 2) {
ms = 10;
}
}
ms_sleep(ms);
} }
if (r == -1) { if (r == -1) {
@ -443,8 +455,17 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes)
wrote += r; wrote += r;
} }
if (sanity < 2000) { if (sanity < WS_WRITE_SANITY) {
ms_sleep(1); int ms = 1;
if (wsh->block) {
if (sanity < WS_WRITE_SANITY * 3 / 4) {
ms = 60;
} else if (sanity < WS_WRITE_SANITY / 2) {
ms = 10;
}
}
ms_sleep(ms);
} }
} while (--sanity > 0 && ((r == -1 && xp_is_blocking(xp_errno())) || (wsh->block && wrote < bytes))); } while (--sanity > 0 && ((r == -1 && xp_is_blocking(xp_errno())) || (wsh->block && wrote < bytes)));
@ -453,7 +474,7 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes)
//printf("wRITE FAIL: %s\n", strerror(errno)); //printf("wRITE FAIL: %s\n", strerror(errno));
//} //}
return r; return r < 0 ? r : wrote;
} }
#ifdef _MSC_VER #ifdef _MSC_VER
@ -587,7 +608,7 @@ int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock, int
wsh->sock = sock; wsh->sock = sock;
wsh->block = block; wsh->block = block;
wsh->sanity = 5000; wsh->sanity = WS_INIT_SANITY;
wsh->ssl_ctx = ssl_ctx; wsh->ssl_ctx = ssl_ctx;
wsh->stay_open = stay_open; wsh->stay_open = stay_open;

View File

@ -20,6 +20,9 @@
#define WS_BLOCK 1 #define WS_BLOCK 1
#define WS_NOBLOCK 0 #define WS_NOBLOCK 0
#define WS_INIT_SANITY 5000
#define WS_WRITE_SANITY 2000
#define SHA1_HASH_SIZE 20 #define SHA1_HASH_SIZE 20
struct ws_globals_s ws_globals; struct ws_globals_s ws_globals;
@ -407,7 +410,7 @@ ssize_t ws_raw_read(wsh_t *wsh, void *data, size_t bytes, int block)
ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes) ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes)
{ {
ssize_t r; ssize_t r;
int sanity = 2000; int sanity = WS_WRITE_SANITY;
int ssl_err = 0; int ssl_err = 0;
size_t wrote = 0; size_t wrote = 0;
@ -419,8 +422,17 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes)
wrote += r; wrote += r;
} }
if (sanity < 2000) { if (sanity < WS_WRITE_SANITY) {
ms_sleep(1); int ms = 1;
if (wsh->block) {
if (sanity < WS_WRITE_SANITY * 3 / 4) {
ms = 60;
} else if (sanity < WS_WRITE_SANITY / 2) {
ms = 10;
}
}
ms_sleep(ms);
} }
if (r == -1) { if (r == -1) {
@ -443,8 +455,17 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes)
wrote += r; wrote += r;
} }
if (sanity < 2000) { if (sanity < WS_WRITE_SANITY) {
ms_sleep(1); int ms = 1;
if (wsh->block) {
if (sanity < WS_WRITE_SANITY * 3 / 4) {
ms = 60;
} else if (sanity < WS_WRITE_SANITY / 2) {
ms = 10;
}
}
ms_sleep(ms);
} }
} while (--sanity > 0 && ((r == -1 && xp_is_blocking(xp_errno())) || (wsh->block && wrote < bytes))); } while (--sanity > 0 && ((r == -1 && xp_is_blocking(xp_errno())) || (wsh->block && wrote < bytes)));
@ -453,7 +474,7 @@ ssize_t ws_raw_write(wsh_t *wsh, void *data, size_t bytes)
//printf("wRITE FAIL: %s\n", strerror(errno)); //printf("wRITE FAIL: %s\n", strerror(errno));
//} //}
return r; return r < 0 ? r : wrote;
} }
#ifdef _MSC_VER #ifdef _MSC_VER
@ -587,7 +608,7 @@ int ws_init(wsh_t *wsh, ws_socket_t sock, SSL_CTX *ssl_ctx, int close_sock, int
wsh->sock = sock; wsh->sock = sock;
wsh->block = block; wsh->block = block;
wsh->sanity = 5000; wsh->sanity = WS_INIT_SANITY;
wsh->ssl_ctx = ssl_ctx; wsh->ssl_ctx = ssl_ctx;
wsh->stay_open = stay_open; wsh->stay_open = stay_open;