Software /
code /
prosody
Annotate
net/server_epoll.lua @ 10372:5482f21a18a2
core.sessionmanager: Fix traceback from passing nil to resourceprep
author | Kim Alvefur <zash@zash.se> |
---|---|
date | Sat, 02 Nov 2019 13:24:33 +0100 |
parent | 10332:e05ea8223b32 |
child | 10416:6af8c812a885 |
rev | line source |
---|---|
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
1 -- Prosody IM |
9474
619ba78709a5
net.server_epoll: Update copyright year
Kim Alvefur <zash@zash.se>
parents:
9473
diff
changeset
|
2 -- Copyright (C) 2016-2018 Kim Alvefur |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
3 -- |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
4 -- This project is MIT/X11 licensed. Please see the |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
5 -- COPYING file in the source package for more information. |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
6 -- |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
7 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
8 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
9 local t_insert = table.insert; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
10 local t_concat = table.concat; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
11 local setmetatable = setmetatable; |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
12 local pcall = pcall; |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
13 local type = type; |
7797
d0c32d86724f
server_epoll: Flush and close all connections before quitting mainloop so no data is lost in buffers
Kim Alvefur <zash@zash.se>
parents:
7796
diff
changeset
|
14 local next = next; |
7794
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
15 local pairs = pairs; |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
16 local logger = require "util.logger"; |
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
17 local log = logger.init("server_epoll"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
18 local socket = require "socket"; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
19 local luasec = require "ssl"; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
20 local gettime = require "util.time".now; |
10059
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
21 local indexedbheap = require "util.indexedbheap"; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
22 local createtable = require "util.table".create; |
9473
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
23 local inet = require "util.net"; |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
24 local inet_pton = inet.pton; |
7737
f93b3083b46b
net.server_epoll: Use _SOCKETINVALID from LuaSocket
Kim Alvefur <zash@zash.se>
parents:
7707
diff
changeset
|
25 local _SOCKETINVALID = socket._SOCKETINVALID or -1; |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
26 local new_id = require "util.id".medium; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
27 |
9506
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
28 local poller = require "util.poll" |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
29 local EEXIST = poller.EEXIST; |
9507
33d21f020b66
net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents:
9506
diff
changeset
|
30 local ENOENT = poller.ENOENT; |
9506
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
31 |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
32 local poll = assert(poller.new()); |
7601
ce20546d7714
net.server_epoll: Make sure a recent LuaSocket is avaliable
Kim Alvefur <zash@zash.se>
parents:
7600
diff
changeset
|
33 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
34 local _ENV = nil; |
8555
4f0f5b49bb03
vairious: Add annotation when an empty environment is set [luacheck]
Kim Alvefur <zash@zash.se>
parents:
8552
diff
changeset
|
35 -- luacheck: std none |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
36 |
7657
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
37 local default_config = { __index = { |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
38 -- If a connection is silent for this long, close it unless onreadtimeout says not to |
8983
23f66f04e216
net.server: Set default read timeout to 14 minutes (fixes #971)
Kim Alvefur <zash@zash.se>
parents:
8781
diff
changeset
|
39 read_timeout = 14 * 60; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
40 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
41 -- How long to wait for a socket to become writable after queuing data to send |
9825
5ccce16c6449
net.server_epoll: Increase send_timeout to 3 minutes (to match server_event)
Kim Alvefur <zash@zash.se>
parents:
9824
diff
changeset
|
42 send_timeout = 180; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
43 |
9824
729de68f0068
net.server_epoll: Separate timeout for initial connection attempts
Kim Alvefur <zash@zash.se>
parents:
9823
diff
changeset
|
44 -- How long to wait for a socket to become writable after creation |
729de68f0068
net.server_epoll: Separate timeout for initial connection attempts
Kim Alvefur <zash@zash.se>
parents:
9823
diff
changeset
|
45 connect_timeout = 20; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
46 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
47 -- Some number possibly influencing how many pending connections can be accepted |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
48 tcp_backlog = 128; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
49 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
50 -- If accepting a new incoming connection fails, wait this long before trying again |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
51 accept_retry_interval = 10; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
52 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
53 -- If there is still more data to read from LuaSocktes buffer, wait this long and read again |
7560
785fa1138293
net.server_epoll: Continue reading after a slight delay if there is buffered data after a read
Kim Alvefur <zash@zash.se>
parents:
7559
diff
changeset
|
54 read_retry_delay = 1e-06; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
55 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
56 -- Size of chunks to read from sockets |
9311
9b0604fe01f1
net.server_epoll: Make read_size a server-wide option
Kim Alvefur <zash@zash.se>
parents:
9310
diff
changeset
|
57 read_size = 8192; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
58 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
59 -- Timeout used during between steps in TLS handshakes |
9822
13c2707d2417
net.server_epoll: Rename handshake_timeout to ssl_handshake_timeout (fixes #1319)
Kim Alvefur <zash@zash.se>
parents:
9821
diff
changeset
|
60 ssl_handshake_timeout = 60; |
9665
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
61 |
4790d1b88919
server_epoll: Add comments describing config options
Kim Alvefur <zash@zash.se>
parents:
9664
diff
changeset
|
62 -- Maximum and minimum amount of time to sleep waiting for events (adjusted for pending timers) |
7623
a1c040a5754f
net.server_epoll: Make maximum wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7606
diff
changeset
|
63 max_wait = 86400; |
7658
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
64 min_wait = 1e-06; |
10081
72adf1f39404
net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents:
10080
diff
changeset
|
65 |
72adf1f39404
net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents:
10080
diff
changeset
|
66 -- EXPERIMENTAL |
72adf1f39404
net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents:
10080
diff
changeset
|
67 -- Whether to kill connections in case of callback errors. |
72adf1f39404
net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents:
10080
diff
changeset
|
68 fatal_errors = false; |
10228
e77bf4222fae
net.server_epoll: Add support for opportunistic writes
Kim Alvefur <zash@zash.se>
parents:
10098
diff
changeset
|
69 |
e77bf4222fae
net.server_epoll: Add support for opportunistic writes
Kim Alvefur <zash@zash.se>
parents:
10098
diff
changeset
|
70 -- Attempt writes instantly |
e77bf4222fae
net.server_epoll: Add support for opportunistic writes
Kim Alvefur <zash@zash.se>
parents:
10098
diff
changeset
|
71 opportunistic_writes = false; |
7657
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
72 }}; |
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
73 local cfg = default_config.__index; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
74 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
75 local fds = createtable(10, 0); -- FD -> conn |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
76 |
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
77 -- Timer and scheduling -- |
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
78 |
10059
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
79 local timers = indexedbheap.create(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
80 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
81 local function noop() end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
82 local function closetimer(t) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
83 t[1] = 0; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
84 t[2] = noop; |
10059
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
85 timers:remove(t.id); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
86 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
87 |
10059
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
88 local function reschedule(t, time) |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
89 t[1] = time; |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
90 timers:reprioritize(t.id, time); |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
91 end |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
92 |
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
93 -- Add absolute timer |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
94 local function at(time, f) |
10059
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
95 local timer = { time, f, close = closetimer, reschedule = reschedule, id = nil }; |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
96 timer.id = timers:insert(timer, time); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
97 return timer; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
98 end |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
99 |
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
100 -- Add relative timer |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
101 local function addtimer(timeout, f) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
102 return at(gettime() + timeout, f); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
103 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
104 |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
105 -- Run callbacks of expired timers |
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
106 -- Return time until next timeout |
7658
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
107 local function runtimers(next_delay, min_wait) |
7629
0fcde9869cc1
net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents:
7628
diff
changeset
|
108 -- Any timers at all? |
10059
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
109 local now = gettime(); |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
110 local peek = timers:peek(); |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
111 while peek do |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
112 |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
113 if peek > now then |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
114 next_delay = peek - now; |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
115 break; |
9855
6c2370f17027
net.server_epoll: Optimize timer handling
Kim Alvefur <zash@zash.se>
parents:
9853
diff
changeset
|
116 end |
10059
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
117 |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
118 local _, timer, id = timers:pop(); |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
119 local ok, ret = pcall(timer[2], now); |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
120 if ok and type(ret) == "number" then |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
121 local next_time = now+ret; |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
122 timer[1] = next_time; |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
123 timers:insert(timer, next_time); |
9855
6c2370f17027
net.server_epoll: Optimize timer handling
Kim Alvefur <zash@zash.se>
parents:
9853
diff
changeset
|
124 end |
10059
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
125 |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
126 peek = timers:peek(); |
9855
6c2370f17027
net.server_epoll: Optimize timer handling
Kim Alvefur <zash@zash.se>
parents:
9853
diff
changeset
|
127 end |
10059
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
128 if peek == nil then |
7629
0fcde9869cc1
net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents:
7628
diff
changeset
|
129 return next_delay; |
0fcde9869cc1
net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents:
7628
diff
changeset
|
130 end |
0fcde9869cc1
net.server_epoll: Return early in the event of zero timers
Kim Alvefur <zash@zash.se>
parents:
7628
diff
changeset
|
131 |
10059
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
132 if next_delay < min_wait then |
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
133 return min_wait; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
134 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
135 return next_delay; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
136 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
137 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
138 -- Socket handler interface |
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
139 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
140 local interface = {}; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
141 local interface_mt = { __index = interface }; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
142 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
143 function interface_mt:__tostring() |
7585
b64218c816de
net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents:
7584
diff
changeset
|
144 if self.sockname and self.peername then |
b64218c816de
net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents:
7584
diff
changeset
|
145 return ("FD %d (%s, %d, %s, %d)"):format(self:getfd(), self.peername, self.peerport, self.sockname, self.sockport); |
b64218c816de
net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents:
7584
diff
changeset
|
146 elseif self.sockname or self.peername then |
b64218c816de
net.server_epoll: Improve string representation of connections
Kim Alvefur <zash@zash.se>
parents:
7584
diff
changeset
|
147 return ("FD %d (%s, %d)"):format(self:getfd(), self.sockname or self.peername, self.sockport or self.peerport); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
148 end |
9321
146dc1e4c7ff
net.server_epoll: Simplify minimal string representation
Kim Alvefur <zash@zash.se>
parents:
9320
diff
changeset
|
149 return ("FD %d"):format(self:getfd()); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
150 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
151 |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
152 interface.log = log; |
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
153 function interface:debug(msg, ...) --luacheck: ignore 212/self |
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
154 self.log("debug", msg, ...); |
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
155 end |
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
156 |
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
157 function interface:error(msg, ...) --luacheck: ignore 212/self |
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
158 self.log("error", msg, ...); |
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
159 end |
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
160 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
161 -- Replace the listener and tell the old one |
9387
33e52f727f0f
net.connect: Fix passing request table to new listener
Kim Alvefur <zash@zash.se>
parents:
9385
diff
changeset
|
162 function interface:setlistener(listeners, data) |
7604
36408ef6c8c9
net.server_epoll: Call ondetach when listeners are replaced
Kim Alvefur <zash@zash.se>
parents:
7603
diff
changeset
|
163 self:on("detach"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
164 self.listeners = listeners; |
9387
33e52f727f0f
net.connect: Fix passing request table to new listener
Kim Alvefur <zash@zash.se>
parents:
9385
diff
changeset
|
165 self:on("attach", data); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
166 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
167 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
168 -- Call a listener callback |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
169 function interface:on(what, ...) |
8006
be374a5778f3
net.server_epoll: Log if there's a connection without listeners
Kim Alvefur <zash@zash.se>
parents:
7910
diff
changeset
|
170 if not self.listeners then |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
171 self:debug("Interface is missing listener callbacks"); |
8006
be374a5778f3
net.server_epoll: Log if there's a connection without listeners
Kim Alvefur <zash@zash.se>
parents:
7910
diff
changeset
|
172 return; |
be374a5778f3
net.server_epoll: Log if there's a connection without listeners
Kim Alvefur <zash@zash.se>
parents:
7910
diff
changeset
|
173 end |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
174 local listener = self.listeners["on"..what]; |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
175 if not listener then |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
176 -- self:debug("Missing listener 'on%s'", what); -- uncomment for development and debugging |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
177 return; |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
178 end |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
179 local ok, err = pcall(listener, self, ...); |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
180 if not ok then |
10081
72adf1f39404
net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents:
10080
diff
changeset
|
181 if cfg.fatal_errors then |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
182 self:debug("Closing due to error calling on%s: %s", what, err); |
10081
72adf1f39404
net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents:
10080
diff
changeset
|
183 self:destroy(); |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
184 else |
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
185 self:debug("Error calling on%s: %s", what, err); |
10081
72adf1f39404
net.server_epoll: Add experimental option to close connections in case of listener error
Kim Alvefur <zash@zash.se>
parents:
10080
diff
changeset
|
186 end |
10080
39ca4202ae1e
net.server_epoll: Return listener error message
Kim Alvefur <zash@zash.se>
parents:
10061
diff
changeset
|
187 return nil, err; |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
188 end |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
189 return err; |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
190 end |
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
191 |
10278
94c584d67533
net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents:
10234
diff
changeset
|
192 -- Allow this one to be overridden |
94c584d67533
net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents:
10234
diff
changeset
|
193 function interface:onincoming(...) |
94c584d67533
net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents:
10234
diff
changeset
|
194 return self:on("incoming", ...); |
94c584d67533
net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents:
10234
diff
changeset
|
195 end |
94c584d67533
net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents:
10234
diff
changeset
|
196 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
197 -- Return the file descriptor number |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
198 function interface:getfd() |
7586
846fdbbc62ba
net.server_epoll: Return something as FD when no connection exists
Kim Alvefur <zash@zash.se>
parents:
7585
diff
changeset
|
199 if self.conn then |
846fdbbc62ba
net.server_epoll: Return something as FD when no connection exists
Kim Alvefur <zash@zash.se>
parents:
7585
diff
changeset
|
200 return self.conn:getfd(); |
846fdbbc62ba
net.server_epoll: Return something as FD when no connection exists
Kim Alvefur <zash@zash.se>
parents:
7585
diff
changeset
|
201 end |
7737
f93b3083b46b
net.server_epoll: Use _SOCKETINVALID from LuaSocket
Kim Alvefur <zash@zash.se>
parents:
7707
diff
changeset
|
202 return _SOCKETINVALID; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
203 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
204 |
8271
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
205 function interface:server() |
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
206 return self._server or self; |
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
207 end |
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
208 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
209 -- Get IP address |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
210 function interface:ip() |
7584
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
211 return self.peername or self.sockname; |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
212 end |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
213 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
214 -- Get a port number, doesn't matter which |
7584
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
215 function interface:port() |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
216 return self.sockport or self.peerport; |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
217 end |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
218 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
219 -- Get local port number |
7584
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
220 function interface:clientport() |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
221 return self.sockport; |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
222 end |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
223 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
224 -- Get remote port |
7584
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
225 function interface:serverport() |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
226 if self.sockport then |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
227 return self.sockport; |
8271
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
228 elseif self._server then |
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
229 self._server:port(); |
7584
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
230 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
231 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
232 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
233 -- Return underlying socket |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
234 function interface:socket() |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
235 return self.conn; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
236 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
237 |
7910
91db637be237
net.server_epoll: Add method for changing read pattern
Kim Alvefur <zash@zash.se>
parents:
7817
diff
changeset
|
238 function interface:set_mode(new_mode) |
9310
2d2d4c293efa
net.server_epoll: Rename pattern to read_size
Kim Alvefur <zash@zash.se>
parents:
9309
diff
changeset
|
239 self.read_size = new_mode; |
7910
91db637be237
net.server_epoll: Add method for changing read pattern
Kim Alvefur <zash@zash.se>
parents:
7817
diff
changeset
|
240 end |
91db637be237
net.server_epoll: Add method for changing read pattern
Kim Alvefur <zash@zash.se>
parents:
7817
diff
changeset
|
241 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
242 function interface:setoption(k, v) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
243 -- LuaSec doesn't expose setoption :( |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
244 if self.conn.setoption then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
245 self.conn:setoption(k, v); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
246 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
247 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
248 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
249 -- Timeout for detecting dead or idle sockets |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
250 function interface:setreadtimeout(t) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
251 if t == false then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
252 if self._readtimeout then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
253 self._readtimeout:close(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
254 self._readtimeout = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
255 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
256 return |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
257 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
258 t = t or cfg.read_timeout; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
259 if self._readtimeout then |
10059
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
260 self._readtimeout:reschedule(gettime() + t); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
261 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
262 self._readtimeout = addtimer(t, function () |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
263 if self:on("readtimeout") then |
10282
05ba04725a15
net.server_epoll: Add some timeout related logging
Kim Alvefur <zash@zash.se>
parents:
10280
diff
changeset
|
264 self:debug("Read timeout, handled"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
265 return cfg.read_timeout; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
266 else |
10282
05ba04725a15
net.server_epoll: Add some timeout related logging
Kim Alvefur <zash@zash.se>
parents:
10280
diff
changeset
|
267 self:debug("Read timeout, fatal"); |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
268 self:on("disconnect", "read timeout"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
269 self:destroy(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
270 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
271 end); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
272 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
273 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
274 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
275 -- Timeout for detecting dead sockets |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
276 function interface:setwritetimeout(t) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
277 if t == false then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
278 if self._writetimeout then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
279 self._writetimeout:close(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
280 self._writetimeout = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
281 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
282 return |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
283 end |
9821
7712488c8e49
net.server_epoll: Use send_timeout for write timout like other implementations (fixes #1316)
Kim Alvefur <zash@zash.se>
parents:
9665
diff
changeset
|
284 t = t or cfg.send_timeout; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
285 if self._writetimeout then |
10059
c8c3f2eba898
net.server_epoll: Backport timer optimization 6c2370f17027 from trunk (see #1388)
Kim Alvefur <zash@zash.se>
parents:
9994
diff
changeset
|
286 self._writetimeout:reschedule(gettime() + t); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
287 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
288 self._writetimeout = addtimer(t, function () |
10282
05ba04725a15
net.server_epoll: Add some timeout related logging
Kim Alvefur <zash@zash.se>
parents:
10280
diff
changeset
|
289 self:debug("Write timeout"); |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
290 self:on("disconnect", "write timeout"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
291 self:destroy(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
292 end); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
293 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
294 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
295 |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
296 function interface:add(r, w) |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
297 local fd = self:getfd(); |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
298 if fd < 0 then |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
299 return nil, "invalid fd"; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
300 end |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
301 if r == nil then r = self._wantread; end |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
302 if w == nil then w = self._wantwrite; end |
9384
5c8006ce8cb8
net.server_epoll: Include errno in log messages
Kim Alvefur <zash@zash.se>
parents:
9383
diff
changeset
|
303 local ok, err, errno = poll:add(fd, r, w); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
304 if not ok then |
9506
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
305 if errno == EEXIST then |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
306 self:debug("FD already registered in poller! (EEXIST)"); |
9506
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
307 return self:set(r, w); -- So try to change its flags |
ae6636052be9
net.server_epoll: Graceful handling of registering already added socket
Kim Alvefur <zash@zash.se>
parents:
9499
diff
changeset
|
308 end |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
309 self:debug("Could not register in poller: %s(%d)", err, errno); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
310 return ok, err; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
311 end |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
312 self._wantread, self._wantwrite = r, w; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
313 fds[fd] = self; |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
314 self:debug("Registered in poller"); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
315 return true; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
316 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
317 |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
318 function interface:set(r, w) |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
319 local fd = self:getfd(); |
7602
a4d5eec36a17
net.server_epoll: Prevent invalid FDs from being added to epoll
Kim Alvefur <zash@zash.se>
parents:
7601
diff
changeset
|
320 if fd < 0 then |
a4d5eec36a17
net.server_epoll: Prevent invalid FDs from being added to epoll
Kim Alvefur <zash@zash.se>
parents:
7601
diff
changeset
|
321 return nil, "invalid fd"; |
a4d5eec36a17
net.server_epoll: Prevent invalid FDs from being added to epoll
Kim Alvefur <zash@zash.se>
parents:
7601
diff
changeset
|
322 end |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
323 if r == nil then r = self._wantread; end |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
324 if w == nil then w = self._wantwrite; end |
9384
5c8006ce8cb8
net.server_epoll: Include errno in log messages
Kim Alvefur <zash@zash.se>
parents:
9383
diff
changeset
|
325 local ok, err, errno = poll:set(fd, r, w); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
326 if not ok then |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
327 self:debug("Could not update poller state: %s(%d)", err, errno); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
328 return ok, err; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
329 end |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
330 self._wantread, self._wantwrite = r, w; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
331 return true; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
332 end |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
333 |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
334 function interface:del() |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
335 local fd = self:getfd(); |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
336 if fd < 0 then |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
337 return nil, "invalid fd"; |
7625
6afafd6d4ef0
net.server_epoll: Move management of fd registry to :setflags
Kim Alvefur <zash@zash.se>
parents:
7624
diff
changeset
|
338 end |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
339 if fds[fd] ~= self then |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
340 return nil, "unregistered fd"; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
341 end |
9384
5c8006ce8cb8
net.server_epoll: Include errno in log messages
Kim Alvefur <zash@zash.se>
parents:
9383
diff
changeset
|
342 local ok, err, errno = poll:del(fd); |
9507
33d21f020b66
net.server_epoll: Ignore ENOENT when deregitering socket
Kim Alvefur <zash@zash.se>
parents:
9506
diff
changeset
|
343 if not ok and errno ~= ENOENT then |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
344 self:debug("Could not unregister: %s(%d)", err, errno); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
345 return ok, err; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
346 end |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
347 self._wantread, self._wantwrite = nil, nil; |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
348 fds[fd] = nil; |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
349 self:debug("Unregistered from poller"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
350 return true; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
351 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
352 |
9320
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
353 function interface:setflags(r, w) |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
354 if not(self._wantread or self._wantwrite) then |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
355 if not(r or w) then |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
356 return true; -- no change |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
357 end |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
358 return self:add(r, w); |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
359 end |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
360 if not(r or w) then |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
361 return self:del(); |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
362 end |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
363 return self:set(r, w); |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
364 end |
8cc498cb0df7
net.server_epoll: Add back setflags API for compat
Kim Alvefur <zash@zash.se>
parents:
9319
diff
changeset
|
365 |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
366 -- Called when socket is readable |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
367 function interface:onreadable() |
9311
9b0604fe01f1
net.server_epoll: Make read_size a server-wide option
Kim Alvefur <zash@zash.se>
parents:
9310
diff
changeset
|
368 local data, err, partial = self.conn:receive(self.read_size or cfg.read_size); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
369 if data then |
8540
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
370 self:onconnect(); |
10278
94c584d67533
net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents:
10234
diff
changeset
|
371 self:onincoming(data); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
372 else |
9931
485231f8b75d
net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents:
9822
diff
changeset
|
373 if err == "wantread" then |
485231f8b75d
net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents:
9822
diff
changeset
|
374 self:set(true, nil); |
485231f8b75d
net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents:
9822
diff
changeset
|
375 err = "timeout"; |
485231f8b75d
net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents:
9822
diff
changeset
|
376 elseif err == "wantwrite" then |
485231f8b75d
net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents:
9822
diff
changeset
|
377 self:set(nil, true); |
485231f8b75d
net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents:
9822
diff
changeset
|
378 err = "timeout"; |
485231f8b75d
net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents:
9822
diff
changeset
|
379 end |
8540
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
380 if partial and partial ~= "" then |
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
381 self:onconnect(); |
10278
94c584d67533
net.server_epoll: Make it easy to override handler for incoming data
Kim Alvefur <zash@zash.se>
parents:
10234
diff
changeset
|
382 self:onincoming(partial, err); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
383 end |
9931
485231f8b75d
net.server_epoll: Handle LuaSec wantread/wantwrite conditions before callbacks (fixes #1333)
Kim Alvefur <zash@zash.se>
parents:
9822
diff
changeset
|
384 if err ~= "timeout" then |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
385 self:on("disconnect", err); |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
386 self:destroy() |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
387 return; |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
388 end |
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
389 end |
7691
a5ad858f3a93
net.server_epoll: Check that connection still exists after callback, fixes traceback when closed
Kim Alvefur <zash@zash.se>
parents:
7687
diff
changeset
|
390 if not self.conn then return; end |
10098
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
391 if self._limit and (data or partial) then |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
392 local cost = self._limit * #(data or partial); |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
393 if cost > cfg.min_wait then |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
394 self:setreadtimeout(false); |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
395 self:pausefor(cost); |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
396 return; |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
397 end |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
398 end |
9932
df73ca804719
net.server_epoll: Skip delayed continuation read on paused connections
Kim Alvefur <zash@zash.se>
parents:
9931
diff
changeset
|
399 if self._wantread and self.conn:dirty() then |
7606
f40f1d9b7872
net.server_epoll: Set timers before calling onincoming, fixes traceback if connection is closed before onincoming returns
Kim Alvefur <zash@zash.se>
parents:
7604
diff
changeset
|
400 self:setreadtimeout(false); |
f40f1d9b7872
net.server_epoll: Set timers before calling onincoming, fixes traceback if connection is closed before onincoming returns
Kim Alvefur <zash@zash.se>
parents:
7604
diff
changeset
|
401 self:pausefor(cfg.read_retry_delay); |
f40f1d9b7872
net.server_epoll: Set timers before calling onincoming, fixes traceback if connection is closed before onincoming returns
Kim Alvefur <zash@zash.se>
parents:
7604
diff
changeset
|
402 else |
f40f1d9b7872
net.server_epoll: Set timers before calling onincoming, fixes traceback if connection is closed before onincoming returns
Kim Alvefur <zash@zash.se>
parents:
7604
diff
changeset
|
403 self:setreadtimeout(); |
f40f1d9b7872
net.server_epoll: Set timers before calling onincoming, fixes traceback if connection is closed before onincoming returns
Kim Alvefur <zash@zash.se>
parents:
7604
diff
changeset
|
404 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
405 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
406 |
7573
0f0a6febbc5a
net.server_epoll: Add some comments
Kim Alvefur <zash@zash.se>
parents:
7564
diff
changeset
|
407 -- Called when socket is writable |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
408 function interface:onwritable() |
8552
1fcefc16a55d
server_epoll: Onconnect should happen before first attempt to write to socket
Kim Alvefur <zash@zash.se>
parents:
8542
diff
changeset
|
409 self:onconnect(); |
8776
e7d4344e0fea
net.server_epoll: Handle connection being closed and destroyed in onconnect callback
Kim Alvefur <zash@zash.se>
parents:
8555
diff
changeset
|
410 if not self.conn then return; end -- could have been closed in onconnect |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
411 local buffer = self.writebuffer; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
412 local data = t_concat(buffer); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
413 local ok, err, partial = self.conn:send(data); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
414 if ok then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
415 self:set(nil, false); |
8541
8186114329d6
net.server_epoll: Remove special casing for having sent an empty write buffer because the wantwrite flag must be unset regardless
Kim Alvefur <zash@zash.se>
parents:
8540
diff
changeset
|
416 for i = #buffer, 1, -1 do |
8186114329d6
net.server_epoll: Remove special casing for having sent an empty write buffer because the wantwrite flag must be unset regardless
Kim Alvefur <zash@zash.se>
parents:
8540
diff
changeset
|
417 buffer[i] = nil; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
418 end |
8541
8186114329d6
net.server_epoll: Remove special casing for having sent an empty write buffer because the wantwrite flag must be unset regardless
Kim Alvefur <zash@zash.se>
parents:
8540
diff
changeset
|
419 self:setwritetimeout(false); |
8186114329d6
net.server_epoll: Remove special casing for having sent an empty write buffer because the wantwrite flag must be unset regardless
Kim Alvefur <zash@zash.se>
parents:
8540
diff
changeset
|
420 self:ondrain(); -- Be aware of writes in ondrain |
7589
125e4f2e0b65
net.server_epoll: Return early on successful write
Kim Alvefur <zash@zash.se>
parents:
7588
diff
changeset
|
421 return; |
8540
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
422 elseif partial then |
10305
2fadbf209678
net.server_epoll: Log size of partial writes (debug)
Kim Alvefur <zash@zash.se>
parents:
10300
diff
changeset
|
423 self:debug("Sent %d out of %d buffered bytes", partial, #data); |
7591
138f16eb39fb
net.server_epoll: Improve syntax
Kim Alvefur <zash@zash.se>
parents:
7590
diff
changeset
|
424 buffer[1] = data:sub(partial+1); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
425 for i = #buffer, 2, -1 do |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
426 buffer[i] = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
427 end |
10228
e77bf4222fae
net.server_epoll: Add support for opportunistic writes
Kim Alvefur <zash@zash.se>
parents:
10098
diff
changeset
|
428 self:set(nil, true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
429 self:setwritetimeout(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
430 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
431 if err == "wantwrite" or err == "timeout" then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
432 self:set(nil, true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
433 elseif err == "wantread" then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
434 self:set(true, nil); |
7687
ae24deae6488
net.server_epoll: Separate ok from err branch more
Kim Alvefur <zash@zash.se>
parents:
7658
diff
changeset
|
435 elseif err ~= "timeout" then |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
436 self:on("disconnect", err); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
437 self:destroy(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
438 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
439 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
440 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
441 -- The write buffer has been successfully emptied |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
442 function interface:ondrain() |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
443 return self:on("drain"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
444 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
445 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
446 -- Add data to write buffer and set flag for wanting to write |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
447 function interface:write(data) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
448 local buffer = self.writebuffer; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
449 if buffer then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
450 t_insert(buffer, data); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
451 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
452 self.writebuffer = { data }; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
453 end |
9581
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
454 if not self._write_lock then |
10228
e77bf4222fae
net.server_epoll: Add support for opportunistic writes
Kim Alvefur <zash@zash.se>
parents:
10098
diff
changeset
|
455 if cfg.opportunistic_writes then |
e77bf4222fae
net.server_epoll: Add support for opportunistic writes
Kim Alvefur <zash@zash.se>
parents:
10098
diff
changeset
|
456 self:onwritable(); |
e77bf4222fae
net.server_epoll: Add support for opportunistic writes
Kim Alvefur <zash@zash.se>
parents:
10098
diff
changeset
|
457 return #data; |
e77bf4222fae
net.server_epoll: Add support for opportunistic writes
Kim Alvefur <zash@zash.se>
parents:
10098
diff
changeset
|
458 end |
9581
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
459 self:setwritetimeout(); |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
460 self:set(nil, true); |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
461 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
462 return #data; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
463 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
464 interface.send = interface.write; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
465 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
466 -- Close, possibly after writing is done |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
467 function interface:close() |
8007
67f8954875a4
net.server_epoll: Check write buffer for remaining data to write when closing
Kim Alvefur <zash@zash.se>
parents:
8006
diff
changeset
|
468 if self.writebuffer and self.writebuffer[1] then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
469 self:set(false, true); -- Flush final buffer contents |
7593
0266434d7df1
net.server_epoll: Prevent further writing after closing a socket
Kim Alvefur <zash@zash.se>
parents:
7592
diff
changeset
|
470 self.write, self.send = noop, noop; -- No more writing |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
471 self:debug("Close after writing"); |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
472 self.ondrain = interface.close; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
473 else |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
474 self:debug("Closing now"); |
7593
0266434d7df1
net.server_epoll: Prevent further writing after closing a socket
Kim Alvefur <zash@zash.se>
parents:
7592
diff
changeset
|
475 self.write, self.send = noop, noop; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
476 self.close = noop; |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
477 self:on("disconnect"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
478 self:destroy(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
479 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
480 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
481 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
482 function interface:destroy() |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
483 self:del(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
484 self:setwritetimeout(false); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
485 self:setreadtimeout(false); |
7594
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
486 self.onreadable = noop; |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
487 self.onwritable = noop; |
7594
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
488 self.destroy = noop; |
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
489 self.close = noop; |
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
490 self.on = noop; |
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
491 self.conn:close(); |
5e884501a9ac
net.server_epoll: Prevent anything from happening to destroyed sockets
Kim Alvefur <zash@zash.se>
parents:
7593
diff
changeset
|
492 self.conn = nil; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
493 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
494 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
495 function interface:ssl() |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
496 return self._tls; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
497 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
498 |
9300
1df8832bd29e
net.server_epoll: Rename all variables holding TLS contexts for consistency
Kim Alvefur <zash@zash.se>
parents:
8983
diff
changeset
|
499 function interface:starttls(tls_ctx) |
1df8832bd29e
net.server_epoll: Rename all variables holding TLS contexts for consistency
Kim Alvefur <zash@zash.se>
parents:
8983
diff
changeset
|
500 if tls_ctx then self.tls_ctx = tls_ctx; end |
9430
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
501 self.starttls = false; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
502 if self.writebuffer and self.writebuffer[1] then |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
503 self:debug("Start TLS after write"); |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
504 self.ondrain = interface.starttls; |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
505 self:set(nil, true); -- make sure wantwrite is set |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
506 else |
9430
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
507 if self.ondrain == interface.starttls then |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
508 self.ondrain = nil; |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
509 end |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
510 self.onwritable = interface.tlshandskake; |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
511 self.onreadable = interface.tlshandskake; |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
512 self:set(true, true); |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
513 self:debug("Prepared to start TLS"); |
9430
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
514 end |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
515 end |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
516 |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
517 function interface:tlshandskake() |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
518 self:setwritetimeout(false); |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
519 self:setreadtimeout(false); |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
520 if not self._tls then |
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
521 self._tls = true; |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
522 self:debug("Starting TLS now"); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
523 self:del(); |
10329
c0a72c97e6cf
net.server_epoll: Fix to get remote IP on direct TLS connections
Kim Alvefur <zash@zash.se>
parents:
10328
diff
changeset
|
524 self:updatenames(); -- Can't getpeer/sockname after wrap() |
9431
c3c0523a37c6
net.server_epoll: Catch errors from luasec.wrap
Kim Alvefur <zash@zash.se>
parents:
9430
diff
changeset
|
525 local ok, conn, err = pcall(luasec.wrap, self.conn, self.tls_ctx); |
c3c0523a37c6
net.server_epoll: Catch errors from luasec.wrap
Kim Alvefur <zash@zash.se>
parents:
9430
diff
changeset
|
526 if not ok then |
c3c0523a37c6
net.server_epoll: Catch errors from luasec.wrap
Kim Alvefur <zash@zash.se>
parents:
9430
diff
changeset
|
527 conn, err = ok, conn; |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
528 self:debug("Failed to initialize TLS: %s", err); |
9431
c3c0523a37c6
net.server_epoll: Catch errors from luasec.wrap
Kim Alvefur <zash@zash.se>
parents:
9430
diff
changeset
|
529 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
530 if not conn then |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
531 self:on("disconnect", err); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
532 self:destroy(); |
7595
166b2e0766a4
net.server_epoll: Return status from starttls method
Kim Alvefur <zash@zash.se>
parents:
7594
diff
changeset
|
533 return conn, err; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
534 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
535 conn:settimeout(0); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
536 self.conn = conn; |
9847
adc0672b700e
net.server_epoll: Add support for SNI (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
537 if conn.sni then |
adc0672b700e
net.server_epoll: Add support for SNI (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
538 if self.servername then |
adc0672b700e
net.server_epoll: Add support for SNI (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
539 conn:sni(self.servername); |
9853
9aea8dbb105d
net.server: Only add alternate SNI contexts if at least one is provided
Kim Alvefur <zash@zash.se>
parents:
9847
diff
changeset
|
540 elseif self._server and type(self._server.hosts) == "table" and next(self._server.hosts) ~= nil then |
9847
adc0672b700e
net.server_epoll: Add support for SNI (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
541 conn:sni(self._server.hosts, true); |
adc0672b700e
net.server_epoll: Add support for SNI (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
542 end |
adc0672b700e
net.server_epoll: Add support for SNI (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
543 end |
9430
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
544 self:on("starttls"); |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
545 self.ondrain = nil; |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
546 self.onwritable = interface.tlshandskake; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
547 self.onreadable = interface.tlshandskake; |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
548 return self:init(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
549 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
550 local ok, err = self.conn:dohandshake(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
551 if ok then |
10309
2a31571efce0
net.server_epoll: Guard against nil return from TLS info method
Kim Alvefur <zash@zash.se>
parents:
10307
diff
changeset
|
552 local info = self.conn.info and self.conn:info(); |
2a31571efce0
net.server_epoll: Guard against nil return from TLS info method
Kim Alvefur <zash@zash.se>
parents:
10307
diff
changeset
|
553 if type(info) == "table" then |
10307
6a997f0ceba8
net.server_epoll: Log TLS version and cipher for all completed handshakes
Kim Alvefur <zash@zash.se>
parents:
10305
diff
changeset
|
554 self:debug("TLS handshake complete (%s with %s)", info.protocol, info.cipher); |
6a997f0ceba8
net.server_epoll: Log TLS version and cipher for all completed handshakes
Kim Alvefur <zash@zash.se>
parents:
10305
diff
changeset
|
555 else |
6a997f0ceba8
net.server_epoll: Log TLS version and cipher for all completed handshakes
Kim Alvefur <zash@zash.se>
parents:
10305
diff
changeset
|
556 self:debug("TLS handshake complete"); |
6a997f0ceba8
net.server_epoll: Log TLS version and cipher for all completed handshakes
Kim Alvefur <zash@zash.se>
parents:
10305
diff
changeset
|
557 end |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
558 self.onwritable = nil; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
559 self.onreadable = nil; |
8008
62ed3795be13
net.server_epoll: Attempt to simplify state control
Kim Alvefur <zash@zash.se>
parents:
8007
diff
changeset
|
560 self:on("status", "ssl-handshake-complete"); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
561 self:setwritetimeout(); |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
562 self:set(true, true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
563 elseif err == "wantread" then |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
564 self:debug("TLS handshake to wait until readable"); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
565 self:set(true, false); |
9822
13c2707d2417
net.server_epoll: Rename handshake_timeout to ssl_handshake_timeout (fixes #1319)
Kim Alvefur <zash@zash.se>
parents:
9821
diff
changeset
|
566 self:setreadtimeout(cfg.ssl_handshake_timeout); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
567 elseif err == "wantwrite" then |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
568 self:debug("TLS handshake to wait until writable"); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
569 self:set(false, true); |
9822
13c2707d2417
net.server_epoll: Rename handshake_timeout to ssl_handshake_timeout (fixes #1319)
Kim Alvefur <zash@zash.se>
parents:
9821
diff
changeset
|
570 self:setwritetimeout(cfg.ssl_handshake_timeout); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
571 else |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
572 self:debug("TLS handshake error: %s", err); |
7583
0498daffa6f0
net.server_epoll: Call callbacks through common method in protected mode
Kim Alvefur <zash@zash.se>
parents:
7574
diff
changeset
|
573 self:on("disconnect", err); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
574 self:destroy(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
575 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
576 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
577 |
10233
600eee3c4752
net.server: Accept and save an 'extra' field for client connections
Kim Alvefur <zash@zash.se>
parents:
10228
diff
changeset
|
578 local function wrapsocket(client, server, read_size, listeners, tls_ctx, extra) -- luasocket object -> interface object |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
579 client:settimeout(0); |
10279
0e1701197722
net.server_epoll: Make log tag accessible as a field
Kim Alvefur <zash@zash.se>
parents:
10278
diff
changeset
|
580 local conn_id = ("conn%s"):format(new_id()); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
581 local conn = setmetatable({ |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
582 conn = client; |
8271
c09deab9989e
net.server_epoll: Add interface:server() method to match server_event (fixes c2s:show traceback in telnet console)
Kim Alvefur <zash@zash.se>
parents:
8091
diff
changeset
|
583 _server = server; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
584 created = gettime(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
585 listeners = listeners; |
9310
2d2d4c293efa
net.server_epoll: Rename pattern to read_size
Kim Alvefur <zash@zash.se>
parents:
9309
diff
changeset
|
586 read_size = read_size or (server and server.read_size); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
587 writebuffer = {}; |
9432
2e3f66475cc8
net.server_epoll: Inherit Direct TLS flag from servers to their clients
Kim Alvefur <zash@zash.se>
parents:
9431
diff
changeset
|
588 tls_ctx = tls_ctx or (server and server.tls_ctx); |
2e3f66475cc8
net.server_epoll: Inherit Direct TLS flag from servers to their clients
Kim Alvefur <zash@zash.se>
parents:
9431
diff
changeset
|
589 tls_direct = server and server.tls_direct; |
10279
0e1701197722
net.server_epoll: Make log tag accessible as a field
Kim Alvefur <zash@zash.se>
parents:
10278
diff
changeset
|
590 id = conn_id; |
0e1701197722
net.server_epoll: Make log tag accessible as a field
Kim Alvefur <zash@zash.se>
parents:
10278
diff
changeset
|
591 log = logger.init(conn_id); |
10233
600eee3c4752
net.server: Accept and save an 'extra' field for client connections
Kim Alvefur <zash@zash.se>
parents:
10228
diff
changeset
|
592 extra = extra; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
593 }, interface_mt); |
7591
138f16eb39fb
net.server_epoll: Improve syntax
Kim Alvefur <zash@zash.se>
parents:
7590
diff
changeset
|
594 |
10234
d4390c427a66
net.server: Handle server name (SNI) as extra argument
Kim Alvefur <zash@zash.se>
parents:
10233
diff
changeset
|
595 if extra then |
d4390c427a66
net.server: Handle server name (SNI) as extra argument
Kim Alvefur <zash@zash.se>
parents:
10233
diff
changeset
|
596 if extra.servername then |
d4390c427a66
net.server: Handle server name (SNI) as extra argument
Kim Alvefur <zash@zash.se>
parents:
10233
diff
changeset
|
597 conn.servername = extra.servername; |
d4390c427a66
net.server: Handle server name (SNI) as extra argument
Kim Alvefur <zash@zash.se>
parents:
10233
diff
changeset
|
598 end |
d4390c427a66
net.server: Handle server name (SNI) as extra argument
Kim Alvefur <zash@zash.se>
parents:
10233
diff
changeset
|
599 end |
d4390c427a66
net.server: Handle server name (SNI) as extra argument
Kim Alvefur <zash@zash.se>
parents:
10233
diff
changeset
|
600 |
9383
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
601 conn:updatenames(); |
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
602 return conn; |
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
603 end |
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
604 |
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
605 function interface:updatenames() |
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
606 local conn = self.conn; |
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
607 local ok, peername, peerport = pcall(conn.getpeername, conn); |
10330
d86b1304451a
net.server_epoll: Handle getpeer/sockname returning a normal error
Kim Alvefur <zash@zash.se>
parents:
10329
diff
changeset
|
608 if ok and peername then |
9383
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
609 self.peername, self.peerport = peername, peerport; |
7584
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
610 end |
9383
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
611 local ok, sockname, sockport = pcall(conn.getsockname, conn); |
10330
d86b1304451a
net.server_epoll: Handle getpeer/sockname returning a normal error
Kim Alvefur <zash@zash.se>
parents:
10329
diff
changeset
|
612 if ok and sockname then |
9383
3260902a5b22
net.server_epoll: Factor out IP/port detection into its own method
Kim Alvefur <zash@zash.se>
parents:
9382
diff
changeset
|
613 self.sockname, self.sockport = sockname, sockport; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
614 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
615 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
616 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
617 -- A server interface has new incoming connections waiting |
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
618 -- This replaces the onreadable callback |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
619 function interface:onacceptable() |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
620 local conn, err = self.conn:accept(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
621 if not conn then |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
622 self:debug("Error accepting new client: %s, server will be paused for %ds", err, cfg.accept_retry_interval); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
623 self:pausefor(cfg.accept_retry_interval); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
624 return; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
625 end |
9309
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
626 local client = wrapsocket(conn, self, nil, self.listeners); |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
627 client:debug("New connection %s on server %s", client, self); |
9430
412ff404bf58
net.server_epoll: Delay wrapping sockets in TLS until just before first handshake
Kim Alvefur <zash@zash.se>
parents:
9387
diff
changeset
|
628 client:init(); |
9309
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
629 if self.tls_direct then |
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
630 client:starttls(self.tls_ctx); |
9695
5ec4eaf76de8
net.server_epoll: Call onconnect right after accept()ing a new client
Kim Alvefur <zash@zash.se>
parents:
9666
diff
changeset
|
631 else |
5ec4eaf76de8
net.server_epoll: Call onconnect right after accept()ing a new client
Kim Alvefur <zash@zash.se>
parents:
9666
diff
changeset
|
632 client:onconnect(); |
9309
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
633 end |
7597
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
634 end |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
635 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
636 -- Initialization |
7597
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
637 function interface:init() |
9824
729de68f0068
net.server_epoll: Separate timeout for initial connection attempts
Kim Alvefur <zash@zash.se>
parents:
9823
diff
changeset
|
638 self:setwritetimeout(cfg.connect_timeout); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
639 return self:add(true, true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
640 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
641 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
642 function interface:pause() |
10280
6b8b255f1d10
net.server_epoll: Add debug logging for various connection events
Kim Alvefur <zash@zash.se>
parents:
10279
diff
changeset
|
643 self:debug("Pause reading"); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
644 return self:set(false); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
645 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
646 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
647 function interface:resume() |
10280
6b8b255f1d10
net.server_epoll: Add debug logging for various connection events
Kim Alvefur <zash@zash.se>
parents:
10279
diff
changeset
|
648 self:debug("Resume reading"); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
649 return self:set(true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
650 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
651 |
7624 | 652 -- Pause connection for some time |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
653 function interface:pausefor(t) |
10098
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
654 self:debug("Pause for %fs", t); |
7559
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
655 if self._pausefor then |
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
656 self._pausefor:close(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
657 end |
7559
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
658 if t == false then return; end |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
659 self:set(false); |
7559
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
660 self._pausefor = addtimer(t, function () |
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
661 self._pausefor = nil; |
9994
524b8cd76780
net.server_epoll: Restore wantread flag after pause (fixes #1354)
Kim Alvefur <zash@zash.se>
parents:
9932
diff
changeset
|
662 self:set(true); |
7707
7bb1cc7278b6
net.server_epoll: Make sure socket exists before checking if there is buffered data in it
Kim Alvefur <zash@zash.se>
parents:
7691
diff
changeset
|
663 if self.conn and self.conn:dirty() then |
7560
785fa1138293
net.server_epoll: Continue reading after a slight delay if there is buffered data after a read
Kim Alvefur <zash@zash.se>
parents:
7559
diff
changeset
|
664 self:onreadable(); |
785fa1138293
net.server_epoll: Continue reading after a slight delay if there is buffered data after a read
Kim Alvefur <zash@zash.se>
parents:
7559
diff
changeset
|
665 end |
7559
b1b27dedf4cf
net.server_epoll: Replace any previous pausefor timer
Kim Alvefur <zash@zash.se>
parents:
7558
diff
changeset
|
666 end); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
667 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
668 |
10098
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
669 function interface:setlimit(Bps) |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
670 if Bps > 0 then |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
671 self._limit = 1/Bps; |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
672 else |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
673 self._limit = nil; |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
674 end |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
675 end |
dda1c7ccb209
server_epoll: Add native support for per socket bandwith limits
Kim Alvefur <zash@zash.se>
parents:
10091
diff
changeset
|
676 |
9581
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
677 function interface:pause_writes() |
10300
78f2a3a9c537
net.server_epoll: Return early when attepting to set write lock state to current state
Kim Alvefur <zash@zash.se>
parents:
10289
diff
changeset
|
678 if self._write_lock then |
78f2a3a9c537
net.server_epoll: Return early when attepting to set write lock state to current state
Kim Alvefur <zash@zash.se>
parents:
10289
diff
changeset
|
679 return |
78f2a3a9c537
net.server_epoll: Return early when attepting to set write lock state to current state
Kim Alvefur <zash@zash.se>
parents:
10289
diff
changeset
|
680 end |
10280
6b8b255f1d10
net.server_epoll: Add debug logging for various connection events
Kim Alvefur <zash@zash.se>
parents:
10279
diff
changeset
|
681 self:debug("Pause writes"); |
9581
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
682 self._write_lock = true; |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
683 self:setwritetimeout(false); |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
684 self:set(nil, false); |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
685 end |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
686 |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
687 function interface:resume_writes() |
10300
78f2a3a9c537
net.server_epoll: Return early when attepting to set write lock state to current state
Kim Alvefur <zash@zash.se>
parents:
10289
diff
changeset
|
688 if not self._write_lock then |
78f2a3a9c537
net.server_epoll: Return early when attepting to set write lock state to current state
Kim Alvefur <zash@zash.se>
parents:
10289
diff
changeset
|
689 return |
78f2a3a9c537
net.server_epoll: Return early when attepting to set write lock state to current state
Kim Alvefur <zash@zash.se>
parents:
10289
diff
changeset
|
690 end |
10280
6b8b255f1d10
net.server_epoll: Add debug logging for various connection events
Kim Alvefur <zash@zash.se>
parents:
10279
diff
changeset
|
691 self:debug("Resume writes"); |
9581
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
692 self._write_lock = nil; |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
693 if self.writebuffer[1] then |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
694 self:setwritetimeout(); |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
695 self:set(nil, true); |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
696 end |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
697 end |
1be99aedb0b3
net.server: Add an API for holding writes of outgoing data
Kim Alvefur <zash@zash.se>
parents:
9573
diff
changeset
|
698 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
699 -- Connected! |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
700 function interface:onconnect() |
9594
dcf466e04f81
net.server_epoll: Use method to update peername on connect
Kim Alvefur <zash@zash.se>
parents:
9587
diff
changeset
|
701 self:updatenames(); |
10331
76bd77ad6a2a
server_epoll: Log full string represestation when connected
Kim Alvefur <zash@zash.se>
parents:
10330
diff
changeset
|
702 self:debug("Connected (%s)", self); |
8540
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
703 self.onconnect = noop; |
ba9fd886b34d
net.server_epoll: Call onconnect listener on first successful read or write
Kim Alvefur <zash@zash.se>
parents:
8507
diff
changeset
|
704 self:on("connect"); |
8506
174fd716c9fa
server_epoll: Rework how socket readyness is detected
Kim Alvefur <zash@zash.se>
parents:
8271
diff
changeset
|
705 end |
174fd716c9fa
server_epoll: Rework how socket readyness is detected
Kim Alvefur <zash@zash.se>
parents:
8271
diff
changeset
|
706 |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
707 local function listen(addr, port, listeners, config) |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
708 local conn, err = socket.bind(addr, port, cfg.tcp_backlog); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
709 if not conn then return conn, err; end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
710 conn:settimeout(0); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
711 local server = setmetatable({ |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
712 conn = conn; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
713 created = gettime(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
714 listeners = listeners; |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
715 read_size = config and config.read_size; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
716 onreadable = interface.onacceptable; |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
717 tls_ctx = config and config.tls_ctx; |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
718 tls_direct = config and config.tls_direct; |
9847
adc0672b700e
net.server_epoll: Add support for SNI (#409)
Kim Alvefur <zash@zash.se>
parents:
9835
diff
changeset
|
719 hosts = config and config.sni_hosts; |
7584
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
720 sockname = addr; |
98ee3ed105cf
net.server_epoll: Collect and expose local and remote address and ports
Kim Alvefur <zash@zash.se>
parents:
7583
diff
changeset
|
721 sockport = port; |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
722 log = logger.init(("serv%s"):format(new_id())); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
723 }, interface_mt); |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
724 server:debug("Server %s created", server); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
725 server:add(true, false); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
726 return server; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
727 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
728 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
729 -- COMPAT |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
730 local function addserver(addr, port, listeners, read_size, tls_ctx) |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
731 return listen(addr, port, listeners, { |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
732 read_size = read_size; |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
733 tls_ctx = tls_ctx; |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
734 tls_direct = tls_ctx and true or false; |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
735 }); |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
736 end |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
737 |
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
738 -- COMPAT |
10233
600eee3c4752
net.server: Accept and save an 'extra' field for client connections
Kim Alvefur <zash@zash.se>
parents:
10228
diff
changeset
|
739 local function wrapclient(conn, addr, port, listeners, read_size, tls_ctx, extra) |
600eee3c4752
net.server: Accept and save an 'extra' field for client connections
Kim Alvefur <zash@zash.se>
parents:
10228
diff
changeset
|
740 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx, extra); |
7597
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
741 if not client.peername then |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
742 client.peername, client.peerport = addr, port; |
cd34aa6b1632
net.server_epoll: Refactor to use common initialization
Kim Alvefur <zash@zash.se>
parents:
7596
diff
changeset
|
743 end |
9473
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
744 local ok, err = client:init(); |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
745 if not ok then return ok, err; end |
9309
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
746 if tls_ctx then |
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
747 client:starttls(tls_ctx); |
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
748 end |
7551
838212918f11
net.server_epoll: Rename arguments and varibles for consistensy
Kim Alvefur <zash@zash.se>
parents:
7550
diff
changeset
|
749 return client; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
750 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
751 |
7800
dadf9ba290c5
server_epoll: Add some comments describing some functions
Kim Alvefur <zash@zash.se>
parents:
7799
diff
changeset
|
752 -- New outgoing TCP connection |
10233
600eee3c4752
net.server: Accept and save an 'extra' field for client connections
Kim Alvefur <zash@zash.se>
parents:
10228
diff
changeset
|
753 local function addclient(addr, port, listeners, read_size, tls_ctx, typ, extra) |
9497
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
754 local create; |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
755 if not typ then |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
756 local n = inet_pton(addr); |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
757 if not n then return nil, "invalid-ip"; end |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
758 if #n == 16 then |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
759 typ = "tcp6"; |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
760 else |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
761 typ = "tcp4"; |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
762 end |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
763 end |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
764 if typ then |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
765 create = socket[typ]; |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
766 end |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
767 if type(create) ~= "function" then |
183ff7a8051b
net.server_epoll: Add support for the conn_type argument to addclient
Kim Alvefur <zash@zash.se>
parents:
9477
diff
changeset
|
768 return nil, "invalid socket type"; |
9473
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
769 end |
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
770 local conn, err = create(); |
10082
1df53ef1f60d
net.server_epoll: Return errors from creating sockets
Kim Alvefur <zash@zash.se>
parents:
10081
diff
changeset
|
771 if not conn then return conn, err; end |
9498
cc593002f2e2
net.server_epoll: Check return conditions of early socket setup
Kim Alvefur <zash@zash.se>
parents:
9497
diff
changeset
|
772 local ok, err = conn:settimeout(0); |
cc593002f2e2
net.server_epoll: Check return conditions of early socket setup
Kim Alvefur <zash@zash.se>
parents:
9497
diff
changeset
|
773 if not ok then return ok, err; end |
9499
36e379f058c8
net.server: Swich method for connecting sockets with remotes
Kim Alvefur <zash@zash.se>
parents:
9498
diff
changeset
|
774 local ok, err = conn:setpeername(addr, port); |
9498
cc593002f2e2
net.server_epoll: Check return conditions of early socket setup
Kim Alvefur <zash@zash.se>
parents:
9497
diff
changeset
|
775 if not ok and err ~= "timeout" then return ok, err; end |
10233
600eee3c4752
net.server: Accept and save an 'extra' field for client connections
Kim Alvefur <zash@zash.se>
parents:
10228
diff
changeset
|
776 local client = wrapsocket(conn, nil, read_size, listeners, tls_ctx, extra) |
9473
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
777 local ok, err = client:init(); |
10332
e05ea8223b32
net.server_epoll: Save IP and port from connection creation call
Kim Alvefur <zash@zash.se>
parents:
10331
diff
changeset
|
778 if not client.peername then |
e05ea8223b32
net.server_epoll: Save IP and port from connection creation call
Kim Alvefur <zash@zash.se>
parents:
10331
diff
changeset
|
779 -- otherwise not set until connected |
e05ea8223b32
net.server_epoll: Save IP and port from connection creation call
Kim Alvefur <zash@zash.se>
parents:
10331
diff
changeset
|
780 client.peername, client.peerport = addr, port; |
e05ea8223b32
net.server_epoll: Save IP and port from connection creation call
Kim Alvefur <zash@zash.se>
parents:
10331
diff
changeset
|
781 end |
9473
5fdda751333a
net.server: Require IP address as argument to addclient (no DNS names)
Kim Alvefur <zash@zash.se>
parents:
9445
diff
changeset
|
782 if not ok then return ok, err; end |
10328
5c2b31c823b9
net.server_epoll: Move a log message to improve ordering
Kim Alvefur <zash@zash.se>
parents:
10309
diff
changeset
|
783 client:debug("Client %s created", client); |
9309
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
784 if tls_ctx then |
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
785 client:starttls(tls_ctx); |
33d500c25d76
net.server_epoll: Refactor Direct TLS assumptions outwards
Kim Alvefur <zash@zash.se>
parents:
9306
diff
changeset
|
786 end |
7556
1f777b38b66c
net.server_epoll: Make addclient conform to API (expected by net.http.request)
Kim Alvefur <zash@zash.se>
parents:
7555
diff
changeset
|
787 return client, conn; |
7553
4f3e4a092348
net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents:
7552
diff
changeset
|
788 end |
4f3e4a092348
net.server_epoll: Make addclient use wrapclient
Kim Alvefur <zash@zash.se>
parents:
7552
diff
changeset
|
789 |
9319
7c954c75b6ac
net.server_epoll: Fix write signal handling in watchfd API
Kim Alvefur <zash@zash.se>
parents:
9314
diff
changeset
|
790 local function watchfd(fd, onreadable, onwritable) |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
791 local conn = setmetatable({ |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
792 conn = fd; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
793 onreadable = onreadable; |
9319
7c954c75b6ac
net.server_epoll: Fix write signal handling in watchfd API
Kim Alvefur <zash@zash.se>
parents:
9314
diff
changeset
|
794 onwritable = onwritable; |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
795 close = function (self) |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
796 self:del(); |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
797 end |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
798 }, interface_mt); |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
799 if type(fd) == "number" then |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
800 conn.getfd = function () |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
801 return fd; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
802 end; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
803 -- Otherwise it'll need to be something LuaSocket-compatible |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
804 end |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
805 conn.log = logger.init(("fdwatch%s"):format(new_id())); |
9319
7c954c75b6ac
net.server_epoll: Fix write signal handling in watchfd API
Kim Alvefur <zash@zash.se>
parents:
9314
diff
changeset
|
806 conn:add(onreadable, onwritable); |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
807 return conn; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
808 end; |
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
809 |
7800
dadf9ba290c5
server_epoll: Add some comments describing some functions
Kim Alvefur <zash@zash.se>
parents:
7799
diff
changeset
|
810 -- Dump all data from one connection into another |
10283
dff360d00dfa
net.server_epoll: Handle read size argument to link
Kim Alvefur <zash@zash.se>
parents:
10282
diff
changeset
|
811 local function link(from, to, read_size) |
10280
6b8b255f1d10
net.server_epoll: Add debug logging for various connection events
Kim Alvefur <zash@zash.se>
parents:
10279
diff
changeset
|
812 from:debug("Linking to %s", to.id); |
10284
cd669975031b
net.server_epoll: Fix link function to not replace listeners
Kim Alvefur <zash@zash.se>
parents:
10283
diff
changeset
|
813 function from:onincoming(data) |
cd669975031b
net.server_epoll: Fix link function to not replace listeners
Kim Alvefur <zash@zash.se>
parents:
10283
diff
changeset
|
814 self:pause(); |
10289
8c59b20f71dc
net.server_epoll: Correct indentation
Kim Alvefur <zash@zash.se>
parents:
10285
diff
changeset
|
815 to:write(data); |
10284
cd669975031b
net.server_epoll: Fix link function to not replace listeners
Kim Alvefur <zash@zash.se>
parents:
10283
diff
changeset
|
816 end |
10285
bcf61e22f841
net.server_epoll: Ignore unused self argument [luacheck]
Kim Alvefur <zash@zash.se>
parents:
10284
diff
changeset
|
817 function to:ondrain() -- luacheck: ignore 212/self |
10289
8c59b20f71dc
net.server_epoll: Correct indentation
Kim Alvefur <zash@zash.se>
parents:
10285
diff
changeset
|
818 from:resume(); |
10284
cd669975031b
net.server_epoll: Fix link function to not replace listeners
Kim Alvefur <zash@zash.se>
parents:
10283
diff
changeset
|
819 end |
10283
dff360d00dfa
net.server_epoll: Handle read size argument to link
Kim Alvefur <zash@zash.se>
parents:
10282
diff
changeset
|
820 from:set_mode(read_size); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
821 from:set(true, nil); |
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
822 to:set(nil, true); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
823 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
824 |
9438
916bee81eb7e
net.server_epoll: Make :set_send a noop, should fix net.adns
Kim Alvefur <zash@zash.se>
parents:
9432
diff
changeset
|
825 -- COMPAT |
916bee81eb7e
net.server_epoll: Make :set_send a noop, should fix net.adns
Kim Alvefur <zash@zash.se>
parents:
9432
diff
changeset
|
826 -- net.adns calls this but then replaces :send so this can be a noop |
9439
988392a56495
net.server_epoll: Fix luacheck annotation
Kim Alvefur <zash@zash.se>
parents:
9438
diff
changeset
|
827 function interface:set_send(new_send) -- luacheck: ignore 212 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
828 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
829 |
7800
dadf9ba290c5
server_epoll: Add some comments describing some functions
Kim Alvefur <zash@zash.se>
parents:
7799
diff
changeset
|
830 -- Close all connections and servers |
7794
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
831 local function closeall() |
7799
93643a61858a
server_epoll: Ignore unused loop variable [luacheck]
Kim Alvefur <zash@zash.se>
parents:
7797
diff
changeset
|
832 for fd, conn in pairs(fds) do -- luacheck: ignore 213/fd |
7794
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
833 conn:close(); |
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
834 end |
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
835 end |
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
836 |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
837 local quitting = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
838 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
839 -- Signal main loop about shutdown via above upvalue |
7796
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
840 local function setquitting(quit) |
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
841 if quit then |
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
842 quitting = "quitting"; |
7797
d0c32d86724f
server_epoll: Flush and close all connections before quitting mainloop so no data is lost in buffers
Kim Alvefur <zash@zash.se>
parents:
7796
diff
changeset
|
843 closeall(); |
7796
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
844 else |
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
845 quitting = nil; |
12609aee3b3b
server_epoll: Handle the argument to setquitting like server_select
Kim Alvefur <zash@zash.se>
parents:
7795
diff
changeset
|
846 end |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
847 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
848 |
7637
cccea9136b2d
net.server_epoll: More comments
Kim Alvefur <zash@zash.se>
parents:
7630
diff
changeset
|
849 -- Main loop |
7795
03081bd5c5bb
net.server_epoll: Add the 'once' argument to loop for parity with server_select
Kim Alvefur <zash@zash.se>
parents:
7794
diff
changeset
|
850 local function loop(once) |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
851 repeat |
7658
e14a124c4d73
net.server_epoll: Make minimum poll wait time configurable
Kim Alvefur <zash@zash.se>
parents:
7657
diff
changeset
|
852 local t = runtimers(cfg.max_wait, cfg.min_wait); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
853 local fd, r, w = poll:wait(t); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
854 if fd then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
855 local conn = fds[fd]; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
856 if conn then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
857 if r then |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
858 conn:onreadable(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
859 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
860 if w then |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
861 conn:onwritable(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
862 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
863 else |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
864 log("debug", "Removing unknown fd %d", fd); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
865 poll:del(fd); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
866 end |
9508
2055b497b515
net.server_epoll: Special handling of signal interrupts
Kim Alvefur <zash@zash.se>
parents:
9507
diff
changeset
|
867 elseif r ~= "timeout" and r ~= "signal" then |
9445
af7eafe82447
net.server_epoll: Log error number from wait()
Kim Alvefur <zash@zash.se>
parents:
9441
diff
changeset
|
868 log("debug", "epoll_wait error: %s[%d]", r, w); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
869 end |
7797
d0c32d86724f
server_epoll: Flush and close all connections before quitting mainloop so no data is lost in buffers
Kim Alvefur <zash@zash.se>
parents:
7796
diff
changeset
|
870 until once or (quitting and next(fds) == nil); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
871 return quitting; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
872 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
873 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
874 return { |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
875 get_backend = function () return "epoll"; end; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
876 addserver = addserver; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
877 addclient = addclient; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
878 add_task = addtimer; |
9835
20bf5b47c1fb
net.server: New API for creating server listeners
Kim Alvefur <zash@zash.se>
parents:
9825
diff
changeset
|
879 listen = listen; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
880 at = at; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
881 loop = loop; |
7794
424bbbeb9257
server_epoll: Add closeall() method for closing all clients and servers
Kim Alvefur <zash@zash.se>
parents:
7737
diff
changeset
|
882 closeall = closeall; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
883 setquitting = setquitting; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
884 wrapclient = wrapclient; |
8781
53178b6ba589
net.server: Add watchfd, a simple API for watching file descriptors
Kim Alvefur <zash@zash.se>
parents:
8778
diff
changeset
|
885 watchfd = watchfd; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
886 link = link; |
7657
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
887 set_config = function (newconfig) |
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
888 cfg = setmetatable(newconfig, default_config); |
252823632401
net.server_epoll: Support for setting configuration parameters
Kim Alvefur <zash@zash.se>
parents:
7637
diff
changeset
|
889 end; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
890 |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
891 -- libevent emulation |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
892 event = { EV_READ = "r", EV_WRITE = "w", EV_READWRITE = "rw", EV_LEAVE = -1 }; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
893 addevent = function (fd, mode, callback) |
10088
97da8452c6a6
net.server_epoll: Deprecate libevent emulation layer
Kim Alvefur <zash@zash.se>
parents:
10082
diff
changeset
|
894 log("warn", "Using deprecated libevent emulation, please update code to use watchfd API instead"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
895 local function onevent(self) |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
896 local ret = self:callback(); |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
897 if ret == -1 then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
898 self:set(false, false); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
899 elseif ret then |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
900 self:set(mode == "r" or mode == "rw", mode == "w" or mode == "rw"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
901 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
902 end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
903 |
7817
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
904 local conn = setmetatable({ |
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
905 getfd = function () return fd; end; |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
906 callback = callback; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
907 onreadable = onevent; |
8542
8c39ec8e6018
net.server_epoll: Spelling writeable -> writable
Kim Alvefur <zash@zash.se>
parents:
8541
diff
changeset
|
908 onwritable = onevent; |
7817
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
909 close = function (self) |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
910 self:del(); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
911 fds[fd] = nil; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
912 end; |
7817
4e3654031693
net.server_epoll: Reuse interface metatable for event compat layer
Kim Alvefur <zash@zash.se>
parents:
7811
diff
changeset
|
913 }, interface_mt); |
10089
90e459f48cbd
net.server_epoll: Overhaul logging with one log sink per connection
Kim Alvefur <zash@zash.se>
parents:
10088
diff
changeset
|
914 conn.log = logger.init(("fdwatch%d"):format(conn:getfd())); |
9314
c9eef7e8ee65
net.server_epoll: Use util.poll
Kim Alvefur <zash@zash.se>
parents:
9311
diff
changeset
|
915 local ok, err = conn:add(mode == "r" or mode == "rw", mode == "w" or mode == "rw"); |
7547
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
916 if not ok then return ok, err; end |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
917 return conn; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
918 end; |
b327322ce2dd
net.server_epoll: New experimental server backend
Kim Alvefur <zash@zash.se>
parents:
diff
changeset
|
919 }; |