Skip to content

Quick Start

Xapp73 edited this page Dec 5, 2019 · 14 revisions

Create and manage Custom FlexBalancer Answer

  1. Create FlexBalancer with Custom Answer
  2. Edit FlexBalancer with Custom Answer
  3. Delete FlexBalancer

Create FlexBalancer with custom answer

First of all, log in and proceed to FlexBalancers page:

Alt text

Select name for your Balancer, Custom code button and press Next Step:

Alt text

Then, fill in your fallback address. You may also change default TTL, or keep it equal to 10. After that, press Create New FlexBalancer button:

Alt text

Now, you have text editor open, so you can write or post there your custom code.

Supported language: TypeScript.

Editor: Monaco Editor, see Monaco Editor Accessibility Guide for detailed info. Notice, that you also are able to edit FlexBalancer description, Fallback and TTL.

Let's create our first Custom FlexBalancer with the simpliest logic: we will check uptime of jsDelivr CDN and if it is bigger that 98,5 - return answer 'google.com' with TTL 25, if not - return 'perfops.net' with default TTL (that you have set before).

/* 
 * If jsDelivr uptime is bigger than 98,5 - return 'google.com' with TTL 25
 * else return 'perfops.net' with default TTL 
 */
async function onRequest(req: IRequest, res: IResponse) {
    if(fetchCdnRumUptime('jsdelivr-cdn') > 98.5) { // if jsDelivr uptime is high
        res.setAddr('google.com'); // we set answer address to 'google.com' 
        res.setTTL(25); // with TTL = 25

        return res; // return answer
    }
    // if jsDelivr uptime is lower than expected
    res.setAddr('perfops.net'); // we set answer address to 'perfops.net'
    return res; // return answer
}

The text editor checks your code syntax for errors, shows error details on mouse hover and won't allow you to publish code with errors:

Alt text

It also 'knows' all definitions of our Custom Answers API (functions, type values for particular user) and provides helpful hints (use Ctrl+Space) and auto-completion (use Tab):

Alt text Alt text

Now all changes are done and no errors remain, you simply press Test and Publish button:

Alt text

Confirm the deployment:

Alt text

The next page has important information, do not forget to copy/save CNAME record for further placement into domain settings for your domain, then press Done:

Alt text

Now you can see your newly created FlexBalancer at FlexBalancers page:

Alt text

Let's check if your Custom Answer works. After some time, when you already have made all nessessary CNAME changes - check CDN RUM Uptime at CDNPerf. You can see that RUM Uptime is higher than 98,5.

Alt text

So, according to rules that you have defined at Custom Answer, it should return google.com as response address.

Alt text

And it does! That means your FlexBalancer works perfectly!

You may want to take a look at our Quick Tutorial and at more complicated script samples stored at our repository.

Edit FlexBalancer with custom answer

To change your custom answer, proceed to FlexBalancers page and press square button with pen at your Balancer:

Alt text

You can edit your code, FlexBalancer description, Fallback and TTL:

Alt text

You are also able to turn your FlexBalancer off and even to Delete it:

Alt text

After editing, press Test and Publish button, then Deploy now:

Alt text

Do not forget to copy/save FlexBalancer name for placement into CNAME record of domain settings for your domain, then press Done:

Alt text

Delete FlexBalancer

Proceed to FlexBalancers page and press red square button with trashcan picture on it:

Alt text

Confirm the deletion. Keep in mind that it may break your service:

Alt text

Clone this wiki locally