diff --git a/include/maxscale/session.h b/include/maxscale/session.h index aaa071ace..f12d75d1c 100644 --- a/include/maxscale/session.h +++ b/include/maxscale/session.h @@ -441,4 +441,21 @@ json_t* session_to_json(const MXS_SESSION *session, const char* host); */ json_t* session_list_to_json(const char* host); +/** + * @brief Return the session of the dcb currently being processed + * by the calling thread. + * + * @return A session, or NULL if the calling thread is not currently handling + * a dcb or if the calling thread is not a polling/worker thread. + **/ +MXS_SESSION* session_get_current(); + +/** + * @brief Return the id of the session of the dcb currently being processed + * by the calling thread. + * + * @return The id of the current session or 0 if there is no current session. + **/ +uint64_t session_get_current_id(); + MXS_END_DECLS diff --git a/server/core/session.cc b/server/core/session.cc index c1355c2ae..972f04f0e 100644 --- a/server/core/session.cc +++ b/server/core/session.cc @@ -1177,3 +1177,17 @@ json_t* session_list_to_json(const char* host) dcb_foreach(seslist_cb, &data); return mxs_json_resource(host, MXS_JSON_API_SESSIONS, data.json); } + +MXS_SESSION* session_get_current() +{ + DCB* dcb = dcb_get_current(); + + return dcb ? dcb->session : NULL; +} + +uint64_t session_get_current_id() +{ + MXS_SESSION* session = session_get_current(); + + return session ? session->ses_id : 0; +}