Skip to content

Commit

Permalink
[Multi-Thread] Support working in multi-thread environment
Browse files Browse the repository at this point in the history
  • Loading branch information
ityuhui committed Oct 27, 2020
1 parent 916f56c commit 99c0ed1
Show file tree
Hide file tree
Showing 12 changed files with 33 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
kubernetes/build/
.vs/
.vscode/

# Prerequisites
*.d
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ list all pods:
basePath = NULL;
sslConfig = NULL;
apiKeys = NULL;
apiClient_unsetupGlobalEnv();
```
list all pods in cluster:
Expand Down Expand Up @@ -121,6 +122,7 @@ list all pods in cluster:
basePath = NULL;
sslConfig = NULL;
apiKeys = NULL;
apiClient_unsetupGlobalEnv();
```


Expand Down
1 change: 1 addition & 0 deletions examples/auth_provider/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ int main(int argc, char *argv[])
basePath = NULL;
sslConfig = NULL;
apiKeys = NULL;
apiClient_unsetupGlobalEnv();

return 0;
}
2 changes: 2 additions & 0 deletions examples/create_pod/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,7 @@ int main(int argc, char *argv[])

apiClient_free(k8sApiClient);
k8sApiClient = NULL;
apiClient_unsetupGlobalEnv();

return 0;
}
1 change: 1 addition & 0 deletions examples/delete_pod/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ int main(int argc, char *argv[])
basePath = NULL;
sslConfig = NULL;
apiKeys = NULL;
apiClient_unsetupGlobalEnv();

return 0;
}
1 change: 1 addition & 0 deletions examples/exec_provider/list_pod_by_exec_provider.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ int main(int argc, char *argv[])

apiClient_free(k8sApiClient);
k8sApiClient = NULL;
apiClient_unsetupGlobalEnv();

return rc;
}
1 change: 1 addition & 0 deletions examples/generic/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ int main(int argc, char *argv[])
basePath = NULL;
sslConfig = NULL;
apiKeys = NULL;
apiClient_unsetupGlobalEnv();

return 0;
}
1 change: 1 addition & 0 deletions examples/list_pod/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ int main(int argc, char *argv[])
basePath = NULL;
sslConfig = NULL;
apiKeys = NULL;
apiClient_unsetupGlobalEnv();

return 0;
}
1 change: 1 addition & 0 deletions examples/list_pod_incluster/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ int main(int argc, char *argv[])
basePath = NULL;
sslConfig = NULL;
apiKeys = NULL;
apiClient_unsetupGlobalEnv();

return 0;
}
1 change: 1 addition & 0 deletions examples/watch_list_pod/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ int main(int argc, char *argv[])
basePath = NULL;
sslConfig = NULL;
apiKeys = NULL;
apiClient_unsetupGlobalEnv();

return 0;
}
12 changes: 12 additions & 0 deletions kubernetes/include/apiClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
11 changes: 9 additions & 2 deletions kubernetes/src/apiClient.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 99c0ed1

Please sign in to comment.