VCL: Fastly integer Math

Comments

9 comments

  • Sean Leach

    Hello! We are working on this right now. Give us a a month or so.

    0
    Comment actions Permalink
  • techreb

    Thank you Sean. Meanwhile, i am using req.grace to do math on it and then restore the original value of grace. Only problem is that initial value i see is "inf", which cannot be restored when there is no specific grace value set.

    -Michael

    0
    Comment actions Permalink
  • Sean Leach

    Hey all - sorry for the delay. It is ready to deploy, just finishing up the documentation and some last minute testing (code is done) - so should be within a few weeks. Will post here when it's live with a link to the docs.

    0
    Comment actions Permalink
  • Håkon Erichsen

    We need this as well, glad to see it's around the corner! :slight_smile:

    0
    Comment actions Permalink
  • Alex G

    Are there any updates here?

    0
    Comment actions Permalink
  • techreb

    Thank you. Its very handy now to use these new variables.

    0
    Comment actions Permalink
  • Alex G

    Is it now possible to do additions and subtractions with integer local variables?

    0
    Comment actions Permalink
  • joshenders

    You can (ab)use req.grace to do simple arithmetic:

    ```

    preserve req.grace

    set req.http.VCL-grace = req.grace;

    addition

    set req.grace = 400s; set req.grace += 200s; set req.http.VCL-sum = req.grace; set req.http.VCL-sum = regsub(req.http.VCL-sum, "..*", "");

    subtraction

    set req.grace = 400s; set req.grace -= 200s; set req.http.VCL-difference = req.grace; set req.http.VCL-difference = regsub(req.http.VCL-difference, "..*", "");

    multiplication

    set req.grace = 400s; set req.grace = 200; set req.http.VCL-product = req.grace; set req.http.VCL-product = regsub(req.http.VCL-product, "..", "");

    division

    set req.grace = 400s; set req.grace /= 200; set req.http.VCL-quotient = req.grace; set req.http.VCL-quotient = regsub(req.http.VCL-quotient, "..*", "");

    dynamic input

    set req.http.X-Some-Input = "500"; set req.grace = std.atoi(req.http.X-Some-Input); set req.grace = 2; set req.http.VCL-product = req.grace; set req.http.VCL-product = regsub(req.http.VCL-product, "..", "");

    time math

    set req.grace = std.atoi(strftime({"%s"}, now)); set req.grace -= std.atoi(obj.entered); set http.X-Object-Entered-Cache-Unix = req.grace; set resp.http.X-Object-Entered-Cache-Unix = regsub(http.X-Object-Entered-Cache-Unix, "..*", "");

    reset grace to its original value

    set req.grace = std.atoi(req.http.VCL-grace); ```

    If you need modulus you can use this formula: dividend-((dividend/divisor)*divisor)

    0
    Comment actions Permalink
  • Leon Brocard

    We recently added local variables to VCL, including numeric and time types, which make these kinds of calculations easy:

    https://docs.fastly.com/guides/vcl/local-variables-in-vcl

    Feel free to reach out if you have any questions.

    0
    Comment actions Permalink

Please sign in to leave a comment.