Skip to content
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

[FIXED] microservice cleanup (flapping MicroServiceStops... tests) #816

Merged
merged 14 commits into from
Nov 5, 2024
Merged
67 changes: 67 additions & 0 deletions src/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@
natsCondition_Destroy(nc->reconnectCond);
natsMutex_Destroy(nc->subsMu);
natsMutex_Destroy(nc->mu);
NATS_FREE(nc->services);

NATS_FREE(nc);

Expand Down Expand Up @@ -4466,3 +4467,69 @@
}
fflush(stderr);
}

int natsConn_getServices(microService ***services, natsConnection *nc)
{
int numServices = 0;
natsConn_Lock(nc);
*services = nc->services;
numServices = nc->numServices;
natsConn_Unlock(nc);
return numServices;
}

bool natsConn_removeService(natsConnection *nc, microService *service)
{
bool removed = false;
if (nc == NULL || service == NULL)
return false;

natsConn_Lock(nc);
for (int i = 0; i < nc->numServices; i++)
{
if (nc->services[i] == service)
{
for (int j = i; j < nc->numServices - 1; j++)
{
nc->services[j] = nc->services[j + 1];
}
nc->numServices--;
removed = true;
break;
}
}
natsConn_Unlock(nc);
return removed;
}

natsStatus natsConn_addService(natsConnection *nc, microService *service)
{
natsStatus s = NATS_OK;
if (nc == NULL || service == NULL)
return nats_setDefaultError(NATS_INVALID_ARG);

Check warning on line 4509 in src/conn.c

View check run for this annotation

Codecov / codecov/patch

src/conn.c#L4509

Added line #L4509 was not covered by tests

natsConn_Lock(nc);
if (nc->services == NULL)
{
nc->services = NATS_CALLOC(1, sizeof(microService *));
if (nc->services == NULL)
s = nats_setDefaultError(NATS_NO_MEMORY);

Check warning on line 4516 in src/conn.c

View check run for this annotation

Codecov / codecov/patch

src/conn.c#L4516

Added line #L4516 was not covered by tests
}
else
{
microService **tmp = NATS_REALLOC(nc->services, (nc->numServices + 1) * sizeof(microService *));
if (tmp == NULL)
s = nats_setDefaultError(NATS_NO_MEMORY);

Check warning on line 4522 in src/conn.c

View check run for this annotation

Codecov / codecov/patch

src/conn.c#L4522

Added line #L4522 was not covered by tests
else
nc->services = tmp;
}

if (s == NATS_OK)
{
nc->services[nc->numServices] = service;
nc->numServices++;
}
natsConn_Unlock(nc);

return s;
}
9 changes: 9 additions & 0 deletions src/conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,4 +160,13 @@ natsConn_close(natsConnection *nc);
void
natsConn_destroy(natsConnection *nc, bool fromPublicDestroy);

int
natsConn_getServices(microService ***services, natsConnection *nc);

bool
natsConn_removeService(natsConnection *nc, microService *service);

natsStatus
natsConn_addService(natsConnection *nc, microService *service);

#endif /* CONN_H_ */
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
Loading