Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various websocket fixes #1736

Merged
merged 2 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/supplemental/http/http_api.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//
// Copyright 2019 Staysail Systems, Inc. <[email protected]>
// Copyright 2023 Staysail Systems, Inc. <[email protected]>
// Copyright 2018 Capitar IT Group BV <[email protected]>
// Copyright 2019 Devolutions <[email protected]>
//
Expand Down Expand Up @@ -216,8 +216,15 @@ extern int nni_http_server_start(nni_http_server *);
// nni_http_server_stop stops the server, closing the listening socket.
// Connections that have been "upgraded" are unaffected. Connections
// associated with a callback will complete their callback, and then close.
// Connections will be aborted but may not have terminated all the way.
extern void nni_http_server_stop(nni_http_server *);

// nni_http_server_close closes down the socket, but does not shut down
// any connections that are already open. This is useful for example
// when shutting down an SP listener, and we don't want to break established
// sessions.
extern void nni_http_server_close(nni_http_server *);

// nni_http_server_set_error_page sets an error page for the named status.
extern int nni_http_server_set_error_page(
nni_http_server *, uint16_t, const char *);
Expand Down
67 changes: 41 additions & 26 deletions src/supplemental/http/http_server.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@
nni_list handlers;
nni_list conns;
nni_mtx mtx;
nni_cv cv;
bool closed;
bool fini; // if nni_http_server_fini was called
nni_aio * accaio;
nng_stream_listener *listener;
int port; // native order
Expand Down Expand Up @@ -301,8 +301,8 @@
if (nni_list_node_active(&sc->node)) {
nni_list_remove(&s->conns, sc);
}
if (nni_list_empty(&s->conns)) {
nni_cv_wake(&s->cv);
if (nni_list_empty(&s->conns) && (s->fini)) {
nni_reap(&http_server_reap_list, s);
}
nni_mtx_unlock(&s->mtx);

Expand Down Expand Up @@ -909,13 +909,7 @@
nni_aio_stop(s->accaio);

nni_mtx_lock(&s->mtx);
if (!nni_list_empty(&s->conns)) {
// Try to reap later, after the connections are done reaping.
// (Note, connections will all have been closed already.)
nni_reap(&http_server_reap_list, s);
nni_mtx_unlock(&s->mtx);
return;
}
NNI_ASSERT(nni_list_empty(&s->conns));
nng_stream_listener_free(s->listener);
while ((h = nni_list_first(&s->handlers)) != NULL) {
nni_list_remove(&s->handlers, h);
Expand All @@ -932,7 +926,6 @@
nni_mtx_fini(&s->errors_mtx);

nni_aio_free(s->accaio);
nni_cv_fini(&s->cv);
nni_mtx_fini(&s->mtx);
nni_strfree(s->hostname);
NNI_FREE_STRUCT(s);
Expand All @@ -958,7 +951,6 @@
}
nni_mtx_init(&s->mtx);
nni_mtx_init(&s->errors_mtx);
nni_cv_init(&s->cv, &s->mtx);
NNI_LIST_INIT(&s->handlers, nni_http_handler, node);
NNI_LIST_INIT(&s->conns, http_sconn, node);

Expand Down Expand Up @@ -1048,10 +1040,8 @@
}

static void
http_server_stop(nni_http_server *s)
http_server_close(nni_http_server *s)
{
http_sconn *sc;

if (s->closed) {
return;
}
Expand All @@ -1063,29 +1053,48 @@
if (s->listener) {
nng_stream_listener_close(s->listener);
}
}

static void
http_server_stop(nni_http_server *s)
{
http_sconn *sc;

http_server_close(s);

// Stopping the server is a hard stop -- it aborts any work
// being done by clients. (No graceful shutdown).
NNI_LIST_FOREACH (&s->conns, sc) {
http_sc_close_locked(sc);
}

while (!nni_list_empty(&s->conns)) {
nni_cv_wait(&s->cv);
}
}

void
nni_http_server_stop(nni_http_server *s)
{
nni_mtx_lock(&s->mtx);
s->starts--;
if (s->starts != 0) {
s->starts--;

Check warning on line 1077 in src/supplemental/http/http_server.c

View check run for this annotation

Codecov / codecov/patch

src/supplemental/http/http_server.c#L1076-L1077

Added lines #L1076 - L1077 were not covered by tests
}
if (s->starts == 0) {
http_server_stop(s);
}
nni_mtx_unlock(&s->mtx);
}

void
nni_http_server_close(nni_http_server *s)
{
nni_mtx_lock(&s->mtx);
if (s->starts != 0) {
s->starts--;
}
if (s->starts == 0) {
http_server_close(s);
}
nni_mtx_unlock(&s->mtx);
}

static int
http_server_set_err(nni_http_server *s, uint16_t code, void *body, size_t len)
{
Expand Down Expand Up @@ -1910,12 +1919,18 @@
{
nni_mtx_lock(&http_servers_lk);
s->refcnt--;
if (s->refcnt == 0) {
nni_mtx_lock(&s->mtx);
http_server_stop(s);
nni_mtx_unlock(&s->mtx);
nni_list_remove(&http_servers, s);
nni_reap(&http_server_reap_list, s);
if (s->refcnt != 0) {
nni_mtx_unlock(&http_servers_lk);
return;
}
nni_list_remove(&http_servers, s);
nni_mtx_unlock(&http_servers_lk);

nni_mtx_lock(&s->mtx);
http_server_stop(s);
s->fini = true;
if (nni_list_empty(&s->conns)) {
nni_reap(&http_server_reap_list, s);
}
nni_mtx_unlock(&s->mtx);
}
Loading
Loading