Skip to content

Commit cde2c4e

Browse files
committed
2.2.0dev: add oauth2_ipc_thread_mutex_t
and use it for Redis, cURL and global lists to improve performance across multiple processes running on the same host Signed-off-by: Hans Zandbelt <[email protected]>
1 parent defdfeb commit cde2c4e

File tree

12 files changed

+123
-36
lines changed

12 files changed

+123
-36
lines changed

ChangeLog

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
08/28/2025
2+
- add oauth2_ipc_thread_mutex_t and use it for Redis, cURL and global lists
3+
to improve performance across multiple processes running on the same host
4+
- bump to 2.2.0dev
5+
16
08/08/2025
27
- release 2.1.1
38

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
AC_INIT([liboauth2],[2.1.1],[[email protected]])
1+
AC_INIT([liboauth2],[2.2.0dev],[[email protected]])
22

33
AM_INIT_AUTOMAKE([foreign no-define subdir-objects])
44
AC_CONFIG_MACRO_DIR([m4])

include/oauth2/ipc.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@ bool oauth2_ipc_mutex_post_config(oauth2_log_t *log, oauth2_ipc_mutex_t *m);
3333
bool oauth2_ipc_mutex_lock(oauth2_log_t *log, oauth2_ipc_mutex_t *m);
3434
bool oauth2_ipc_mutex_unlock(oauth2_log_t *log, oauth2_ipc_mutex_t *m);
3535

36+
OAUTH2_TYPE_DECLARE(ipc, thread_mutex)
37+
bool oauth2_ipc_thread_mutex_lock(oauth2_log_t *log,
38+
oauth2_ipc_thread_mutex_t *m);
39+
bool oauth2_ipc_thread_mutex_unlock(oauth2_log_t *log,
40+
oauth2_ipc_thread_mutex_t *m);
41+
3642
OAUTH2_TYPE_DECLARE(ipc, sema)
3743
bool oauth2_ipc_sema_post_config(oauth2_log_t *log, oauth2_ipc_sema_t *sema);
3844
bool oauth2_ipc_sema_post(oauth2_log_t *log, oauth2_ipc_sema_t *sema);

src/cache/redis.c

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
#include "hiredis/hiredis.h"
3030

