Skip to content

Commit

Permalink
[FIXED] microservice cleanup (flapping MicroServiceStops... tests) (n…
Browse files Browse the repository at this point in the history
…ats-io#816)

* [FIXED] microservice cleanup (flapping MicroServiceStops... tests)

- Removed the global hash of services, it was the most immediate source of the deadlock.
- Added the list of microservices to `natsConnection`. **NOTE** increased memory allocation for all connections, need a better way.
- Fixed the flow of replacing an existing endpoint in a service.
- Got rid of ep->name since it was synonimous with ep->cfg->Name
- Adjusted the tests, some minor fixes.

* Adjusted test parameters, REPEAT micro on travis

* fixes, cleannup

* Restored Travis

* PR Feedback: include EP name in the error description

* PR Feedback: s/not_used/ignored

* PR Feedback: better connection handling in AddService

* PR Feedback: removed extra indent

* PR Feedback: just move the last array element, do not copy

* PR feedback: removed unneeded return

* PR feedback: quote display names in errors (micro)

* PR feedback: removed trailing whitespace in micro*

* PR feedback
  • Loading branch information
levb committed Dec 9, 2024
1 parent c29f736 commit 21acc14
Show file tree
Hide file tree
Showing 11 changed files with 445 additions and 575 deletions.
2 changes: 1 addition & 1 deletion examples/micro-sequence.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static microError *handle_sequence(microRequest *req)
result_len = snprintf(result, sizeof(result), "%Lf", value);
if (err == NULL)
err = microRequest_Respond(req, result, result_len);

microArgs_Destroy(args);
return err;
}
Expand Down
4 changes: 4 additions & 0 deletions src/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,9 @@ _freeConn(natsConnection *nc)
natsStrHash_Destroy(nc->respMap);
natsCondition_Destroy(nc->reconnectCond);
natsMutex_Destroy(nc->subsMu);
natsMutex_Destroy(nc->servicesMu);
natsMutex_Destroy(nc->mu);
NATS_FREE(nc->services);

NATS_FREE(nc);

Expand Down Expand Up @@ -3256,6 +3258,8 @@ natsConn_create(natsConnection **newConn, natsOptions *options)
s = natsMutex_Create(&(nc->mu));
if (s == NATS_OK)
s = natsMutex_Create(&(nc->subsMu));
if (s == NATS_OK)
s = natsMutex_Create(&(nc->servicesMu));
if (s == NATS_OK)
s = _setupServerPool(nc);
if (s == NATS_OK)
Expand Down
41 changes: 0 additions & 41 deletions src/glib/glib.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,6 @@ _freeLib(void)
nats_freeDispatcherPool(&gLib.replyDispatchers);

natsNUID_free();
natsMutex_Destroy(gLib.service_callback_mu);
natsHash_Destroy(gLib.all_services_to_callback);

natsCondition_Destroy(gLib.cond);

Expand Down Expand Up @@ -290,11 +288,6 @@ nats_openLib(natsClientConfig *config)
if (s == NATS_OK)
s = nats_initDispatcherPool(&(gLib.replyDispatchers), config->ReplyThreadPoolMax);

if (s == NATS_OK)
s = natsMutex_Create(&gLib.service_callback_mu);
if (s == NATS_OK)
s = natsHash_Create(&gLib.all_services_to_callback, 8);

if (s == NATS_OK)
gLib.initialized = true;

Expand Down Expand Up @@ -443,40 +436,6 @@ void nats_ReleaseThreadMemory(void)
natsMutex_Unlock(gLib.lock);
}

natsStatus
natsLib_startServiceCallbacks(microService *m)
{
natsStatus s;

natsMutex_Lock(gLib.service_callback_mu);
s = natsHash_Set(gLib.all_services_to_callback, (int64_t)m, (void *)m, NULL);
natsMutex_Unlock(gLib.service_callback_mu);

return NATS_UPDATE_ERR_STACK(s);
}

void natsLib_stopServiceCallbacks(microService *m)
{
if (m == NULL)
return;

natsMutex_Lock(gLib.service_callback_mu);
natsHash_Remove(gLib.all_services_to_callback, (int64_t)m);
natsMutex_Unlock(gLib.service_callback_mu);
}

natsMutex *
natsLib_getServiceCallbackMutex(void)
{
return gLib.service_callback_mu;
}

natsHash *
natsLib_getAllServicesToCallback(void)
{
return gLib.all_services_to_callback;
}

natsClientConfig *nats_testInspectClientConfig(void)
{
// Immutable after startup
Expand Down
5 changes: 0 additions & 5 deletions src/glib/glibp.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,6 @@ typedef struct __natsLib

natsGCList gc;

// For micro services code
natsMutex *service_callback_mu;
// uses `microService*` as the key and the value.
natsHash *all_services_to_callback;

} natsLib;

natsLib *nats_lib(void);
Expand Down
Loading

0 comments on commit 21acc14

Please sign in to comment.