Skip to content

Implement curl KeepAlive for watch connections #275

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/watch_list_pod/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ void watch_list_pod(apiClient_t * apiClient)
int watch = 1;
int timeoutSeconds = 30; /* Watch for 30 seconds */
// int timeoutSeconds = 0; /* Set timeoutSeconds to 0 to keep watching and not exit */
// apiClient->curlConfig->keepalive = 1; /* Enable keepalive */
CoreV1API_listNamespacedPod(apiClient, "default", /*namespace */
NULL, /* pretty */
NULL, /* allowWatchBookmarks */
Expand Down
10 changes: 10 additions & 0 deletions kubernetes/include/apiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,19 @@ typedef struct sslConfig_t {
/* 1 -- skip ssl verify for server certificate */
} sslConfig_t;

typedef struct curl_config_t {
int keepalive; /* 0 -- disable keepalive */
/* 1 -- enable keepalive */
long keepidle; /* keep-alive idle time: default to 120 seconds */
long keepintvl; /* interval time between keep-alive probes: default to 60 seconds */
} curl_config_t;

bool isWatchCall(list_t *queryParameters);

typedef struct apiClient_t {
char *basePath;
sslConfig_t *sslConfig;
curl_config_t *curlConfig;
void *dataReceived;
long dataReceivedLen;
void (*data_callback_func)(void **, long *);
Expand Down
32 changes: 32 additions & 0 deletions kubernetes/src/apiClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,25 @@

size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp);

bool isWatchCall(list_t * queryParameters)
{
bool found = false;
listEntry_t *listEntry = NULL;
list_ForEach(listEntry, queryParameters) {
keyValuePair_t *pair = listEntry->data;
if (strcmp(pair->key, "watch") == 0) {
found = (strcmp(pair->key, "1") == 0);
break;
}
}
return found;
}

apiClient_t *apiClient_create() {
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
apiClient->basePath = strdup("http://localhost");
apiClient->sslConfig = NULL;
apiClient->curlConfig = NULL;
apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->data_callback_func = NULL;
Expand Down Expand Up @@ -38,6 +53,11 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
apiClient->sslConfig = NULL;
}

apiClient->curlConfig = malloc(sizeof(curl_config_t));
apiClient->curlConfig->keepalive = 0; // default value, it can be set to 1 to enable keepalive by user
apiClient->curlConfig->keepidle = 120; // default value
apiClient->curlConfig->keepintvl = 60; // default value

apiClient->dataReceived = NULL;
apiClient->dataReceivedLen = 0;
apiClient->data_callback_func = NULL;
Expand Down Expand Up @@ -80,6 +100,10 @@ void apiClient_free(apiClient_t *apiClient) {
}
list_freeList(apiClient->apiKeys_BearerToken);
}
if(apiClient->curlConfig) {
free(apiClient->curlConfig);
apiClient->curlConfig = NULL;
}
free(apiClient);
}

Expand Down Expand Up @@ -407,6 +431,14 @@ void apiClient_invoke(apiClient_t *apiClient,
assembleTargetUrl(apiClient->basePath,
operationParameter,
queryParameters);

if(apiClient->curlConfig->keepalive == 1 && isWatchCall(queryParameters)) {
curl_easy_setopt(handle, CURLOPT_TCP_KEEPALIVE, 1L);
curl_easy_setopt(handle, CURLOPT_TCP_KEEPIDLE, apiClient->curlConfig->keepidle);
curl_easy_setopt(handle, CURLOPT_TCP_KEEPINTVL, apiClient->curlConfig->keepintvl);
// curl_easy_setopt(handle, CURLOPT_TCP_KEEPCNT, apiClient->curlConfig->keepcnt);
}


curl_easy_setopt(handle, CURLOPT_URL, targetUrl);
curl_easy_setopt(handle,
Expand Down