From 690d592a944dc24f8f0fd19a2381c8e51976be3d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20M=C3=A4kel=C3=A4?= Date: Sat, 22 Apr 2017 08:47:42 +0300 Subject: [PATCH] MXS-1220: Implement /sessions/ resource The /sessions/ resource was not implemented due to changes in the core polling mechanics. With the new worker thread messaging system, sessions can be listed in a safe manner. --- include/maxscale/session.h | 9 +++++++++ server/core/resource.cc | 3 +-- server/core/session.cc | 20 ++++++++++++++++++++ 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/include/maxscale/session.h b/include/maxscale/session.h index 5aaba6e1b..e8746d8fe 100644 --- a/include/maxscale/session.h +++ b/include/maxscale/session.h @@ -423,4 +423,13 @@ void session_clear_stmt(MXS_SESSION *session); */ json_t* session_to_json(const MXS_SESSION *session, const char* host); +/** + * @brief Convert all sessions to JSON + * + * @param host Hostname of this server + * + * @return A JSON array with all sessions + */ +json_t* session_list_to_json(const char* host); + MXS_END_DECLS diff --git a/server/core/resource.cc b/server/core/resource.cc index cff0dbe6a..b02009ff2 100644 --- a/server/core/resource.cc +++ b/server/core/resource.cc @@ -228,8 +228,7 @@ HttpResponse cb_get_monitor(const HttpRequest& request) HttpResponse cb_all_sessions(const HttpRequest& request) { - // TODO: Implement this - return HttpResponse(MHD_HTTP_OK); + return HttpResponse(MHD_HTTP_OK, session_list_to_json(request.host())); } HttpResponse cb_get_session(const HttpRequest& request) diff --git a/server/core/session.cc b/server/core/session.cc index 18b42d014..5793ce78a 100644 --- a/server/core/session.cc +++ b/server/core/session.cc @@ -1059,3 +1059,23 @@ json_t* session_to_json(const MXS_SESSION *session, const char *host) return rval; } + +struct SessionListData +{ + json_t* json; + const char* host; +}; + +bool seslist_cb(DCB* dcb, void* data) +{ + SessionListData* d = (SessionListData*)data; + json_array_append_new(d->json, session_to_json(dcb->session, d->host)); + return true; +} + +json_t* session_list_to_json(const char* host) +{ + SessionListData data = {json_array(), host}; + dcb_foreach(seslist_cb, &data); + return data.json; +}