From 99c0ed1c65faa9dff61e13de23991a72225d0b04 Mon Sep 17 00:00:00 2001 From: Hui Yu Date: Tue, 27 Oct 2020 20:33:38 +0800 Subject: [PATCH] [Multi-Thread] Support working in multi-thread environment --- .gitignore | 1 + README.md | 2 ++ examples/auth_provider/main.c | 1 + examples/create_pod/main.c | 2 ++ examples/delete_pod/main.c | 1 + examples/exec_provider/list_pod_by_exec_provider.c | 1 + examples/generic/main.c | 1 + examples/list_pod/main.c | 1 + examples/list_pod_incluster/main.c | 1 + examples/watch_list_pod/main.c | 1 + kubernetes/include/apiClient.h | 12 ++++++++++++ kubernetes/src/apiClient.c | 11 +++++++++-- 12 files changed, 33 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 776e53c6..d7deb138 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ kubernetes/build/ .vs/ +.vscode/ # Prerequisites *.d diff --git a/README.md b/README.md index 52793b36..17ec1aba 100644 --- a/README.md +++ b/README.md @@ -78,6 +78,7 @@ list all pods: basePath = NULL; sslConfig = NULL; apiKeys = NULL; + apiClient_unsetupGlobalEnv(); ``` list all pods in cluster: @@ -121,6 +122,7 @@ list all pods in cluster: basePath = NULL; sslConfig = NULL; apiKeys = NULL; + apiClient_unsetupGlobalEnv(); ``` diff --git a/examples/auth_provider/main.c b/examples/auth_provider/main.c index 88b0fe52..fa65270b 100644 --- a/examples/auth_provider/main.c +++ b/examples/auth_provider/main.c @@ -59,6 +59,7 @@ int main(int argc, char *argv[]) basePath = NULL; sslConfig = NULL; apiKeys = NULL; + apiClient_unsetupGlobalEnv(); return 0; } diff --git a/examples/create_pod/main.c b/examples/create_pod/main.c index 17a869b0..e44dcf2a 100644 --- a/examples/create_pod/main.c +++ b/examples/create_pod/main.c @@ -96,5 +96,7 @@ int main(int argc, char *argv[]) apiClient_free(k8sApiClient); k8sApiClient = NULL; + apiClient_unsetupGlobalEnv(); + return 0; } diff --git a/examples/delete_pod/main.c b/examples/delete_pod/main.c index 14412f9e..c2794777 100644 --- a/examples/delete_pod/main.c +++ b/examples/delete_pod/main.c @@ -58,6 +58,7 @@ int main(int argc, char *argv[]) basePath = NULL; sslConfig = NULL; apiKeys = NULL; + apiClient_unsetupGlobalEnv(); return 0; } diff --git a/examples/exec_provider/list_pod_by_exec_provider.c b/examples/exec_provider/list_pod_by_exec_provider.c index 9f0c3d46..29ee7fb2 100644 --- a/examples/exec_provider/list_pod_by_exec_provider.c +++ b/examples/exec_provider/list_pod_by_exec_provider.c @@ -63,6 +63,7 @@ int main(int argc, char *argv[]) apiClient_free(k8sApiClient); k8sApiClient = NULL; + apiClient_unsetupGlobalEnv(); return rc; } diff --git a/examples/generic/main.c b/examples/generic/main.c index 95703468..0c6e590a 100644 --- a/examples/generic/main.c +++ b/examples/generic/main.c @@ -60,6 +60,7 @@ int main(int argc, char *argv[]) basePath = NULL; sslConfig = NULL; apiKeys = NULL; + apiClient_unsetupGlobalEnv(); return 0; } diff --git a/examples/list_pod/main.c b/examples/list_pod/main.c index 2d601777..02c8339d 100644 --- a/examples/list_pod/main.c +++ b/examples/list_pod/main.c @@ -59,6 +59,7 @@ int main(int argc, char *argv[]) basePath = NULL; sslConfig = NULL; apiKeys = NULL; + apiClient_unsetupGlobalEnv(); return 0; } diff --git a/examples/list_pod_incluster/main.c b/examples/list_pod_incluster/main.c index 426c8650..03bb2dba 100644 --- a/examples/list_pod_incluster/main.c +++ b/examples/list_pod_incluster/main.c @@ -59,6 +59,7 @@ int main(int argc, char *argv[]) basePath = NULL; sslConfig = NULL; apiKeys = NULL; + apiClient_unsetupGlobalEnv(); return 0; } diff --git a/examples/watch_list_pod/main.c b/examples/watch_list_pod/main.c index debe6850..f11bda45 100644 --- a/examples/watch_list_pod/main.c +++ b/examples/watch_list_pod/main.c @@ -105,6 +105,7 @@ int main(int argc, char *argv[]) basePath = NULL; sslConfig = NULL; apiKeys = NULL; + apiClient_unsetupGlobalEnv(); return 0; } diff --git a/kubernetes/include/apiClient.h b/kubernetes/include/apiClient.h index 397381ca..e937c5ef 100644 --- a/kubernetes/include/apiClient.h +++ b/kubernetes/include/apiClient.h @@ -45,4 +45,16 @@ void sslConfig_free(sslConfig_t *sslConfig); char *strReplace(char *orig, char *rep, char *with); +/* + * In single thread program, the function apiClient_setupGlobalEnv is not needed. + * But in multi-thread program, apiClient_setupGlobalEnv must be called before any worker thread is created + */ +void apiClient_setupGlobalEnv(); + +/* + * This function apiClient_unsetupGlobalEnv must be called whether single or multiple program. + * In multi-thread program, it is must be called after all worker threads end. + */ +void apiClient_unsetupGlobalEnv(); + #endif // INCLUDE_API_CLIENT_H diff --git a/kubernetes/src/apiClient.c b/kubernetes/src/apiClient.c index 4784d892..4e35356a 100644 --- a/kubernetes/src/apiClient.c +++ b/kubernetes/src/apiClient.c @@ -6,6 +6,14 @@ size_t writeDataCallback(void *buffer, size_t size, size_t nmemb, void *userp); +void apiClient_setupGlobalEnv() { + curl_global_init(CURL_GLOBAL_ALL); +} + +void apiClient_unsetupGlobalEnv() { + curl_global_cleanup(); +} + apiClient_t *apiClient_create() { curl_global_init(CURL_GLOBAL_ALL); apiClient_t *apiClient = malloc(sizeof(apiClient_t)); @@ -24,7 +32,7 @@ apiClient_t *apiClient_create_with_base_path(const char *basePath , sslConfig_t *sslConfig , list_t *apiKeys_BearerToken ) { - curl_global_init(CURL_GLOBAL_ALL); + apiClient_t *apiClient = malloc(sizeof(apiClient_t)); if(basePath){ apiClient->basePath = strdup(basePath); @@ -78,7 +86,6 @@ void apiClient_free(apiClient_t *apiClient) { } free(apiClient); - curl_global_cleanup(); } sslConfig_t *sslConfig_create(const char *clientCertFile, const char *clientKeyFile, const char *CACertFile, int insecureSkipTlsVerify) {