Skip to content

Commit 3a9d28d

Browse files
committed
Add keepalive to c-libcurl
1 parent 0b0d534 commit 3a9d28d

8 files changed

Lines changed: 120 additions & 4 deletions

File tree

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.c.mustache

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ apiClient_t *apiClient_create() {
1010
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
1111
apiClient->basePath = strdup("{{{basePath}}}");
1212
apiClient->sslConfig = NULL;
13+
apiClient->curlConfig = NULL;
1314
apiClient->dataReceived = NULL;
1415
apiClient->dataReceivedLen = 0;
1516
apiClient->data_callback_func = NULL;
@@ -60,6 +61,12 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
6061
apiClient->sslConfig = NULL;
6162
}
6263

64+
apiClient->curlConfig = malloc(sizeof(curlConfig_t));
65+
apiClient->curlConfig->verbose = 0;
66+
apiClient->curlConfig->keepalive = 0;
67+
apiClient->curlConfig->keepidle = 120;
68+
apiClient->curlConfig->keepintvl = 60;
69+
6370
apiClient->dataReceived = NULL;
6471
apiClient->dataReceivedLen = 0;
6572
apiClient->data_callback_func = NULL;
@@ -142,6 +149,12 @@ void apiClient_free(apiClient_t *apiClient) {
142149
{{/isApiKey}}
143150
{{/authMethods}}
144151
{{/hasAuthMethods}}
152+
153+
if(apiClient->curlConfig) {
154+
free(apiClient->curlConfig);
155+
apiClient->curlConfig = NULL;
156+
}
157+
145158
free(apiClient);
146159
}
147160

@@ -490,6 +503,12 @@ void apiClient_invoke(apiClient_t *apiClient,
490503
operationParameter,
491504
queryParameters);
492505