3131
typedef struct oauth2_cache_impl_redis_t {
32-
oauth2_ipc_mutex_t *mutex;
32+
oauth2_ipc_thread_mutex_t *mutex;
3333
char *host_str;
3434
oauth2_uint_t port;
3535
char *username;
@@ -55,7 +55,7 @@ static bool oauth2_cache_redis_init(oauth2_log_t *log, oauth2_cache_t *cache,
5555
cache->impl = impl;
5656
cache->type = &oauth2_cache_redis;
5757

58-
impl->mutex = oauth2_ipc_mutex_init(log);
58+
impl->mutex = oauth2_ipc_thread_mutex_init(log);
5959
if (impl->mutex == NULL)
6060
goto end;
6161

@@ -97,15 +97,15 @@ static bool oauth2_cache_redis_free(oauth2_log_t *log, oauth2_cache_t *cache)
9797
goto end;
9898

9999
if (impl->mutex) {
100-
oauth2_ipc_mutex_lock(log, impl->mutex);
100+
oauth2_ipc_thread_mutex_lock(log, impl->mutex);
101101

102102
if (impl->ctx) {
103103
redisFree(impl->ctx);
104104
impl->ctx = NULL;
105105
}
106106

107-
oauth2_ipc_mutex_unlock(log, impl->mutex);
108-
oauth2_ipc_mutex_free(log, impl->mutex);
107+
oauth2_ipc_thread_mutex_unlock(log, impl->mutex);
108+
oauth2_ipc_thread_mutex_free(log, impl->mutex);
109109

110110
impl->mutex = NULL;
111111
}
@@ -141,10 +141,6 @@ static bool oauth2_cache_redis_post_config(oauth2_log_t *log,
141141
if (impl == NULL)
142142
goto end;
143143

144-
rc = oauth2_ipc_mutex_post_config(log, impl->mutex);
145-
if (rc == false)
146-
goto end;
147-
148144
// TODO: connect to the server here?
149145

150146
rc = true;
@@ -299,7 +295,7 @@ static bool oauth2_cache_redis_get(oauth2_log_t *log, oauth2_cache_t *cache,
299295

300296
*value = NULL;
301297

302-
if (oauth2_ipc_mutex_lock(log, impl->mutex) == false)
298+
if (oauth2_ipc_thread_mutex_lock(log, impl->mutex) == false)
303299
goto end;
304300

305301
cmd = oauth2_stradd(NULL, "GET", " ", key);
@@ -326,7 +322,7 @@ static bool oauth2_cache_redis_get(oauth2_log_t *log, oauth2_cache_t *cache,
326322

327323
unlock:
328324

329-
oauth2_ipc_mutex_unlock(log, impl->mutex);
325+
oauth2_ipc_thread_mutex_unlock(log, impl->mutex);
330326

331327
end:
332328

@@ -358,7 +354,7 @@ static bool oauth2_cache_redis_set(oauth2_log_t *log, oauth2_cache_t *cache,
358354
if (impl == NULL)
359355
goto end;
360356

361-
if (oauth2_ipc_mutex_lock(log, impl->mutex) == false)
357+
if (oauth2_ipc_thread_mutex_lock(log, impl->mutex) == false)
362358
goto end;
363359

364360
if (value) {
@@ -382,7 +378,7 @@ static bool oauth2_cache_redis_set(oauth2_log_t *log, oauth2_cache_t *cache,
382378

383379
unlock:
384380

385-
oauth2_ipc_mutex_unlock(log, impl->mutex);
381+
oauth2_ipc_thread_mutex_unlock(log, impl->mutex);
386382

387383
end:
388384

src/cfg/auth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ oauth2_cfg_endpoint_auth_basic_options_set(oauth2_log_t *log,
380380
}
381381

382382
typedef char *(
383-
oauth2_cfg_endpoint_auth_set_options_cb_t)(oauth2_log_t *log,
383+
oauth2_cfg_endpoint_auth_set_options_cb_t)(oauth2_log_t * log,
384384
oauth2_cfg_endpoint_auth_t *auth,
385385
const oauth2_nv_list_t *params);
386386

src/cfg/source.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ oauth2_cfg_accept_in_cookie_options_set(oauth2_log_t *log,
9696
}
9797

9898
typedef char *(
99-
oauth2_cfg_accept_token_in_set_options_cb_t)(oauth2_log_t *log,
99+
oauth2_cfg_accept_token_in_set_options_cb_t)(oauth2_log_t * log,
100100
oauth2_cfg_token_in_t
101101
*accept_in,
102102
const oauth2_nv_list_t

src/cfg_int.h

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ char *oauth2_cfg_cache_set_options(oauth2_log_t *log, const char *type,
136136
* verify
137137
*/
138138

139-
typedef void *(oauth2_cfg_ctx_init_cb)(oauth2_log_t *log);
140-
typedef void *(oauth2_cfg_ctx_clone_cb)(oauth2_log_t *log, void *src);
139+
typedef void *(oauth2_cfg_ctx_init_cb)(oauth2_log_t * log);
140+
typedef void *(oauth2_cfg_ctx_clone_cb)(oauth2_log_t * log, void *src);
141141
typedef void(oauth2_cfg_ctx_free_cb)(oauth2_log_t *log, void *);
142142

143143
typedef struct oauth2_cfg_ctx_funcs_t {
@@ -194,7 +194,7 @@ typedef struct oauth2_cfg_token_verify_t {
194194
struct oauth2_cfg_token_verify_t *next;
195195
} oauth2_cfg_token_verify_t;
196196

197-
typedef char *(oauth2_cfg_set_options_cb_t)(oauth2_log_t *log,
197+
typedef char *(oauth2_cfg_set_options_cb_t)(oauth2_log_t * log,
198198
const char *value,
199199
const oauth2_nv_list_t *params,
200200
void *cfg);
@@ -366,26 +366,25 @@ oauth2_cfg_session_t *_oauth2_cfg_session_obtain(oauth2_log_t *log,
366366
} oauth2_##name##_list_t; \
367367
\
368368
static oauth2_##name##_list_t *_oauth2_##name##_list = NULL; \
369-
static oauth2_ipc_mutex_t *_oauth2_##name##_list_mutex = NULL; \
369+
static oauth2_ipc_thread_mutex_t *_oauth2_##name##_list_mutex = NULL; \
370370
\
371371
static bool _M_##name##_list_lock(oauth2_log_t *log) \
372372
{ \
373373
bool rc = false; \
374374
if (_oauth2_##name##_list_mutex == NULL) { \
375375
_oauth2_##name##_list_mutex = \
376-
oauth2_ipc_mutex_init(log); \
377-
oauth2_ipc_mutex_post_config( \
378-
log, _oauth2_##name##_list_mutex); \
376+
oauth2_ipc_thread_mutex_init(log); \
379377
} \
380-
rc = oauth2_ipc_mutex_lock(log, _oauth2_##name##_list_mutex); \
378+
rc = oauth2_ipc_thread_mutex_lock( \
379+
log, _oauth2_##name##_list_mutex); \
381380
return rc; \
382381
} \
383382
\
384383
static bool _M_##name##_list_unlock(oauth2_log_t *log) \
385384
{ \
386385
bool rc = false; \
387-
rc = \
388-
oauth2_ipc_mutex_unlock(log, _oauth2_##name##_list_mutex); \
386+
rc = oauth2_ipc_thread_mutex_unlock( \
387+
log, _oauth2_##name##_list_mutex); \
389388
return rc; \
390389
} \
391390
\
@@ -473,8 +472,8 @@ oauth2_cfg_session_t *_oauth2_cfg_session_obtain(oauth2_log_t *log,
473472
_M_##name##_list_unlock(log); \
474473
\
475474
if (_oauth2_##name##_list_mutex != NULL) { \
476-
oauth2_ipc_mutex_free(log, \
477-
_oauth2_##name##_list_mutex); \
475+
oauth2_ipc_thread_mutex_free( \
476+
log, _oauth2_##name##_list_mutex); \
478477
_oauth2_##name##_list_mutex = NULL; \
479478
} \
480479
}

src/ipc.c

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
#include <string.h>
3434
#include <sys/stat.h>
3535

36+
#include <pthread.h>
37+
3638
#include "oauth2/ipc.h"
3739
#include "oauth2/mem.h"
3840
#include "oauth2/util.h"
@@ -272,6 +274,67 @@ bool oauth2_ipc_mutex_unlock(oauth2_log_t *log, oauth2_ipc_mutex_t *m)
272274
return rc;
273275
}
274276

277+
/*
278+
* thread mutex
279+
*/
280+
281+
typedef struct oauth2_ipc_thread_mutex_t {
282+
pthread_mutex_t mutex;
283+
} oauth2_ipc_thread_mutex_t;
284+
285+
oauth2_ipc_thread_mutex_t *oauth2_ipc_thread_mutex_init(oauth2_log_t *log)
286+
{
287+
oauth2_ipc_thread_mutex_t *m =
288+
oauth2_mem_alloc(sizeof(oauth2_ipc_thread_mutex_t));
289+
if (m) {
290+
pthread_mutex_init(&m->mutex, NULL);
291+
}
292+
return m;
293+
}
294+
295+
void oauth2_ipc_thread_mutex_free(oauth2_log_t *log,
296+
oauth2_ipc_thread_mutex_t *m)
297+
{
298+
if (m == NULL)
299+
goto end;
300+
pthread_mutex_destroy(&m->mutex);
301+
oauth2_mem_free(m);
302+
303+
end:
304+
305+
return;
306+
}
307+
308+
bool oauth2_ipc_thread_mutex_lock(oauth2_log_t *log,
309+
oauth2_ipc_thread_mutex_t *m)
310+
{
311+
bool rc = false;
312+
313+
if (m == NULL)
314+
goto end;
315+
316+
rc = (pthread_mutex_lock(&m->mutex) == 0);
317+
318+
end:
319+
320+
return rc;
321+
}
322+
323+
bool oauth2_ipc_thread_mutex_unlock(oauth2_log_t *log,
324+
oauth2_ipc_thread_mutex_t *m)
325+
{
326+
bool rc = false;
327+
328+
if (m == NULL)
329+
goto end;
330+
331+
rc = (pthread_mutex_unlock(&m->mutex) == 0);
332+
333+
end:
334+
335+
return rc;
336+
}
337+
275338
/*
276339
* shared memory
277340
*/

src/jose.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1913,7 +1913,7 @@ oauth2_jose_jwks_list_resolve(oauth2_log_t *log,
19131913
}
19141914

19151915
typedef oauth2_jose_jwk_list_t *(
1916-
oauth2_jose_jwks_url_resolve_response_cb_t)(oauth2_log_t *log,
1916+
oauth2_jose_jwks_url_resolve_response_cb_t)(oauth2_log_t * log,
19171917
char *response);
19181918

19191919
// cater for the (Amazon ALB) use case that only a single EC(!) key is served

src/util.c

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
#include "cfg_int.h"
4141

4242
static CURL *_s_curl = NULL;
43-
static oauth2_ipc_mutex_t *_curl_mutex = NULL;
43+
static oauth2_ipc_thread_mutex_t *_curl_mutex = NULL;
4444

4545
oauth2_log_t *oauth2_init(oauth2_log_level_t level, oauth2_log_sink_t *sink)
4646
{
@@ -56,8 +56,7 @@ oauth2_log_t *oauth2_init(oauth2_log_level_t level, oauth2_log_sink_t *sink)
5656
// possibly providing alloc funcs as part of init?
5757
curl_global_init(CURL_GLOBAL_ALL);
5858
log = oauth2_log_init(level, sink);
59-
_curl_mutex = oauth2_ipc_mutex_init(log);
60-
oauth2_ipc_mutex_post_config(log, _curl_mutex);
59+
_curl_mutex = oauth2_ipc_thread_mutex_init(log);
6160
return log;
6261
}
6362

@@ -71,7 +70,7 @@ void oauth2_shutdown(oauth2_log_t *log)
7170
_s_curl = NULL;
7271
}
7372
if (_curl_mutex != NULL) {
74-
oauth2_ipc_mutex_free(log, _curl_mutex);
73+
oauth2_ipc_thread_mutex_free(log, _curl_mutex);
7574
_curl_mutex = NULL;
7675
}
7776
curl_global_cleanup();
@@ -256,7 +255,7 @@ int oauth2_strnenvcmp(const char *a, const char *b, int len)
256255

257256
static CURL *oauth2_curl_init(oauth2_log_t *log)
258257
{
259-
oauth2_ipc_mutex_lock(log, _curl_mutex);
258+
oauth2_ipc_thread_mutex_lock(log, _curl_mutex);
260259
if (_s_curl == NULL) {
261260
_s_curl = curl_easy_init();
262261
if (_s_curl == NULL) {
@@ -268,7 +267,7 @@ static CURL *oauth2_curl_init(oauth2_log_t *log)
268267

269268
static void oauth2_curl_free(oauth2_log_t *log, CURL *curl)
270269
{
271-
oauth2_ipc_mutex_unlock(log, _curl_mutex);
270+
oauth2_ipc_thread_mutex_unlock(log, _curl_mutex);
272271
}
273272

274273
char *oauth2_url_encode(oauth2_log_t *log, const char *src)

0 commit comments

Comments
 (0)