Advertisement
Guest User

Untitled

a guest
Sep 27th, 2020
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.88 KB | None | 0 0
  1. local event = require("event")
  2.  
  3. local PORT = 80
  4. local SEND_ATTEMPTS = 5
  5. local BACKOFF_BASE = 1 --seconds
  6.  
  7. local gdp = {}
  8.  
  9. --init
  10. math.randomseed(os.time())
  11. --end-init
  12.  
  13.  
  14. local function set_timer(callback, max_wait_time, elapsed_attempts)
  15.     local function on_timer()
  16.         if elapsed_attempts > SEND_ATTEMPTS then --TODO: Check off-by-1 error
  17.             io.write("ERROR: TODO")
  18.             return
  19.         end
  20.         callback()
  21.         set_timer(callback, max_wait_time * 2, elapsed_attempts + 1)
  22.     end
  23.     io.write(elapsed_attempts, ":", event.timer(math.random() * max_wait_time, on_timer, 1), "\n")
  24. end
  25.  
  26. local function backoff(callback)
  27.     set_timer(callback, BACKOFF_BASE, 1)
  28. end
  29.  
  30. function gdp.send(to, ...)
  31.     local args = table.pack(...)
  32.     local send_actual = function()
  33.         io.write("id:", args[1], " ")
  34.     end
  35.     backoff(send_actual)
  36. end
  37.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement