Repository: awslabs/llrt
File: llrt_core/src/modules/embedded/loader.rs (Rust)

Code before:
    };

    let rt = unsafe { qjs::JS_GetRuntime(ctx.as_raw().as_ptr()) };
    let rt_ptr = rt as usize; //hack to move, is safe since runtime is still alive in spawn

    if check_client_inited(rt, endpoint) {
        return Ok(());
    }

    let client = HTTP_CLIENT.as_ref().or_throw(ctx)?;

    trace!("Started client init {}", client_name);
    let mut region = env::var("AWS_REGION").unwrap();
    let mut region_separator = ".";

    //do not use regional endpoint for global services https://docs.aws.amazon.com/general/latest/gr/rande.html#global-endpoints
    if matches!(
        client_name,
        "iam"
            | "route-53"
            | "cloudfront"
            | "waf"
            | "shield"
            | "global-accelerator"
            | "organizations"
            | "networkmanager"
    ) {
        if region != "us-east-1" {
            trace!(
                "Ignoring init for global client: {} because region {} not geographically close to us-east-1",
                client_name,
                region
            );
            mark_client_inited(rt_ptr as _);
            return Ok(());
        }
        region_separator = "";
        region.clear();
    };

    let url_string = [
        "https://",
        endpoint,
        region_separator,
        &region,
        ".amazonaws.com/sping",
    ]
    .concat();

    if let Ok(url) = url_string.parse::<Uri>() {
        tokio::task::spawn(async move {

Review comment (refactor):
If we move this check to before `check_client_inited` we can just return without calling `mark_client_inited` . That saves us from running a mutex and decrementing an atomic counter. 

Code after:
    };

    let mut region = env::var("AWS_REGION").unwrap();
    let mut region_separator = ".";

    //do not use regional endpoint for global services https://docs.aws.amazon.com/general/latest/gr/rande.html#global-endpoints
    if matches!(
        client_name,
        "iam"
            | "route-53"
            | "cloudfront"
            | "waf"
            | "shield"
            | "global-accelerator"
            | "organizations"
            | "networkmanager"
    ) {
        // the latency to do a roundtrip to a global endpoint can exceed the savings if the region is not us-east-1
        if region != "us-east-1" {
            trace!(
                "Ignoring init for global client: {} because region: {} not geographically close to us-east-1",
                client_name,
                region
            );
            return Ok(());
        }
        region_separator = "";
        region.clear();
    };

    let rt = unsafe { qjs::JS_GetRuntime(ctx.as_raw().as_ptr()) };
    let rt_ptr = rt as usize; //hack to move, is safe since runtime is still alive in spawn

    if check_client_inited(rt, endpoint) {
        return Ok(());
    }

    let client = HTTP_CLIENT.as_ref().or_throw(ctx)?;

    trace!("Started client init {}", client_name);

    let url_string = [
        "https://",
        endpoint,
        region_separator,
        &region,
        ".amazonaws.com/sping",
    ]
    .concat();

    if let Ok(url) = url_string.parse::<Uri>() {