Comparison

net/server_epoll.lua @ 10492:8e1d9bba9244

net.server_epoll: Use monotonic time for scheduling Timer API of passing wallclock time remains
author Kim Alvefur <zash@zash.se>
date Sun, 08 Dec 2019 14:26:32 +0100
parent 10491:6f7a77aff9d5
child 10546:944863f878b9
comparison
equal deleted inserted replaced
10491:6f7a77aff9d5 10492:8e1d9bba9244
15 local pairs = pairs; 15 local pairs = pairs;
16 local logger = require "util.logger"; 16 local logger = require "util.logger";
17 local log = logger.init("server_epoll"); 17 local log = logger.init("server_epoll");
18 local socket = require "socket"; 18 local socket = require "socket";
19 local luasec = require "ssl"; 19 local luasec = require "ssl";
20 local gettime = require "util.time".now; 20 local realtime = require "util.time".now;
21 local monotonic = require "util.time".monotonic;
21 local indexedbheap = require "util.indexedbheap"; 22 local indexedbheap = require "util.indexedbheap";
22 local createtable = require "util.table".create; 23 local createtable = require "util.table".create;
23 local inet = require "util.net"; 24 local inet = require "util.net";
24 local inet_pton = inet.pton; 25 local inet_pton = inet.pton;
25 local _SOCKETINVALID = socket._SOCKETINVALID or -1; 26 local _SOCKETINVALID = socket._SOCKETINVALID or -1;
84 t[2] = noop; 85 t[2] = noop;
85 timers:remove(t.id); 86 timers:remove(t.id);
86 end 87 end
87 88
88 local function reschedule(t, time) 89 local function reschedule(t, time)
89 time = gettime() + time; 90 time = monotonic() + time;
90 t[1] = time; 91 t[1] = time;
91 timers:reprioritize(t.id, time); 92 timers:reprioritize(t.id, time);
92 end 93 end
93 94
94 -- Add relative timer 95 -- Add relative timer
95 local function addtimer(timeout, f) 96 local function addtimer(timeout, f)
96 local time = gettime() + timeout; 97 local time = monotonic() + timeout;
97 local timer = { time, f, close = closetimer, reschedule = reschedule, id = nil }; 98 local timer = { time, f, close = closetimer, reschedule = reschedule, id = nil };
98 timer.id = timers:insert(timer, time); 99 timer.id = timers:insert(timer, time);
99 return timer; 100 return timer;
100 end 101 end
101 102
102 -- Run callbacks of expired timers 103 -- Run callbacks of expired timers
103 -- Return time until next timeout 104 -- Return time until next timeout
104 local function runtimers(next_delay, min_wait) 105 local function runtimers(next_delay, min_wait)
105 -- Any timers at all? 106 -- Any timers at all?
106 local now = gettime(); 107 local elapsed = monotonic();
108 local now = realtime();
107 local peek = timers:peek(); 109 local peek = timers:peek();
108 while peek do 110 while peek do
109 111
110 if peek > now then 112 if peek > elapsed then
111 next_delay = peek - now; 113 next_delay = peek - elapsed;
112 break; 114 break;
113 end 115 end
114 116
115 local _, timer, id = timers:pop(); 117 local _, timer, id = timers:pop();
116 local ok, ret = pcall(timer[2], now); 118 local ok, ret = pcall(timer[2], now);
117 if ok and type(ret) == "number" then 119 if ok and type(ret) == "number" then
118 local next_time = now+ret; 120 local next_time = elapsed+ret;
119 timer[1] = next_time; 121 timer[1] = next_time;
120 timers:insert(timer, next_time); 122 timers:insert(timer, next_time);
121 end 123 end
122 124
123 peek = timers:peek(); 125 peek = timers:peek();
576 client:settimeout(0); 578 client:settimeout(0);
577 local conn_id = ("conn%s"):format(new_id()); 579 local conn_id = ("conn%s"):format(new_id());
578 local conn = setmetatable({ 580 local conn = setmetatable({
579 conn = client; 581 conn = client;
580 _server = server; 582 _server = server;
581 created = gettime(); 583 created = realtime();
582 listeners = listeners; 584 listeners = listeners;
583 read_size = read_size or (server and server.read_size); 585 read_size = read_size or (server and server.read_size);
584 writebuffer = {}; 586 writebuffer = {};
585 tls_ctx = tls_ctx or (server and server.tls_ctx); 587 tls_ctx = tls_ctx or (server and server.tls_ctx);
586 tls_direct = server and server.tls_direct; 588 tls_direct = server and server.tls_direct;
706 local conn, err = socket.bind(addr, port, cfg.tcp_backlog); 708 local conn, err = socket.bind(addr, port, cfg.tcp_backlog);
707 if not conn then return conn, err; end 709 if not conn then return conn, err; end
708 conn:settimeout(0); 710 conn:settimeout(0);
709 local server = setmetatable({ 711 local server = setmetatable({
710 conn = conn; 712 conn = conn;
711 created = gettime(); 713 created = realtime();
712 listeners = listeners; 714 listeners = listeners;
713 read_size = config and config.read_size; 715 read_size = config and config.read_size;
714 onreadable = interface.onacceptable; 716 onreadable = interface.onacceptable;
715 tls_ctx = config and config.tls_ctx; 717 tls_ctx = config and config.tls_ctx;
716 tls_direct = config and config.tls_direct; 718 tls_direct = config and config.tls_direct;