monkrb 0.23.1 → 0.24.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +53 -0
- data/README.md +3 -2
- data/lib/monk/generators/presence.rb +22 -0
- data/lib/monk/generators.rb +1 -1
- data/lib/monk/live/client/monk_live.js +159 -7
- data/lib/monk/live/client/protocol.js +37 -0
- data/lib/monk/live/session.rb +59 -2
- data/lib/monk/presence/counts.rb +118 -0
- data/lib/monk/presence/reactor.rb +126 -0
- data/lib/monk/presence/store.rb +271 -0
- data/lib/monk/presence/tracker.rb +111 -0
- data/lib/monk/presence.rb +129 -0
- data/lib/monk/templates/base/Gemfile +1 -1
- data/lib/monk/templates/base/config/load.rb +1 -1
- data/lib/monk/templates/base/config.ru +8 -1
- data/lib/monk/templates/presence/agents.md +29 -0
- data/lib/monk/templates/presence/app/views/presence/_dot.erb +1 -0
- data/lib/monk/templates/presence/config/presence.rb +52 -0
- data/lib/monk/templates/presence/db/migrate/00000000000005_create_presence_tables.down.sql +3 -0
- data/lib/monk/templates/presence/db/migrate/00000000000005_create_presence_tables.up.sql +36 -0
- data/lib/monk/templates/presence/setup.md +51 -0
- data/lib/monk/templates/presence/test/presence_test.rb +13 -0
- data/lib/monk/templates/websocket/bin/websocket_server +29 -13
- data/lib/monk/version.rb +1 -1
- data/lib/monk/websocket/connection.rb +108 -5
- data/lib/monk/websocket/server.rb +38 -12
- metadata +14 -1
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
## presence
|
|
2
|
+
|
|
3
|
+
**Where:** `config/presence.rb` (`Monk::Presence.configure`, `on_change`),
|
|
4
|
+
the dot partial in `app/views/presence/_dot.erb`. Counted in
|
|
5
|
+
`bin/websocket_server`, reacted to in `bin/server` (`config.ru`).
|
|
6
|
+
|
|
7
|
+
**Main calls:** `Monk::Presence.status(subjects)` →
|
|
8
|
+
`{subject => {status: :online | :away | :offline, last_seen_at:}}`, one
|
|
9
|
+
query for all of them; `Monk::Presence.online?(subject)`. Subjects are
|
|
10
|
+
Monk::Auth's (the user's email). `Monk::Presence.on_change { |changes| ... }`
|
|
11
|
+
with a block built in a module, receiving the same hash for the people
|
|
12
|
+
whose status changed.
|
|
13
|
+
|
|
14
|
+
**Test:** `test/presence_test.rb` reads the status of someone with no page
|
|
15
|
+
open.
|
|
16
|
+
|
|
17
|
+
**Pitfalls:**
|
|
18
|
+
- Only signed-in sockets count; a visitor is never online.
|
|
19
|
+
- `on_change` runs in its own Ractor in `bin/server`: build its block in a
|
|
20
|
+
module body; it sees no request, params or session.
|
|
21
|
+
- `on_change` gets a batch: push one Live message per batch (or per 50),
|
|
22
|
+
not one per person, or a burst fills the `:notify` pool and drops
|
|
23
|
+
patches.
|
|
24
|
+
- A change is reported at most once: one that happens while no
|
|
25
|
+
`bin/server` runs is never reported. Render the current status when a
|
|
26
|
+
page loads, and use `on_change` only to update it.
|
|
27
|
+
- Status is read from the primary database, not a replica.
|
|
28
|
+
|
|
29
|
+
**Examples:** `presence-dots` in `config/presence.rb`.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
<span class="presence presence-<%= locals[:status] %>" data-presence="<%= locals[:subject] %>" title="<%= locals[:status] %>"></span>
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
require "monk"
|
|
2
|
+
require "monk/presence"
|
|
3
|
+
require_relative "persistence"
|
|
4
|
+
|
|
5
|
+
# Monk::Presence (monk add presence): whether a signed-in person (a
|
|
6
|
+
# Monk::Auth subject) has a page open. bin/websocket_server counts its
|
|
7
|
+
# sockets and writes them to Postgres; any process reads them. Loaded by
|
|
8
|
+
# config/load.rb and by bin/websocket_server. Monk's
|
|
9
|
+
# docs/guides/presence.md says how it works.
|
|
10
|
+
#
|
|
11
|
+
# Monk::Presence.status(["ann@example.com"])
|
|
12
|
+
# # => {"ann@example.com" => {status: :online, last_seen_at: nil}}
|
|
13
|
+
# Monk::Presence.online?("ann@example.com")
|
|
14
|
+
#
|
|
15
|
+
# :online is a page open and visible, :away only hidden ones (a phone in a
|
|
16
|
+
# pocket), :offline none; last_seen_at is when an offline person was last
|
|
17
|
+
# there. grace: seconds after a person's last page closes before they
|
|
18
|
+
# read as offline, so a phone changing network or a deploy reconnecting
|
|
19
|
+
# every page changes nobody's status.
|
|
20
|
+
Monk::Presence.configure(db_name: :primary, grace: 20)
|
|
21
|
+
|
|
22
|
+
# on_change runs in bin/server (config.ru starts it) each time some
|
|
23
|
+
# people's status changes, with all of them at once:
|
|
24
|
+
# {subject => {status:, last_seen_at:}}. At most once per change: one
|
|
25
|
+
# missed while no bin/server runs isn't sent later, and a Live page
|
|
26
|
+
# catches up when it reconnects. Its block must be Ractor-shareable, so
|
|
27
|
+
# it's built in a module (self at the top of this file is not).
|
|
28
|
+
#
|
|
29
|
+
# monk:example presence-dots -- push each change to the pages showing those people (needs live).
|
|
30
|
+
# # A page wraps its people in <div <%= live_topic "presence" %>> and shows
|
|
31
|
+
# # each with <%= render "presence/_dot", subject: email, status: ..., layout: false %>.
|
|
32
|
+
# # One message per 50 changes, not one per person: a burst stays within
|
|
33
|
+
# # the WebSocket transport's limits (8 KB a message over Postgres).
|
|
34
|
+
# module AppPresence
|
|
35
|
+
# CHANGED = proc do |changes|
|
|
36
|
+
# changes.each_slice(50) do |slice|
|
|
37
|
+
# Monk::Live.batch("presence") do |batch|
|
|
38
|
+
# slice.each do |subject, change|
|
|
39
|
+
# batch.patch(to: AppPresence.dots(subject), partial: "presence/_dot", subject: subject,
|
|
40
|
+
# status: change[:status],)
|
|
41
|
+
# end
|
|
42
|
+
# end
|
|
43
|
+
# end
|
|
44
|
+
# end
|
|
45
|
+
#
|
|
46
|
+
# # Every dot of one person on a page, as a CSS selector.
|
|
47
|
+
# def self.dots(subject) = %([data-presence="#{subject.gsub(/["\\]/) { "\\#{it}" }}"])
|
|
48
|
+
# end
|
|
49
|
+
# Monk::Presence.on_change(&AppPresence::CHANGED)
|
|
50
|
+
# # Signed-in people may see everyone's dots: narrow it to what your pages show.
|
|
51
|
+
# Monk::Live.authorize("presence", &Monk::Live::ALLOW_ALL)
|
|
52
|
+
# monk:end
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
-- Monk::Presence: who has a socket open, per WebSocket process, and each
|
|
2
|
+
-- subject's settled status. Why this shape:
|
|
3
|
+
-- docs/adr/0019-presence-per-process-rows-in-postgres-settled-once-per-transition.md.
|
|
4
|
+
|
|
5
|
+
-- One row per bin/websocket_server process, heartbeat every few seconds:
|
|
6
|
+
-- a process whose heartbeat stops stops counting its subjects. UNLOGGED,
|
|
7
|
+
-- as monk_presence: after a Postgres crash both come back empty, and
|
|
8
|
+
-- each process writes its own again.
|
|
9
|
+
CREATE UNLOGGED TABLE monk_presence_processes (
|
|
10
|
+
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
|
|
11
|
+
hostname TEXT NOT NULL,
|
|
12
|
+
pid INTEGER NOT NULL,
|
|
13
|
+
heartbeat_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
14
|
+
);
|
|
15
|
+
|
|
16
|
+
-- One row per (process, subject) with a socket open there, or closed
|
|
17
|
+
-- less than the grace ago (gone_at). Rewritten all the time, so not
|
|
18
|
+
-- worth the WAL. No foreign key: a process row pruned first just means
|
|
19
|
+
-- its rows stop counting.
|
|
20
|
+
CREATE UNLOGGED TABLE monk_presence (
|
|
21
|
+
process_id BIGINT NOT NULL,
|
|
22
|
+
subject TEXT NOT NULL,
|
|
23
|
+
status TEXT NOT NULL CHECK (status IN ('online', 'away')),
|
|
24
|
+
gone_at TIMESTAMPTZ,
|
|
25
|
+
PRIMARY KEY (process_id, subject)
|
|
26
|
+
);
|
|
27
|
+
CREATE INDEX monk_presence_subject ON monk_presence (subject);
|
|
28
|
+
|
|
29
|
+
-- Each subject's last settled status, written once per transition, and
|
|
30
|
+
-- when it was last seen: shown to users, so logged.
|
|
31
|
+
CREATE TABLE monk_presence_status (
|
|
32
|
+
subject TEXT PRIMARY KEY,
|
|
33
|
+
status TEXT NOT NULL CHECK (status IN ('online', 'away', 'offline')),
|
|
34
|
+
changed_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
35
|
+
last_seen_at TIMESTAMPTZ
|
|
36
|
+
);
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
## presence
|
|
2
|
+
|
|
3
|
+
Whether a signed-in person has a page open: online, away (only hidden
|
|
4
|
+
pages, like a phone in a pocket) or offline. `bin/websocket_server`
|
|
5
|
+
counts its sockets and writes them to Postgres every few seconds;
|
|
6
|
+
`Monk::Presence.status` reads them from any process. A migration
|
|
7
|
+
creates the `monk_presence*` tables: run `bin/setup_db` and
|
|
8
|
+
`DB_NAME={{app}}_test bin/setup_db` (postgres, above).
|
|
9
|
+
|
|
10
|
+
Only signed-in sockets count (auth, above). With live, a page counts as
|
|
11
|
+
away until its first ping, a moment after it opens.
|
|
12
|
+
|
|
13
|
+
### Reacting to a change
|
|
14
|
+
|
|
15
|
+
`Monk::Presence.on_change` in `config/presence.rb` runs in `bin/server`
|
|
16
|
+
each time some people's status changes: `config.ru` starts it. Among
|
|
17
|
+
several `bin/server` processes, one reacts at a time; if it stops,
|
|
18
|
+
another takes over within seconds. The example `presence-dots` pushes
|
|
19
|
+
each change to the pages showing those people, with live.
|
|
20
|
+
|
|
21
|
+
### Before production
|
|
22
|
+
|
|
23
|
+
- The process reacting to changes holds its own Postgres connection,
|
|
24
|
+
and needs a direct one: not through PgBouncer in transaction mode.
|
|
25
|
+
Each `bin/server` process opens one more, plus one for reading the
|
|
26
|
+
statuses when it reacts.
|
|
27
|
+
- A person stays as they were for `grace:` seconds (20) after their last
|
|
28
|
+
page closes, so a deploy restarting `bin/websocket_server` changes
|
|
29
|
+
nobody's status.
|
|
30
|
+
|
|
31
|
+
### Check it works
|
|
32
|
+
|
|
33
|
+
1. `bundle exec rake test` runs `test/presence_test.rb`: someone with no
|
|
34
|
+
page open reads as offline.
|
|
35
|
+
2. By hand, with `bin/server` and `bin/websocket_server` running: log in
|
|
36
|
+
(auth, above) and open a page that connects to the WebSocket (with
|
|
37
|
+
live, any page with `live_topic`). Then, in `bin/console`:
|
|
38
|
+
|
|
39
|
+
```ruby
|
|
40
|
+
Monk::Presence.status(["you@example.com"])
|
|
41
|
+
# => {"you@example.com" => {status: :online, last_seen_at: nil}}
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
Hide the tab (or lock the phone) and it reads `:away` within a
|
|
45
|
+
second or two; close it and, after the grace, `:offline` with
|
|
46
|
+
`last_seen_at`. A locked iPhone goes `:offline` too, about 25 s after
|
|
47
|
+
locking: iOS drops a locked page's connection.
|
|
48
|
+
3. The example: uncomment `presence-dots` in `config/presence.rb`, put
|
|
49
|
+
`<div <%= live_topic "presence" %>><%= render "presence/_dot", subject: "you@example.com", status: :offline, layout: false %></div>`
|
|
50
|
+
in a page, and restart both servers. Open it in one browser, and log
|
|
51
|
+
in as that person in another: the dot changes by itself.
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
require_relative "test_helper"
|
|
2
|
+
|
|
3
|
+
# Monk::Presence reads each person's status from the tables its migration
|
|
4
|
+
# made (bin/setup_db). Someone with no page open is offline, never seen.
|
|
5
|
+
# SETUP.md, presence.
|
|
6
|
+
class PresenceTest < Minitest::Test
|
|
7
|
+
def test_someone_with_no_page_open_is_offline
|
|
8
|
+
subject = "presence-check-#{rand(1_000_000)}@example.test"
|
|
9
|
+
|
|
10
|
+
assert_equal({ subject => { status: :offline, last_seen_at: nil } }, Monk::Presence.status([subject]))
|
|
11
|
+
refute Monk::Presence.online?(subject)
|
|
12
|
+
end
|
|
13
|
+
end
|
|
@@ -22,8 +22,8 @@ Monk::Bin.run(__FILE__) do
|
|
|
22
22
|
require_relative "../config/websocket"
|
|
23
23
|
|
|
24
24
|
# Each only if this app has it: auth says whose a socket is, live what it
|
|
25
|
-
# may subscribe to.
|
|
26
|
-
%w[auth live].each do |name|
|
|
25
|
+
# may subscribe to, presence whether this process counts who is online.
|
|
26
|
+
%w[auth live presence].each do |name|
|
|
27
27
|
path = File.expand_path("../config/#{name}.rb", __dir__)
|
|
28
28
|
require path if File.exist?(path)
|
|
29
29
|
end
|
|
@@ -61,16 +61,32 @@ Monk::Bin.run(__FILE__) do
|
|
|
61
61
|
db_pool = (:auth if authenticate)
|
|
62
62
|
Monk::Persistence::Pg.start_pools!(db_pool) if db_pool
|
|
63
63
|
|
|
64
|
-
|
|
65
|
-
#
|
|
66
|
-
#
|
|
67
|
-
|
|
64
|
+
# With presence (monk add presence), this process counts its signed-in
|
|
65
|
+
# sockets and writes them to Postgres every few seconds, through a
|
|
66
|
+
# connection of its own. With live, a socket counts as a hidden page
|
|
67
|
+
# until its page's first ping.
|
|
68
|
+
presence = (Monk::Presence.start!(start_visible: !live) if defined?(Monk::Presence) && authenticate)
|
|
69
|
+
begin
|
|
70
|
+
port = ENV.fetch("WS_PORT", "9293").to_i
|
|
71
|
+
# Defaults to public_url (config/settings.rb): one PUBLIC_URL keeps both
|
|
72
|
+
# in sync. More than one origin: comma-separated.
|
|
73
|
+
allowed_origins = ENV.fetch("WS_ALLOWED_ORIGINS", Monk::Settings[:public_url]).split(",")
|
|
68
74
|
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
75
|
+
# A ping every 25 s keeps a quiet socket open through a proxy's idle
|
|
76
|
+
# timeout (nginx's is 60 s), and a socket that answers nothing for two
|
|
77
|
+
# of them (a phone that changed network) is closed instead of held.
|
|
78
|
+
server = Monk::WebSocket::Server.new(
|
|
79
|
+
port: port, authenticate: authenticate, allowed_origins: allowed_origins, db_pool: db_pool,
|
|
80
|
+
ping_interval: 25, reuse_port: processes > 1, presence: presence,
|
|
81
|
+
)
|
|
82
|
+
puts "Monk::WebSocket::Server listening on ws://0.0.0.0:#{port} " \
|
|
83
|
+
"(#{live ? "live" : "app/sockets"}, transport: #{AppWebSocket::TRANSPORT}, authenticate: #{authenticate}" \
|
|
84
|
+
"#{", presence" if presence})"
|
|
85
|
+
server.run(&handler)
|
|
86
|
+
ensure
|
|
87
|
+
# On TERM (a deploy): this process's people stay as they were for the
|
|
88
|
+
# grace, while their pages reconnect to another process. Also when the
|
|
89
|
+
# server couldn't start (its port in use), so presence stops with it.
|
|
90
|
+
Monk::Presence.stop! if presence
|
|
91
|
+
end
|
|
76
92
|
end
|
data/lib/monk/version.rb
CHANGED
|
@@ -5,5 +5,5 @@ module Monk
|
|
|
5
5
|
# class of bug .freeze! guards against for routes/error handlers/models,
|
|
6
6
|
# just on a top-level constant that every worker Ractor reads on boot
|
|
7
7
|
# (found live under kino: GET /hello 500'd until this was frozen).
|
|
8
|
-
VERSION = "0.
|
|
8
|
+
VERSION = "0.24.0".freeze
|
|
9
9
|
end
|
|
@@ -14,16 +14,27 @@ module Monk
|
|
|
14
14
|
# each just under the per-frame cap would silently bypass it.
|
|
15
15
|
DEFAULT_MAX_PAYLOAD_SIZE = 1 * 1024 * 1024
|
|
16
16
|
|
|
17
|
+
# With a heartbeat (#start_heartbeat), a connection that sends
|
|
18
|
+
# nothing for this many ping intervals is taken for gone and closed:
|
|
19
|
+
# one ping unanswered is allowed, two aren't.
|
|
20
|
+
IDLE_PINGS = 2
|
|
21
|
+
|
|
17
22
|
# subject is whatever Monk::Auth.verify returned when
|
|
18
23
|
# Server.new(authenticate: true) verified this handshake
|
|
19
24
|
# (docs/history/plan-websocket.md Phase 5) -- nil for an unauthenticated server.
|
|
20
25
|
attr_reader :subject
|
|
21
26
|
|
|
22
|
-
def
|
|
27
|
+
def self.monotonic = Process.clock_gettime(Process::CLOCK_MONOTONIC)
|
|
28
|
+
|
|
29
|
+
# presence: is a Monk::Presence::Counts this connection reports to
|
|
30
|
+
# (Server.new(presence:)), used only for a connection with a subject.
|
|
31
|
+
def initialize(socket, subject: nil, max_payload_size: DEFAULT_MAX_PAYLOAD_SIZE, presence: nil)
|
|
23
32
|
@socket = socket
|
|
24
33
|
@subject = subject
|
|
34
|
+
@presence = presence if subject
|
|
25
35
|
@write_mutex = Mutex.new
|
|
26
36
|
@max_payload_size = max_payload_size
|
|
37
|
+
@last_read_at = Connection.monotonic
|
|
27
38
|
end
|
|
28
39
|
|
|
29
40
|
# Reassembles a fragmented message per RFC 6455 5.4: a frame with
|
|
@@ -59,8 +70,9 @@ module Monk
|
|
|
59
70
|
# payload back, per RFC 6455 5.5.3.
|
|
60
71
|
write_frame(frame[:payload], 0xA)
|
|
61
72
|
when 0xA
|
|
62
|
-
#
|
|
63
|
-
#
|
|
73
|
+
# A pong: read_frame has already noted that the peer is
|
|
74
|
+
# there (#start_heartbeat's idle deadline), and it's not a
|
|
75
|
+
# message for app code.
|
|
64
76
|
when 0x0
|
|
65
77
|
return close_with(1002, "unexpected continuation frame") unless fragments
|
|
66
78
|
|
|
@@ -141,11 +153,30 @@ module Monk
|
|
|
141
153
|
# no app-level JS involved) answers a server-sent ping with a pong
|
|
142
154
|
# automatically; #read already discards an incoming pong silently
|
|
143
155
|
# either way, so nothing app-visible changes.
|
|
156
|
+
#
|
|
157
|
+
# The same thread closes a connection that has sent nothing, not even
|
|
158
|
+
# a pong, for IDLE_PINGS intervals (docs/plan-live-networks-presence.md,
|
|
159
|
+
# A1): a peer that vanished without a FIN or an RST (a phone that
|
|
160
|
+
# changed network) never ends the read on its own. With nothing to
|
|
161
|
+
# send, the kernel never notices; with a ping pending, Linux gives up
|
|
162
|
+
# after about 15 minutes. It sleeps until the earlier of its next ping
|
|
163
|
+
# and that deadline, so the close comes at IDLE_PINGS intervals, not
|
|
164
|
+
# up to one more.
|
|
144
165
|
def start_heartbeat(interval)
|
|
166
|
+
idle_timeout = interval * IDLE_PINGS
|
|
145
167
|
@heartbeat_thread = Thread.new do
|
|
168
|
+
next_ping = Connection.monotonic + interval
|
|
146
169
|
loop do
|
|
147
|
-
|
|
148
|
-
|
|
170
|
+
now = Connection.monotonic
|
|
171
|
+
deadline = @last_read_at + idle_timeout
|
|
172
|
+
break drop_silent_peer if now >= deadline
|
|
173
|
+
|
|
174
|
+
if now >= next_ping
|
|
175
|
+
send_ping
|
|
176
|
+
@on_heartbeat&.call
|
|
177
|
+
next_ping = now + interval
|
|
178
|
+
end
|
|
179
|
+
sleep [[next_ping, deadline].min - Connection.monotonic, 0].max
|
|
149
180
|
end
|
|
150
181
|
rescue IOError, Errno::EPIPE, Errno::ECONNRESET
|
|
151
182
|
# The socket closed out from under this loop -- #close or a
|
|
@@ -156,14 +187,84 @@ module Monk
|
|
|
156
187
|
end
|
|
157
188
|
end
|
|
158
189
|
|
|
190
|
+
# A block run on the heartbeat thread after each ping, in this
|
|
191
|
+
# connection's Ractor: periodic work for a handler without a thread
|
|
192
|
+
# of its own per connection (Live::Session's check for a silent
|
|
193
|
+
# page). Never runs without ping_interval:.
|
|
194
|
+
def on_heartbeat(&block)
|
|
195
|
+
@on_heartbeat = block
|
|
196
|
+
end
|
|
197
|
+
|
|
159
198
|
# Called unconditionally from Server.serve's ensure, alongside
|
|
160
199
|
# #unsubscribe! -- a no-op if #start_heartbeat was never called.
|
|
161
200
|
def stop_heartbeat!
|
|
162
201
|
@heartbeat_thread&.kill
|
|
163
202
|
end
|
|
164
203
|
|
|
204
|
+
# Presence (docs/adr/0019-...): whether this connection is counted
|
|
205
|
+
# for its subject. Server.serve reports the open after the 101 and
|
|
206
|
+
# the close in its ensure; a handler that knows whether its page is
|
|
207
|
+
# visible (Live::Session, from its client's pings) sets #visible=.
|
|
208
|
+
# Each is a send to the counts, never a wait, keyed by this
|
|
209
|
+
# connection's own Ractor.
|
|
210
|
+
def tracks_presence? = !@presence.nil?
|
|
211
|
+
|
|
212
|
+
def presence_open!
|
|
213
|
+
@presence&.open(Ractor.current, @subject.to_s)
|
|
214
|
+
end
|
|
215
|
+
|
|
216
|
+
def presence_close!
|
|
217
|
+
@presence&.close(Ractor.current)
|
|
218
|
+
end
|
|
219
|
+
|
|
220
|
+
# Reported only when it changes.
|
|
221
|
+
def visible=(visible)
|
|
222
|
+
return if @visible == visible
|
|
223
|
+
|
|
224
|
+
@visible = visible
|
|
225
|
+
@presence&.visible(Ractor.current, visible)
|
|
226
|
+
end
|
|
227
|
+
|
|
165
228
|
private
|
|
166
229
|
|
|
230
|
+
# A ping that can't go out at once is skipped, not waited for: on a
|
|
231
|
+
# dead connection the kernel's send buffer fills, a writer (a relay
|
|
232
|
+
# with patches queued) blocks inside write_frame holding the mutex,
|
|
233
|
+
# and a heartbeat waiting behind it would never reach its deadline.
|
|
234
|
+
# A ping cut in half (the buffer had room for one byte) would corrupt
|
|
235
|
+
# the stream, so that connection is dropped instead.
|
|
236
|
+
#
|
|
237
|
+
# A ping that fails with a network error (Errno::EHOSTDOWN on macOS,
|
|
238
|
+
# EHOSTUNREACH on Linux, once the peer's network is gone) is skipped
|
|
239
|
+
# too: ending the heartbeat there would also end the close after
|
|
240
|
+
# IDLE_PINGS silent intervals, the one thing that still frees a
|
|
241
|
+
# connection whose peer vanished. A closed socket (IOError) still
|
|
242
|
+
# ends it.
|
|
243
|
+
def send_ping
|
|
244
|
+
return unless @write_mutex.try_lock
|
|
245
|
+
|
|
246
|
+
begin
|
|
247
|
+
ping = Monk::WebSocket::Frame.encode("", opcode: 0x9)
|
|
248
|
+
written = @socket.write_nonblock(ping, exception: false)
|
|
249
|
+
drop_silent_peer if written.is_a?(Integer) && written < ping.bytesize
|
|
250
|
+
rescue SystemCallError
|
|
251
|
+
# Skipped; the deadline decides.
|
|
252
|
+
ensure
|
|
253
|
+
@write_mutex.unlock
|
|
254
|
+
end
|
|
255
|
+
end
|
|
256
|
+
|
|
257
|
+
# Shut down, without a close frame and without the write mutex: the
|
|
258
|
+
# peer is taken for gone, and a writer may be blocked holding the
|
|
259
|
+
# mutex on its full send buffer. shutdown makes that write raise and
|
|
260
|
+
# the handler's read return nil, and Server.serve's ensure cleans up
|
|
261
|
+
# as after any other close.
|
|
262
|
+
def drop_silent_peer
|
|
263
|
+
@socket.shutdown(:RDWR)
|
|
264
|
+
rescue IOError, SystemCallError
|
|
265
|
+
# Already closed or gone.
|
|
266
|
+
end
|
|
267
|
+
|
|
167
268
|
def relay_broadcasts
|
|
168
269
|
loop do
|
|
169
270
|
message = @port.receive
|
|
@@ -197,6 +298,8 @@ module Monk
|
|
|
197
298
|
header = read_exactly(2)
|
|
198
299
|
return nil unless header
|
|
199
300
|
|
|
301
|
+
# Any frame shows the peer is there: data, a ping, a pong.
|
|
302
|
+
@last_read_at = Connection.monotonic
|
|
200
303
|
byte1 = header.getbyte(1)
|
|
201
304
|
masked = byte1.anybits?(0x80)
|
|
202
305
|
length_indicator = byte1 & 0x7F
|
|
@@ -17,7 +17,12 @@ module Monk
|
|
|
17
17
|
# ping_interval: (seconds) opts a connection into a server-sent
|
|
18
18
|
# heartbeat ping -- see Connection#start_heartbeat. Defaults to nil
|
|
19
19
|
# (disabled) for the same reason authenticate: defaults false: a
|
|
20
|
-
# server that doesn't ask for it behaves exactly as before.
|
|
20
|
+
# server that doesn't ask for it behaves exactly as before. It also
|
|
21
|
+
# closes a connection silent for Connection::IDLE_PINGS intervals,
|
|
22
|
+
# so it can't be under MIN_PING_INTERVAL: reads stall during a GC
|
|
23
|
+
# pause or a slow db_pool: rule, and a timeout near that would close
|
|
24
|
+
# healthy sockets in bulk and start a reconnect wave
|
|
25
|
+
# (docs/plan-live-networks-presence.md, open question 2).
|
|
21
26
|
#
|
|
22
27
|
# reverify_interval: (seconds) re-runs Monk::Auth.verify against the
|
|
23
28
|
# same credential on that cadence for as long as the connection
|
|
@@ -81,8 +86,16 @@ module Monk
|
|
|
81
86
|
# heap, so its own GC pauses, over a share of the sockets: on Ruby 4.0
|
|
82
87
|
# K processes cut what GC costs a connect by about K squared
|
|
83
88
|
# (item 8). A broadcast reaches all of them through the fan-out.
|
|
89
|
+
#
|
|
90
|
+
# presence: a Monk::Presence::Counts (Monk::Presence.start!), told
|
|
91
|
+
# when each connection with a subject opens (after the 101) and
|
|
92
|
+
# closes, with a send, so a connect never waits on it
|
|
93
|
+
# (docs/adr/0019-presence-per-process-rows-in-postgres-settled-once-per-transition.md).
|
|
94
|
+
# Requires authenticate:: only Monk::Auth subjects are tracked.
|
|
84
95
|
AUTHENTICATE_MODES = [false, true, :optional].freeze
|
|
85
96
|
|
|
97
|
+
MIN_PING_INTERVAL = 5
|
|
98
|
+
|
|
86
99
|
DEFAULT_MAX_HANDSHAKES = 128
|
|
87
100
|
DEFAULT_HANDSHAKE_TIMEOUT = 10
|
|
88
101
|
|
|
@@ -97,10 +110,14 @@ module Monk
|
|
|
97
110
|
def initialize(
|
|
98
111
|
port:, bind: "0.0.0.0", allowed_origins: nil, authenticate: false, ping_interval: nil,
|
|
99
112
|
reverify_interval: nil, max_payload_size: Connection::DEFAULT_MAX_PAYLOAD_SIZE, db_pool: nil,
|
|
100
|
-
max_handshakes: DEFAULT_MAX_HANDSHAKES, handshake_timeout: DEFAULT_HANDSHAKE_TIMEOUT, reuse_port: false
|
|
113
|
+
max_handshakes: DEFAULT_MAX_HANDSHAKES, handshake_timeout: DEFAULT_HANDSHAKE_TIMEOUT, reuse_port: false,
|
|
114
|
+
presence: nil
|
|
101
115
|
)
|
|
102
|
-
if ping_interval && !ping_interval.
|
|
103
|
-
raise ArgumentError,
|
|
116
|
+
if ping_interval && !(ping_interval.is_a?(Numeric) && ping_interval >= MIN_PING_INTERVAL)
|
|
117
|
+
raise ArgumentError,
|
|
118
|
+
"ping_interval must be at least #{MIN_PING_INTERVAL} (seconds), got #{ping_interval.inspect}: " \
|
|
119
|
+
"a connection silent for #{Connection::IDLE_PINGS} intervals is closed, and a shorter one would " \
|
|
120
|
+
"close healthy sockets during a GC pause"
|
|
104
121
|
end
|
|
105
122
|
|
|
106
123
|
unless AUTHENTICATE_MODES.include?(authenticate)
|
|
@@ -115,6 +132,11 @@ module Monk
|
|
|
115
132
|
end
|
|
116
133
|
|
|
117
134
|
raise ArgumentError, "db_pool requires authenticate: true or :optional" if db_pool && !authenticate
|
|
135
|
+
raise ArgumentError, "presence requires authenticate: true or :optional" if presence && !authenticate
|
|
136
|
+
# A Live page that went silent turns hidden on the heartbeat
|
|
137
|
+
# (Connection#on_heartbeat), and a socket whose peer vanished is
|
|
138
|
+
# only closed, and stops counting, through it.
|
|
139
|
+
raise ArgumentError, "presence requires ping_interval:" if presence && !ping_interval
|
|
118
140
|
|
|
119
141
|
unless max_payload_size.positive?
|
|
120
142
|
raise ArgumentError, "max_payload_size must be positive, got #{max_payload_size.inspect}"
|
|
@@ -158,6 +180,7 @@ module Monk
|
|
|
158
180
|
@db_pool = db_pool
|
|
159
181
|
@max_handshakes = max_handshakes
|
|
160
182
|
@handshake_timeout = handshake_timeout
|
|
183
|
+
@presence = presence && Ractor.make_shareable(presence)
|
|
161
184
|
end
|
|
162
185
|
|
|
163
186
|
def port
|
|
@@ -217,13 +240,14 @@ module Monk
|
|
|
217
240
|
socket = @tcp_server.accept
|
|
218
241
|
ractor = Ractor.new(
|
|
219
242
|
shareable_block, @authenticate, @allowed_origins, @ping_interval, @reverify_interval, @max_payload_size,
|
|
220
|
-
@db_pool, handshake_done, @handshake_timeout
|
|
221
|
-
) do |blk, authenticate, origins, ping_interval, reverify_interval, max_payload_size, db_pool, done, timeout
|
|
243
|
+
@db_pool, handshake_done, @handshake_timeout, @presence
|
|
244
|
+
) do |blk, authenticate, origins, ping_interval, reverify_interval, max_payload_size, db_pool, done, timeout,
|
|
245
|
+
presence|
|
|
222
246
|
Monk::WebSocket::Server.serve(
|
|
223
247
|
Ractor.receive, blk,
|
|
224
248
|
authenticate: authenticate, allowed_origins: origins, ping_interval: ping_interval,
|
|
225
249
|
reverify_interval: reverify_interval, max_payload_size: max_payload_size, db_pool: db_pool,
|
|
226
|
-
handshake_done: done, handshake_timeout: timeout
|
|
250
|
+
handshake_done: done, handshake_timeout: timeout, presence: presence
|
|
227
251
|
)
|
|
228
252
|
end
|
|
229
253
|
ractor.send(socket, move: true)
|
|
@@ -261,7 +285,7 @@ module Monk
|
|
|
261
285
|
def self.serve(
|
|
262
286
|
socket, block, authenticate: false, allowed_origins: nil, ping_interval: nil, reverify_interval: nil,
|
|
263
287
|
max_payload_size: Connection::DEFAULT_MAX_PAYLOAD_SIZE, db_pool: nil, handshake_done: nil,
|
|
264
|
-
handshake_timeout: DEFAULT_HANDSHAKE_TIMEOUT
|
|
288
|
+
handshake_timeout: DEFAULT_HANDSHAKE_TIMEOUT, presence: nil
|
|
265
289
|
)
|
|
266
290
|
# A `return` in here (a refused handshake) ends serve, and its
|
|
267
291
|
# ensure frees the slot.
|
|
@@ -297,7 +321,8 @@ module Monk
|
|
|
297
321
|
end
|
|
298
322
|
handshake_done = handshake_over(handshake_done)
|
|
299
323
|
|
|
300
|
-
connection = Connection.new(socket, subject: subject, max_payload_size: max_payload_size)
|
|
324
|
+
connection = Connection.new(socket, subject: subject, max_payload_size: max_payload_size, presence: presence)
|
|
325
|
+
connection.presence_open!
|
|
301
326
|
connection.start_heartbeat(ping_interval) if ping_interval
|
|
302
327
|
if reverify_interval && subject
|
|
303
328
|
reverify_thread = start_reverify(connection, credential[:token], reverify_interval, db_pool)
|
|
@@ -322,6 +347,7 @@ module Monk
|
|
|
322
347
|
# A handshake that never reached its 101 still frees its slot.
|
|
323
348
|
handshake_over(handshake_done)
|
|
324
349
|
reverify_thread&.kill
|
|
350
|
+
connection&.presence_close!
|
|
325
351
|
connection&.unsubscribe!
|
|
326
352
|
connection&.stop_heartbeat!
|
|
327
353
|
socket.close
|
|
@@ -464,10 +490,10 @@ module Monk
|
|
|
464
490
|
break unless verify(raw_token, db_pool)
|
|
465
491
|
end
|
|
466
492
|
connection.close(code: 1008, reason: "session no longer valid")
|
|
467
|
-
rescue IOError,
|
|
493
|
+
rescue IOError, SystemCallError
|
|
468
494
|
# The connection already closed via some other path (normal
|
|
469
|
-
# disconnect, the close handshake, a dead ping)
|
|
470
|
-
# to close.
|
|
495
|
+
# disconnect, the close handshake, a dead ping), or its peer's
|
|
496
|
+
# network is gone (Errno::EHOSTDOWN): nothing left to close.
|
|
471
497
|
end
|
|
472
498
|
end
|
|
473
499
|
private_class_method :start_reverify
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: monkrb
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.24.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Matteo Folin
|
|
@@ -205,6 +205,7 @@ files:
|
|
|
205
205
|
- lib/monk/generators/live.rb
|
|
206
206
|
- lib/monk/generators/mail.rb
|
|
207
207
|
- lib/monk/generators/postgres.rb
|
|
208
|
+
- lib/monk/generators/presence.rb
|
|
208
209
|
- lib/monk/generators/redis.rb
|
|
209
210
|
- lib/monk/generators/storage.rb
|
|
210
211
|
- lib/monk/generators/websocket.rb
|
|
@@ -250,6 +251,11 @@ files:
|
|
|
250
251
|
- lib/monk/persistence/pg/migrator.rb
|
|
251
252
|
- lib/monk/persistence/pg/model.rb
|
|
252
253
|
- lib/monk/persistence/pool.rb
|
|
254
|
+
- lib/monk/presence.rb
|
|
255
|
+
- lib/monk/presence/counts.rb
|
|
256
|
+
- lib/monk/presence/reactor.rb
|
|
257
|
+
- lib/monk/presence/store.rb
|
|
258
|
+
- lib/monk/presence/tracker.rb
|
|
253
259
|
- lib/monk/settings.rb
|
|
254
260
|
- lib/monk/state_ractor.rb
|
|
255
261
|
- lib/monk/storage.rb
|
|
@@ -343,6 +349,13 @@ files:
|
|
|
343
349
|
- lib/monk/templates/postgres/db/migrate/.keep
|
|
344
350
|
- lib/monk/templates/postgres/setup.md
|
|
345
351
|
- lib/monk/templates/postgres/test/persistence_test.rb
|
|
352
|
+
- lib/monk/templates/presence/agents.md
|
|
353
|
+
- lib/monk/templates/presence/app/views/presence/_dot.erb
|
|
354
|
+
- lib/monk/templates/presence/config/presence.rb
|
|
355
|
+
- lib/monk/templates/presence/db/migrate/00000000000005_create_presence_tables.down.sql
|
|
356
|
+
- lib/monk/templates/presence/db/migrate/00000000000005_create_presence_tables.up.sql
|
|
357
|
+
- lib/monk/templates/presence/setup.md
|
|
358
|
+
- lib/monk/templates/presence/test/presence_test.rb
|
|
346
359
|
- lib/monk/templates/redis/agents.md
|
|
347
360
|
- lib/monk/templates/redis/app/routes/redis.rb
|
|
348
361
|
- lib/monk/templates/redis/config/redis.rb
|