Comparison

net/server_epoll.lua @ 11696:917eca7be82b

net.server_epoll: Optionally let go of buffers Reusing an already existing buffer table would reduce garbage, but keeping it while idle is a waste.
author Kim Alvefur <zash@zash.se>
date Fri, 16 Jul 2021 02:28:32 +0200
parent 11695:1573099157c9
child 11697:6c3d8aa3bbd9
comparison
equal deleted inserted replaced
11695:1573099157c9 11696:917eca7be82b
84 -- TCP Keepalives 84 -- TCP Keepalives
85 tcp_keepalive = false; -- boolean | number 85 tcp_keepalive = false; -- boolean | number
86 86
87 -- Whether to let the Nagle algorithm stay enabled 87 -- Whether to let the Nagle algorithm stay enabled
88 nagle = true; 88 nagle = true;
89
90 -- Reuse write buffer tables
91 keep_buffers = true;
89 }}; 92 }};
90 local cfg = default_config.__index; 93 local cfg = default_config.__index;
91 94
92 local fds = createtable(10, 0); -- FD -> conn 95 local fds = createtable(10, 0); -- FD -> conn
93 96
470 local buffer = self.writebuffer; 473 local buffer = self.writebuffer;
471 local data = #buffer == 1 and buffer[1] or t_concat(buffer); 474 local data = #buffer == 1 and buffer[1] or t_concat(buffer);
472 local ok, err, partial = self.conn:send(data); 475 local ok, err, partial = self.conn:send(data);
473 if ok then 476 if ok then
474 self:set(nil, false); 477 self:set(nil, false);
475 for i = #buffer, 1, -1 do 478 if cfg.keep_buffers then
476 buffer[i] = nil; 479 for i = #buffer, 1, -1 do
480 buffer[i] = nil;
481 end
482 else
483 self.writebuffer = nil;
477 end 484 end
478 self:setwritetimeout(false); 485 self:setwritetimeout(false);
479 self:ondrain(); -- Be aware of writes in ondrain 486 self:ondrain(); -- Be aware of writes in ondrain
480 return ok; 487 return ok;
481 elseif partial then 488 elseif partial then
482 self:debug("Sent %d out of %d buffered bytes", partial, #data); 489 self:debug("Sent %d out of %d buffered bytes", partial, #data);
483 buffer[1] = data:sub(partial+1); 490 if cfg.keep_buffers then
484 for i = #buffer, 2, -1 do 491 buffer[1] = data:sub(partial+1);
485 buffer[i] = nil; 492 for i = #buffer, 2, -1 do
493 buffer[i] = nil;
494 end
495 else
496 data.writebuffer = { data:sub(partial+1) };
486 end 497 end
487 self:set(nil, true); 498 self:set(nil, true);
488 self:setwritetimeout(); 499 self:setwritetimeout();
489 end 500 end
490 if err == "wantwrite" or err == "timeout" then 501 if err == "wantwrite" or err == "timeout" then
507 function interface:write(data) 518 function interface:write(data)
508 local buffer = self.writebuffer; 519 local buffer = self.writebuffer;
509 if buffer then 520 if buffer then
510 t_insert(buffer, data); 521 t_insert(buffer, data);
511 else 522 else
523 self:noise("Allocating buffer!")
512 self.writebuffer = { data }; 524 self.writebuffer = { data };
513 end 525 end
514 if not self._write_lock then 526 if not self._write_lock then
515 if cfg.opportunistic_writes and not self._opportunistic_write then 527 if cfg.opportunistic_writes and not self._opportunistic_write then
516 self._opportunistic_write = true; 528 self._opportunistic_write = true;