506+
if(apiClient->curlConfig->keepalive == 1) {
507+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPALIVE, 1L);
508+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPIDLE, apiClient->curlConfig->keepidle);
509+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPINTVL, apiClient->curlConfig->keepintvl);
510+
}
511+
493512
curl_easy_setopt(handle, CURLOPT_URL, targetUrl);
494513
curl_easy_setopt(handle,
495514
CURLOPT_WRITEFUNCTION,
@@ -498,7 +517,7 @@ void apiClient_invoke(apiClient_t *apiClient,
498517
CURLOPT_WRITEDATA,
499518
apiClient);
500519
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
501-
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable
520+
curl_easy_setopt(handle, CURLOPT_VERBOSE, apiClient->curlConfig->verbose);
502521

503522
{{#hasAuthMethods}}
504523
{{#authMethods}}

modules/openapi-generator/src/main/resources/C-libcurl/apiClient.h.mustache

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@ typedef struct sslConfig_t {
1919
/* 1 -- skip ssl verify for server certificate */
2020
} sslConfig_t;
2121

22+
typedef struct curlConfig_t {
23+
long verbose; /* 0 -- disable verbose (default) */
24+
/* 1 -- enable verbose */
25+
int keepalive; /* 0 -- disable keepalive (default) */
26+
/* 1 -- enable keepalive */
27+
long keepidle; /* keep-alive idle time: default to 120 seconds */
28+
long keepintvl; /* interval time between keep-alive probes: default to 60 seconds */
29+
} curlConfig_t;
30+
2231
typedef struct apiClient_t {
2332
char *basePath;
2433
sslConfig_t *sslConfig;
34+
curlConfig_t *curlConfig;
2535
void *dataReceived;
2636
long dataReceivedLen;
2737
void (*data_callback_func)(void **, long *);

samples/client/others/c/bearerAuth/include/apiClient.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@ typedef struct sslConfig_t {
1919
/* 1 -- skip ssl verify for server certificate */
2020
} sslConfig_t;
2121

22+
typedef struct curlConfig_t {
23+
long verbose; /* 0 -- disable verbose (default) */
24+
/* 1 -- enable verbose */
25+
int keepalive; /* 0 -- disable keepalive (default) */
26+
/* 1 -- enable keepalive */
27+
long keepidle; /* keep-alive idle time: default to 120 seconds */
28+
long keepintvl; /* interval time between keep-alive probes: default to 60 seconds */
29+
} curlConfig_t;
30+
2231
typedef struct apiClient_t {
2332
char *basePath;
2433
sslConfig_t *sslConfig;
34+
curlConfig_t *curlConfig;
2535
void *dataReceived;
2636
long dataReceivedLen;
2737
void (*data_callback_func)(void **, long *);

samples/client/others/c/bearerAuth/src/apiClient.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ apiClient_t *apiClient_create() {
1010
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
1111
apiClient->basePath = strdup("http://api.example.com/v1");
1212
apiClient->sslConfig = NULL;
13+
apiClient->curlConfig = NULL;
1314
apiClient->dataReceived = NULL;
1415
apiClient->dataReceivedLen = 0;
1516
apiClient->data_callback_func = NULL;
@@ -37,6 +38,12 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
3738
apiClient->sslConfig = NULL;
3839
}
3940

41+
apiClient->curlConfig = malloc(sizeof(curlConfig_t));
42+
apiClient->curlConfig->verbose = 0;
43+
apiClient->curlConfig->keepalive = 0;
44+
apiClient->curlConfig->keepidle = 120;
45+
apiClient->curlConfig->keepintvl = 60;
46+
4047
apiClient->dataReceived = NULL;
4148
apiClient->dataReceivedLen = 0;
4249
apiClient->data_callback_func = NULL;
@@ -58,6 +65,12 @@ void apiClient_free(apiClient_t *apiClient) {
5865
if(apiClient->accessToken) {
5966
free(apiClient->accessToken);
6067
}
68+
69+
if(apiClient->curlConfig) {
70+
free(apiClient->curlConfig);
71+
apiClient->curlConfig = NULL;
72+
}
73+
6174
free(apiClient);
6275
}
6376

@@ -383,6 +396,12 @@ void apiClient_invoke(apiClient_t *apiClient,
383396
operationParameter,
384397
queryParameters);
385398

399+
if(apiClient->curlConfig->keepalive == 1) {
400+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPALIVE, 1L);
401+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPIDLE, apiClient->curlConfig->keepidle);
402+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPINTVL, apiClient->curlConfig->keepintvl);
403+
}
404+
386405
curl_easy_setopt(handle, CURLOPT_URL, targetUrl);
387406
curl_easy_setopt(handle,
388407
CURLOPT_WRITEFUNCTION,
@@ -391,7 +410,7 @@ void apiClient_invoke(apiClient_t *apiClient,
391410
CURLOPT_WRITEDATA,
392411
apiClient);
393412
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
394-
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable
413+
curl_easy_setopt(handle, CURLOPT_VERBOSE, apiClient->curlConfig->verbose);
395414

396415

397416
if(bodyParameters != NULL) {

samples/client/petstore/c-useJsonUnformatted/include/apiClient.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@ typedef struct sslConfig_t {
1919
/* 1 -- skip ssl verify for server certificate */
2020
} sslConfig_t;
2121

22+
typedef struct curlConfig_t {
23+
long verbose; /* 0 -- disable verbose (default) */
24+
/* 1 -- enable verbose */
25+
int keepalive; /* 0 -- disable keepalive (default) */
26+
/* 1 -- enable keepalive */
27+
long keepidle; /* keep-alive idle time: default to 120 seconds */
28+
long keepintvl; /* interval time between keep-alive probes: default to 60 seconds */
29+
} curlConfig_t;
30+
2231
typedef struct apiClient_t {
2332
char *basePath;
2433
sslConfig_t *sslConfig;
34+
curlConfig_t *curlConfig;
2535
void *dataReceived;
2636
long dataReceivedLen;
2737
void (*data_callback_func)(void **, long *);

samples/client/petstore/c-useJsonUnformatted/src/apiClient.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ apiClient_t *apiClient_create() {
1010
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
1111
apiClient->basePath = strdup("http://petstore.swagger.io/v2");
1212
apiClient->sslConfig = NULL;
13+
apiClient->curlConfig = NULL;
1314
apiClient->dataReceived = NULL;
1415
apiClient->dataReceivedLen = 0;
1516
apiClient->data_callback_func = NULL;
@@ -39,6 +40,12 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
3940
apiClient->sslConfig = NULL;
4041
}
4142

43+
apiClient->curlConfig = malloc(sizeof(curlConfig_t));
44+
apiClient->curlConfig->verbose = 0;
45+
apiClient->curlConfig->keepalive = 0;
46+
apiClient->curlConfig->keepidle = 120;
47+
apiClient->curlConfig->keepintvl = 60;
48+
4249
apiClient->dataReceived = NULL;
4350
apiClient->dataReceivedLen = 0;
4451
apiClient->data_callback_func = NULL;
@@ -85,6 +92,12 @@ void apiClient_free(apiClient_t *apiClient) {
8592
}
8693
list_freeList(apiClient->apiKeys_api_key);
8794
}
95+
96+
if(apiClient->curlConfig) {
97+
free(apiClient->curlConfig);
98+
apiClient->curlConfig = NULL;
99+
}
100+
88101
free(apiClient);
89102
}
90103

@@ -413,6 +426,12 @@ void apiClient_invoke(apiClient_t *apiClient,
413426
operationParameter,
414427
queryParameters);
415428

429+
if(apiClient->curlConfig->keepalive == 1) {
430+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPALIVE, 1L);
431+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPIDLE, apiClient->curlConfig->keepidle);
432+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPINTVL, apiClient->curlConfig->keepintvl);
433+
}
434+
416435
curl_easy_setopt(handle, CURLOPT_URL, targetUrl);
417436
curl_easy_setopt(handle,
418437
CURLOPT_WRITEFUNCTION,
@@ -421,7 +440,7 @@ void apiClient_invoke(apiClient_t *apiClient,
421440
CURLOPT_WRITEDATA,
422441
apiClient);
423442
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
424-
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable
443+
curl_easy_setopt(handle, CURLOPT_VERBOSE, apiClient->curlConfig->verbose);
425444

426445
// this would only be generated for OAuth2 authentication
427446
if(apiClient->accessToken != NULL) {

samples/client/petstore/c/include/apiClient.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,19 @@ typedef struct sslConfig_t {
1919
/* 1 -- skip ssl verify for server certificate */
2020
} sslConfig_t;
2121

22+
typedef struct curlConfig_t {
23+
long verbose; /* 0 -- disable verbose (default) */
24+
/* 1 -- enable verbose */
25+
int keepalive; /* 0 -- disable keepalive (default) */
26+
/* 1 -- enable keepalive */
27+
long keepidle; /* keep-alive idle time: default to 120 seconds */
28+
long keepintvl; /* interval time between keep-alive probes: default to 60 seconds */
29+
} curlConfig_t;
30+
2231
typedef struct apiClient_t {
2332
char *basePath;
2433
sslConfig_t *sslConfig;
34+
curlConfig_t *curlConfig;
2535
void *dataReceived;
2636
long dataReceivedLen;
2737
void (*data_callback_func)(void **, long *);

samples/client/petstore/c/src/apiClient.c

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ apiClient_t *apiClient_create() {
1010
apiClient_t *apiClient = malloc(sizeof(apiClient_t));
1111
apiClient->basePath = strdup("http://petstore.swagger.io/v2");
1212
apiClient->sslConfig = NULL;
13+
apiClient->curlConfig = NULL;
1314
apiClient->dataReceived = NULL;
1415
apiClient->dataReceivedLen = 0;
1516
apiClient->data_callback_func = NULL;
@@ -39,6 +40,12 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath
3940
apiClient->sslConfig = NULL;
4041
}
4142

43+
apiClient->curlConfig = malloc(sizeof(curlConfig_t));
44+
apiClient->curlConfig->verbose = 0;
45+
apiClient->curlConfig->keepalive = 0;
46+
apiClient->curlConfig->keepidle = 120;
47+
apiClient->curlConfig->keepintvl = 60;
48+
4249
apiClient->dataReceived = NULL;
4350
apiClient->dataReceivedLen = 0;
4451
apiClient->data_callback_func = NULL;
@@ -85,6 +92,12 @@ void apiClient_free(apiClient_t *apiClient) {
8592
}
8693
list_freeList(apiClient->apiKeys_api_key);
8794
}
95+
96+
if(apiClient->curlConfig) {
97+
free(apiClient->curlConfig);
98+
apiClient->curlConfig = NULL;
99+
}
100+
88101
free(apiClient);
89102
}
90103

@@ -413,6 +426,12 @@ void apiClient_invoke(apiClient_t *apiClient,
413426
operationParameter,
414427
queryParameters);
415428

429+
if(apiClient->curlConfig->keepalive == 1) {
430+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPALIVE, 1L);
431+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPIDLE, apiClient->curlConfig->keepidle);
432+
curl_easy_setopt(handle, CURLOPT_TCP_KEEPINTVL, apiClient->curlConfig->keepintvl);
433+
}
434+
416435
curl_easy_setopt(handle, CURLOPT_URL, targetUrl);
417436
curl_easy_setopt(handle,
418437
CURLOPT_WRITEFUNCTION,
@@ -421,7 +440,7 @@ void apiClient_invoke(apiClient_t *apiClient,
421440
CURLOPT_WRITEDATA,
422441
apiClient);
423442
curl_easy_setopt(handle, CURLOPT_HTTPHEADER, headers);
424-
curl_easy_setopt(handle, CURLOPT_VERBOSE, 0); // to get curl debug msg 0: to disable, 1L:to enable
443+
curl_easy_setopt(handle, CURLOPT_VERBOSE, apiClient->curlConfig->verbose);
425444

426445
// this would only be generated for OAuth2 authentication
427446
if(apiClient->accessToken != NULL) {

0 commit comments

Comments
 (0)