Comparison

net/server_epoll.lua @ 10061:5c71693c8345

Merge 0.11->trunk
author Kim Alvefur <zash@zash.se>
date Mon, 08 Jul 2019 02:44:32 +0200
parent 9995:8d46ecc4bd0c
parent 10059:c8c3f2eba898
child 10080:39ca4202ae1e
comparison
equal deleted inserted replaced
10060:7a36b7ac309b 10061:5c71693c8345
36 local default_config = { __index = { 36 local default_config = { __index = {
37 -- If a connection is silent for this long, close it unless onreadtimeout says not to 37 -- If a connection is silent for this long, close it unless onreadtimeout says not to
38 read_timeout = 14 * 60; 38 read_timeout = 14 * 60;
39 39
40 -- How long to wait for a socket to become writable after queuing data to send 40 -- How long to wait for a socket to become writable after queuing data to send
41 send_timeout = 60; 41 send_timeout = 180;
42
43 -- How long to wait for a socket to become writable after creation
44 connect_timeout = 20;
42 45
43 -- Some number possibly influencing how many pending connections can be accepted 46 -- Some number possibly influencing how many pending connections can be accepted
44 tcp_backlog = 128; 47 tcp_backlog = 128;
45 48
46 -- If accepting a new incoming connection fails, wait this long before trying again 49 -- If accepting a new incoming connection fails, wait this long before trying again
100 while peek do 103 while peek do
101 104
102 if peek > now then 105 if peek > now then
103 next_delay = peek - now; 106 next_delay = peek - now;
104 break; 107 break;
105 end 108 end
106 109
107 local _, timer, id = timers:pop(); 110 local _, timer, id = timers:pop();
108 local ok, ret = pcall(timer[2], now); 111 local ok, ret = pcall(timer[2], now);
109 if ok and type(ret) == "number" then 112 if ok and type(ret) == "number" then
110 local next_time = now+ret; 113 local next_time = now+ret;
111 timer[1] = next_time; 114 timer[1] = next_time;
112 timers:insert(timer, next_time); 115 timers:insert(timer, next_time);
113 end 116 end
114 117
115 peek = timers:peek(); 118 peek = timers:peek();
116 end 119 end
117 if peek == nil then 120 if peek == nil then
118 return next_delay; 121 return next_delay;
119 end 122 end
120 123
121 if next_delay < min_wait then 124 if next_delay < min_wait then
157 return; 160 return;
158 end 161 end
159 local ok, err = pcall(listener, self, ...); 162 local ok, err = pcall(listener, self, ...);
160 if not ok then 163 if not ok then
161 log("error", "Error calling on%s: %s", what, err); 164 log("error", "Error calling on%s: %s", what, err);
165 return;
162 end 166 end
163 return err; 167 return err;
164 end 168 end
165 169
166 -- Return the file descriptor number 170 -- Return the file descriptor number
405 if buffer then 409 if buffer then
406 t_insert(buffer, data); 410 t_insert(buffer, data);
407 else 411 else
408 self.writebuffer = { data }; 412 self.writebuffer = { data };
409 end 413 end
410 self:setwritetimeout(); 414 if not self._write_lock then
411 self:set(nil, true); 415 self:setwritetimeout();
416 self:set(nil, true);
417 end
412 return #data; 418 return #data;
413 end 419 end
414 interface.send = interface.write; 420 interface.send = interface.write;
415 421
416 -- Close, possibly after writing is done 422 -- Close, possibly after writing is done
481 self:destroy(); 487 self:destroy();
482 return conn, err; 488 return conn, err;
483 end 489 end
484 conn:settimeout(0); 490 conn:settimeout(0);
485 self.conn = conn; 491 self.conn = conn;
492 if conn.sni then
493 if self.servername then
494 conn:sni(self.servername);
495 elseif self._server and type(self._server.hosts) == "table" and next(self._server.hosts) ~= nil then
496 conn:sni(self._server.hosts, true);
497 end
498 end
486 self:on("starttls"); 499 self:on("starttls");
487 self.ondrain = nil; 500 self.ondrain = nil;
488 self.onwritable = interface.tlshandskake; 501 self.onwritable = interface.tlshandskake;
489 self.onreadable = interface.tlshandskake; 502 self.onreadable = interface.tlshandskake;
490 return self:init(); 503 return self:init();
553 local client = wrapsocket(conn, self, nil, self.listeners); 566 local client = wrapsocket(conn, self, nil, self.listeners);
554 log("debug", "New connection %s", tostring(client)); 567 log("debug", "New connection %s", tostring(client));
555 client:init(); 568 client:init();
556 if self.tls_direct then 569 if self.tls_direct then
557 client:starttls(self.tls_ctx); 570 client:starttls(self.tls_ctx);
571 else
572 client:onconnect();
558 end 573 end
559 end 574 end
560 575
561 -- Initialization 576 -- Initialization
562 function interface:init() 577 function interface:init()
563 self:setwritetimeout(); 578 self:setwritetimeout(cfg.connect_timeout);
564 return self:add(true, true); 579 return self:add(true, true);
565 end 580 end
566 581
567 function interface:pause() 582 function interface:pause()
568 return self:set(false); 583 return self:set(false);
586 self:onreadable(); 601 self:onreadable();
587 end 602 end
588 end); 603 end);
589 end 604 end
590 605
606 function interface:pause_writes()
607 self._write_lock = true;
608 self:setwritetimeout(false);
609 self:set(nil, false);
610 end
611
612 function interface:resume_writes()
613 self._write_lock = nil;
614 if self.writebuffer[1] then
615 self:setwritetimeout();
616 self:set(nil, true);
617 end
618 end
619
591 -- Connected! 620 -- Connected!
592 function interface:onconnect() 621 function interface:onconnect()
593 if self.conn and not self.peername and self.conn.getpeername then 622 self:updatenames();
594 self.peername, self.peerport = self.conn:getpeername();
595 end
596 self.onconnect = noop; 623 self.onconnect = noop;
597 self:on("connect"); 624 self:on("connect");
598 end 625 end
599 626
600 local function addserver(addr, port, listeners, read_size, tls_ctx) 627 local function listen(addr, port, listeners, config)
601 local conn, err = socket.bind(addr, port, cfg.tcp_backlog); 628 local conn, err = socket.bind(addr, port, cfg.tcp_backlog);
602 if not conn then return conn, err; end 629 if not conn then return conn, err; end
603 conn:settimeout(0); 630 conn:settimeout(0);
604 local server = setmetatable({ 631 local server = setmetatable({
605 conn = conn; 632 conn = conn;
606 created = gettime(); 633 created = gettime();
607 listeners = listeners; 634 listeners = listeners;
608 read_size = read_size; 635 read_size = config and config.read_size;
609 onreadable = interface.onacceptable; 636 onreadable = interface.onacceptable;
610 tls_ctx = tls_ctx; 637 tls_ctx = config and config.tls_ctx;
611 tls_direct = tls_ctx and true or false; 638 tls_direct = config and config.tls_direct;
639 hosts = config and config.sni_hosts;
612 sockname = addr; 640 sockname = addr;
613 sockport = port; 641 sockport = port;
614 }, interface_mt); 642 }, interface_mt);
615 server:add(true, false); 643 server:add(true, false);
616 return server; 644 return server;
645 end
646
647 -- COMPAT
648 local function addserver(addr, port, listeners, read_size, tls_ctx)
649 return listen(addr, port, listeners, {
650 read_size = read_size;
651 tls_ctx = tls_ctx;
652 tls_direct = tls_ctx and true or false;
653 });
617 end 654 end
618 655
619 -- COMPAT 656 -- COMPAT
620 local function wrapclient(conn, addr, port, listeners, read_size, tls_ctx) 657 local function wrapclient(conn, addr, port, listeners, read_size, tls_ctx)
621 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx); 658 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx);
750 return { 787 return {
751 get_backend = function () return "epoll"; end; 788 get_backend = function () return "epoll"; end;
752 addserver = addserver; 789 addserver = addserver;
753 addclient = addclient; 790 addclient = addclient;
754 add_task = addtimer; 791 add_task = addtimer;
792 listen = listen;
755 at = at; 793 at = at;
756 loop = loop; 794 loop = loop;
757 closeall = closeall; 795 closeall = closeall;
758 setquitting = setquitting; 796 setquitting = setquitting;
759 wrapclient = wrapclient; 797 wrapclient = wrapclient;