-
Notifications
You must be signed in to change notification settings - Fork 955
Offload TLS negotiation to I/O threads #1338
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
Changes from 1 commit
d865119
dc5aa76
c0cf818
7668efb
0abc14c
0256c4b
cea9d4a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static __thread int thread_id = 0; /* Thread local var */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static pthread_t io_threads[IO_THREADS_MAX_NUM] = {0}; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static pthread_mutex_t io_threads_mutex[IO_THREADS_MAX_NUM]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void (*tls_negotiation_cb)(void *); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
uriyage marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* IO jobs queue functions - Used to send jobs from the main-thread to the IO thread. */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| typedef void (*job_handler)(void *); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -554,3 +555,56 @@ void trySendPollJobToIOThreads(void) { | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| aeSetPollProtect(server.el, 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IOJobQueue_push(jq, IOThreadPoll, server.el); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| void setTLSNegotiationCallback(void (*cb)(void *)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tls_negotiation_cb = cb; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| static void ioThreadTLSNegotiation(void *data) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| client *c = (client *)data; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| tls_negotiation_cb(c->conn); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c->io_read_state = CLIENT_COMPLETED_IO; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /* | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * This function attempts to offload TLS negotiation for a client connection to an I/O thread. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Returns C_OK if the TLS negotiation was successfully queued for processing by an I/O thread, | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * or C_ERR if the client is not eligible for offloading. | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * Parameters: | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| * conn: The connection object for which TLS negotiation should be performed | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| */ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| int trySendTLSNegotiationToIOThreads(connection *conn) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (server.io_threads_num <= 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return C_ERR; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (!(conn->flags & CONN_FLAG_CLIENT)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return C_ERR; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| client *c = connGetPrivateData(conn); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (c->io_read_state != CLIENT_IDLE) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return C_OK; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (server.active_io_threads_num <= 1) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return C_ERR; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It seems everywhere else we just check for active threads.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider a scenario where the main thread sends a job to the IO thread. After the IO thread completes the job, the main thread deactivates all threads. In this case, we want to ensure the main thread processes the returned job from the IO thread before performing an accept operation even if the io threads are not active. To achieve this: First, check if IO threads are enabled at all (less expensive check) Unlike read/write where we anyway check for the read/write states, the accept flow operates at the connection layer rather than the client layer, so we can't check the read state there. |
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| size_t thread_id = (c->id % (server.active_io_threads_num - 1)) + 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IOJobQueue *job_queue = &io_jobs[thread_id]; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| if (IOJobQueue_isFull(job_queue)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return C_ERR; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c->read_flags = READ_FLAGS_TLS_NEGOTIATION; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c->io_read_state = CLIENT_PENDING_IO; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| c->flag.pending_read = 1; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| listLinkNodeTail(server.clients_pending_io_read, &c->pending_read_list_node); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| connSetPostponeUpdateState(c->conn, 1); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| server.stat_io_tls_negotiation_offloaded++; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| IOJobQueue_push(job_queue, ioThreadTLSNegotiation, c); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return C_OK; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.