Skip to content

Commit

Permalink
fixes, cleannup
Browse files Browse the repository at this point in the history
  • Loading branch information
levb committed Nov 4, 2024
1 parent bc40ea7 commit 8479c08
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 158 deletions.
69 changes: 3 additions & 66 deletions src/conn.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ _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);

Expand Down Expand Up @@ -3239,6 +3240,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 Expand Up @@ -4467,69 +4470,3 @@ natsConn_defaultErrHandler(natsConnection *nc, natsSubscription *sub, natsStatus
}
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);

natsConn_Lock(nc);
if (nc->services == NULL)
{
nc->services = NATS_CALLOC(1, sizeof(microService *));
if (nc->services == NULL)
s = nats_setDefaultError(NATS_NO_MEMORY);
}
else
{
microService **tmp = NATS_REALLOC(nc->services, (nc->numServices + 1) * sizeof(microService *));
if (tmp == NULL)
s = nats_setDefaultError(NATS_NO_MEMORY);
else
nc->services = tmp;
}

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

return s;
}
9 changes: 0 additions & 9 deletions src/conn.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,13 +160,4 @@ 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_ */
Loading

0 comments on commit 8479c08

Please sign in to comment.