The problem is that any request on the client returned "os error 103" when the network interface shutdown.
I called initialize_http_client() again to re-initialize the CLIENT when a new interface is available. but the request still got 103 error.
I also tried not to cache the CLIENT, created it on every requests, but after the network interface switched, the requests returned 103 error.
The error is not related to how you initialize the client.
However, please don't ever use static mut, and in general don't use unsafe if you don't have a sufficient understanding of the language. The code you wrote is unsound, because it provides unsynchronized access to a mutable global.
What's more, it's completely useless. You are cloning the client anyway when accessing it (upon every call to get_cms_client()), so there's absolutely no "caching" going on. You made a wildly unsafe and unsound (ie., wrong) function for absolutely no benefit.
let client = Client::builder().....build()
let mut cms_client = CMS_CLIENT.write().unwrap();
cms_client.push(client);
get the cache and make a request call
let client_cache = CMS_CLIENT.read().unwrap();
let client = client_cache.get(0).unwrap();
let req = client
.get(host_url.to_string())
.query(&build_query(¶ms_map))
.headers(headers)
.build()?;
let resp = client.execute(req)?;
Do you have any comments or suggestion about the code?
I don't get why there still is a separate initialization step if you are now (correctly) performing lazy initialization anyway.
The Vec doesn't seem to be used for anything, since you are (apparently) only pushing a single element to it. Why don't you just store the Client directly instead?
lazy_static works, but once_cell::sync::Lazy (that I linked to in my previous post) is preferable, since there is not much reason to use a macro if a plain function will do. (It also avoids the funny business with let ref.)
the requests have different queries params and headers. I cannot put them into 1st initialization, the 1st initialization only setups the proxy, keep alive, etc;
If you are doing so much context-dependent, dynamic configuration, then just don't involve a static at all. It's not worth it, it only makes the code ugly. Create a new client with the required configuration each time this function is called, and pass it to whatever other function requires it.