Skip to content

Marchusness/cloud_ping

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

29 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cloud Ping

Simple api that returns the latency data from the closest cloudflare region to all aws regions.

There is also a basic webpage for viewing all latency data at https://marchusness.github.io/cloud_ping/web/

How it works

The api will get the cloudflare data center code from the worker handling the request using request.cf?.colo. The KV is checked to see if the latency data has been calculated and cached for the current cloudflare data center code. The API will return the latency stats that was stored. After the request returned the stats, the worker will ping all aws regions and update the D1 with the first and second latency ping. A cron job runs every 10 minutes to calculate the latency stats from d1 and store the stats in KV.

API

GET or POST 
https://api.cloudping.dev
Query parameters
Response format
type AnalyticDocument = {
  results: {
    region: (AWSRegion | ChinaAwsRegion);
    regionName: string;
    firstPingAnalytics: {
      min: number;
      max: number;
      avg: number;
      stdDev: number;
      p50: number;
      p90: number;
      p99: number;
    };
    secondPingAnalytics: {
      min: number;
      max: number;
      avg: number;
      stdDev: number;
      p50: number;
      p90: number;
      p99: number;
    };
  }[];
  cloudflareDataCenterAirportCode: string;
  pingCount: number;
  sourceCode: string;
}

Example use case

You are using cloudflare worker as a reverse proxy to your aws infrastructure. Your infrastructure is deployed in multiple regions. You want to know the latency from the current cloudflare data center to all aws regions so you can route the traffic to the closest region.

Example Cloudflare Worker code

import { AWS_REGION_TO_ENDPOINT, DEFAULT_ENDPOINT } from "./awsRegionMapper";
import { getAws4FetchClient } from "./aws4FetchClient";

export default {
  fetch: async (request: Request, env: Env, ctx: ExecutionContext) => {
    const currentDataCenter = request.cf.colo;
    const response = await fetch(`https://api.cloudping.dev?cloudflareDataCenterAirportCode=${currentDataCenter}`);

    const data = await response.json() as {
      results: {
        region: string;
        secondPingAnalytics: {
          avg: number;
        };
      }[];
    };

    const closestRegion = data.results
      .sort((a, b) => a.secondPingAnalytics.avg - b.secondPingAnalytics.avg)
      .filter((res) => AWS_REGION_TO_ENDPOINT[res.region])[0];

    const closestRegionEndpoint = AWS_REGION_TO_ENDPOINT[closestRegion.region] ?? DEFAULT_ENDPOINT;

    return getAws4FetchClient(env).fetch(closestRegionEndpoint);
  }
} satisfies ExportedHandler<Env>;

About

cloudflare worker for getting the latency stats from the current worker to all aws regions

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published