From 5cece84ca93626a6e8e7d1b10704d9e9f6b720f7 Mon Sep 17 00:00:00 2001 From: fengzero Date: Tue, 19 Jul 2022 02:46:27 +0000 Subject: [PATCH] fix(http): aio timeout --- package-sdk.sh | 1 + plugins/modbus/modbus_req.c | 2 +- plugins/restful/handle.h | 2 ++ plugins/restful/http.c | 17 +++++++++++++++++ plugins/restful/http.h | 1 + src/adapter/adapter.c | 2 ++ src/core/manager.c | 2 ++ 7 files changed, 26 insertions(+), 1 deletion(-) diff --git a/package-sdk.sh b/package-sdk.sh index d35fce05..44a0a0c8 100755 --- a/package-sdk.sh +++ b/package-sdk.sh @@ -44,6 +44,7 @@ cp build/libneuron-base.so ${package_name}/lib cp zlog/src/libzlog.so.1.2 ${package_name}/lib cp zlog.conf ${package_name}/config/ +cp dev.conf ${package_name}/config/ cp neuron.key ${package_name}/config/ cp neuron.pem ${package_name}/config/ diff --git a/plugins/modbus/modbus_req.c b/plugins/modbus/modbus_req.c index 35df4faf..14f99230 100644 --- a/plugins/modbus/modbus_req.c +++ b/plugins/modbus/modbus_req.c @@ -356,7 +356,7 @@ int modbus_write(neu_plugin_t *plugin, void *req, neu_datatag_t *tag, process_protocol_buf(plugin, response_size); } else { plugin->common.adapter_callbacks->driver.write_response( - plugin->common.adapter, req, NEU_ERR_PLUGIN_WRITE_FAILURE); + plugin->common.adapter, req, NEU_ERR_PLUGIN_DISCONNECTED); } return 0; diff --git a/plugins/restful/handle.h b/plugins/restful/handle.h index f4ee5b7c..c03e321c 100644 --- a/plugins/restful/handle.h +++ b/plugins/restful/handle.h @@ -32,6 +32,8 @@ size_t req_data_size = 0; \ req_type *req = NULL; \ \ + nng_aio_set_timeout(aio, 3000); \ + nng_aio_defer(aio, http_timeout, NULL); \ if (http_get_body((aio), (void **) &req_data, &req_data_size) == 0 && \ decode_fun(req_data, &req) == 0) { \ { func }; \ diff --git a/plugins/restful/http.c b/plugins/restful/http.c index e38196ff..0c663533 100644 --- a/plugins/restful/http.c +++ b/plugins/restful/http.c @@ -395,3 +395,20 @@ int http_internal_error(nng_aio *aio, char *content) { return response(aio, content, NNG_HTTP_STATUS_INTERNAL_SERVER_ERROR); } + +void http_timeout(nng_aio *aio, void *arg, int err) +{ + (void) arg; + + switch (err) { + case NNG_ETIMEDOUT: + http_internal_error(aio, "{\"error\": \"timeout\"}"); + break; + case NNG_ECANCELED: + http_internal_error(aio, "{\"error\": \"canceled\"}"); + break; + default: + http_internal_error(aio, "{\"error\": \"unknown\"}"); + break; + } +} \ No newline at end of file diff --git a/plugins/restful/http.h b/plugins/restful/http.h index 862cf98f..59882f26 100644 --- a/plugins/restful/http.h +++ b/plugins/restful/http.h @@ -80,6 +80,7 @@ int http_unauthorized(nng_aio *aio, char *content); int http_not_found(nng_aio *aio, char *content); int http_conflict(nng_aio *aio, char *content); int http_internal_error(nng_aio *aio, char *content); +void http_timeout(nng_aio *aio, void *arg, int err); #ifdef __cplusplus } diff --git a/src/adapter/adapter.c b/src/adapter/adapter.c index de0078b6..1122a91d 100644 --- a/src/adapter/adapter.c +++ b/src/adapter/adapter.c @@ -85,6 +85,8 @@ neu_adapter_t *neu_adapter_create(neu_adapter_info_t *info, bool start) rv = nng_pair1_open(&adapter->sock); assert(rv == 0); + nng_socket_set_int(adapter->sock, NNG_OPT_RECVBUF, 128); + nng_socket_set_int(adapter->sock, NNG_OPT_SENDBUF, 128); nng_socket_set_ms(adapter->sock, NNG_OPT_SENDTIMEO, 1000); nng_socket_get_int(adapter->sock, NNG_OPT_RECVFD, &adapter->recv_fd); diff --git a/src/core/manager.c b/src/core/manager.c index 62835ea7..b345f106 100644 --- a/src/core/manager.c +++ b/src/core/manager.c @@ -75,6 +75,8 @@ neu_manager_t *neu_manager_create() rv = nng_listen(manager->socket, url, NULL, 0); assert(rv == 0); + nng_socket_set_int(manager->socket, NNG_OPT_RECVBUF, 2048); + nng_socket_set_int(manager->socket, NNG_OPT_SENDBUF, 2048); nng_socket_get_int(manager->socket, NNG_OPT_RECVFD, ¶m.fd); manager->loop = neu_event_add_io(manager->events, param);