Commit Graph

54 Commits

Author SHA1 Message Date
Daniel Sockwell 10fa24c5d3
Benchmark & performance tune (#132)
* Add temporary perf metrics

* Add load testing and tune performance
2020-04-17 17:07:10 -04:00
Daniel Sockwell 37b652ad79
Error handling, pt3 (#131)
* Improve handling of Postgres errors

* Finish error handling improvements

* Remove `format!` calls from hot path
2020-04-14 20:37:49 -04:00
Daniel Sockwell 45f9d4b9fb
Code reorganization (#130)
* Reorganize files

* Refactor main()

* Code reorganization [WIP]

* Reorganize code [WIP]

* Refacto RedisConn [WIP]

* Complete code reorganization
2020-04-13 16:03:06 -04:00
Daniel Sockwell 0eec8f6f7b Remove unneeded dependency 2020-04-10 22:56:19 -04:00
Daniel Sockwell 5d2b0b94e2
Error handling pt2 (#129)
This commit improves error handling in Flodgatt's main request-response loop, including the portions of that loop that were revised in #128.

This nearly completes the addition of more explicit error handling, but there will be a smaller part 3 to bring the handling of configuration/Postgres errors into conformity with the style here.
2020-04-10 22:36:03 -04:00
Daniel Sockwell 1657113c58
Stream events via a watch channel (#128)
This squashed commit makes a fairly significant structural change to significantly reduce Flodgatt's CPU usage.

Flodgatt connects to Redis in a single (green) thread, and then creates a new thread to handle each WebSocket/SSE connection. Previously, each thread was responsible for polling the Redis thread to determine whether it had a message relevant to the connected client. I initially selected this structure both because it was simple and because it minimized memory overhead – no messages are sent to a particular thread unless they are relevant to the client connected to the thread. However, I recently ran some load tests that show this approach to have unacceptable CPU costs when 300+ clients are simultaneously connected.

Accordingly, Flodgatt now uses a different structure: the main Redis thread now announces each incoming message via a watch channel connected to every client thread, and each client thread filters out irrelevant messages. In theory, this could lead to slightly higher memory use, but tests I have run so far have not found a measurable increase. On the other hand, Flodgatt's CPU use is now an order of magnitude lower in tests I've run.

This approach does run a (very slight) risk of dropping messages under extremely heavy load: because a watch channel only stores the most recent message transmitted, if Flodgatt adds a second message before the thread can read the first message, the first message will be overwritten and never transmitted. This seems unlikely to happen in practice, and we can avoid the issue entirely by changing to a broadcast channel when we upgrade to the most recent Tokio version (see #75).
2020-04-09 13:32:36 -04:00
Daniel Sockwell fa8b695129
Minor performance tune (#127)
* Tweak release profile & micro optimizations

* Replace std HashMap with hashbrown::HashMap

The hashbrown::HashMap is faster than the std::collections::HashMap,
though it does not protect as well against malicious hash collisions
(e.g., in a DDoS).  Since we don't expose the hashing externally,
we should switch to the faster implementation.
2020-04-08 18:39:52 -04:00
Daniel Sockwell d2e0a01baf
Stub status (#124)
* Add /status API endpoints [WIP]

* Finish /status API endpoints

This PR enables compiling Flodgatt with the `stub_status` feature.
When compiled with `stub_status`, Flodgatt has 3 new API endpoints:
/api/v1/streaming/status, /api/v1/streaming/status/per_timeline, and
/api/v1/streaming/status/queue.  The first endpoint lists the total
number of connections, the second lists the number of connections per
timeline, and the third lists the length of the longest queue of
unsent messages (which should be low or zero when Flodgatt is
functioning normally).

Note that the number of _connections_ is not equal to the number of
connected _clients_.  If a user is viewing the local timeline, they
would have at least two connections: one for the local timeline, and
one for their user timeline.  Other users could have even more
connections.

I decided to make the status endpoints an option you enable at compile
time rather than at run time for three reasons:

  * It keeps the API of the default version of Flodgatt 100%
    compatible with the Node server's API;

  * I don't beleive it's an option Flodgatt adminstrators will want to
    toggle on and off frequently.

  * Using a compile time option ensures that there is zero runtime
    cost when the option is disabled.  (The runtime cost should be
    negligible either way, but there is value in being 100% sure that
    the cost can be eliminated.)

However, I'm happy to make it a runtime option instead if other think
that would be helpful.
2020-04-05 10:54:42 -04:00
Daniel Sockwell 6a6537253d
Fix `DATABASE_URL` parsing (#122) 2020-04-03 16:35:32 -04:00
Daniel Sockwell 19792d9484
Handle non conforment events (#117)
* Initial implementation of DynamicEvent

* Restore early Event parsing
2020-04-03 12:41:53 -04:00
Daniel Sockwell d5f079a864
Error handling, pt1 (#115)
* Initial work to support structured errors

* WIP error handling and RedisConn refactor

* WIP for error handling refactor

* Finish substantive work for Redis error handling

* Apply clippy lints
2020-04-01 15:35:24 -04:00
Daniel Sockwell 81b454c88c
Extract tests to separate files (#113)
This very minor change moves tests from their current location in
submodules within the file under test into submodules in separate
files.  This is a slight deviation from the normal Rust convention
(though only very slight, since the module structure remains the
same).  However, it is justified here since the tests are fairly
verbose and including them in the same file was a bit unwieldy.
2020-03-31 09:05:51 -04:00
Daniel Sockwell 0acbde3eee
Reorganize code, pt1 (#110)
* Prevent Reciever from querying postgres

Before this commit, the Receiver would query Postgres for the name
associated with a hashtag when it encountered one not in its cache.
This ensured that the Receiver never encountered a (valid) hashtag id
that it couldn't handle, but caused a extra DB query and made
independent sections of the code more entangled than they need to be.

Now, we pass the relevant tag name to the Receiver when it first
starts managing a new subscription and it adds the tag name to its
cache then.

* Improve module boundary/privacy

* Reorganize Receiver to cut RedisStream

* Fix tests for code reorganization

Note that this change includes testing some private functionality by
exposing it publicly in tests via conditional compilation.  This
doesn't expose that functionality for the benchmarks, so the benchmark
tests do not currently pass without adding a few `pub use`
statements.  This might be worth changing later, but benchmark tests
aren't part of our CI and it's not hard to change when we want to test
performance.

This change also cuts the benchmark tests that were benchmarking old
ways Flodgatt functioned.  Those were useful for comparison purposes,
but have served their purpose – we've firmly moved away from the
older/slower approach.

* Fix Receiver for tests
2020-03-27 12:00:48 -04:00
Daniel Sockwell 2dd9ccbf91
Performance tuning (#108)
* Initial implementation WIP

* Add Event type for faster parsing

* Add tests and benchmarks

* Add additional parsing tests
2020-03-25 17:50:32 -04:00
Daniel Sockwell a6b4d968cb
Add support for WHITELIST_MODE (#99)
When the `WHITELIST_MODE` environmental variable is set, Flodgatt
requires users to authenticate with a valid access token before
subscribing to any timelines (even those that are typically public).
2020-03-20 14:42:01 -04:00
Daniel Sockwell 2096e1348b
Add support for announcement event type (#96) 2020-03-19 13:20:48 -04:00
Daniel Sockwell dc6256d521
Fix double-escaping of delete payload (#95) 2020-03-19 12:10:59 -04:00
Daniel Sockwell 8843f18f5f
Fix valid language (#93)
* Fix panic on delete events

Previously, the code attempted to check the toot's language regardless
of event types.  That caused a panic for `delete` events, which lack a
language.

* WIP implementation of Message refactor

* Major refactor

* Refactor scope managment to use enum

* Use Timeline type instead of String

* Clean up Receiver's use of Timeline

* Make debug output more readable

* Block statuses from blocking users

This commit fixes an issue where a status from A would be displayed on
B's public timelines even when A had B blocked (i.e., it would treat B
as though they were muted rather than blocked for the purpose of
public timelines).

* Fix bug with incorrect parsing of incomming timeline

* Disable outdated tests

* Bump version
2020-03-18 20:37:10 -04:00
Daniel Sockwell ed75905fa3
Increase verbosity of debug info (#86)
* Increase verbosity of debug info

* Add email field to User in tests
2020-03-11 11:31:29 -04:00
Daniel Sockwell 405b5e88e5
Update logging (#85)
* Change "Incoming" log msgs from Warn to Info

* Stop logging err when unix socket closed

* Bump version to 0.4.7
2020-01-10 17:56:19 -05:00
Daniel Sockwell ac75cb54af
Postgres connection pool (#84)
* Upgrade rust-postgres library

* Initial postgres connection pool

* Update tests

* s/pg_conn/pg_pool to match reality
2020-01-10 15:45:16 -05:00
Daniel Sockwell 5482be9414
Fix dev dependencies for benchmarks (#82) 2020-01-09 18:17:01 -05:00
Daniel Sockwell 0462267125
Unix sockets (#81)
* Fix unix socket permission issue

* Add support for Unix sockets

* Update README and bump version
2020-01-09 17:54:57 -05:00
Daniel Sockwell 67c59401fd
Unix sockets WIP (#77)
* Initial WIP Unix socket implementation

* Bump version to v0.4.5

* Update type data
2020-01-08 09:51:25 -05:00
Daniel Sockwell bf529a901b
Treat env vars set to "" as if they were unset (#73)
* Treat env vars set to "" as if they were unset

* Bump version to 0.4.4
2020-01-07 13:07:54 -05:00
Daniel Sockwell 07e8776090
Redis hostname (#71)
* Allow hostname in REDIS_HOST

* Bump version to 0.4.3
2020-01-06 14:20:00 -05:00
Daniel Sockwell 0de3d3c484
Postgres config (#70)
* Add logging for known env variables

* Update postgres config to match other configs

* Update README and bump version to 0.4.2
2020-01-05 21:58:18 -05:00
Daniel Sockwell 4a2d08c693
Refactor/reorganize streaming code (#64) 2019-10-09 14:46:56 -04:00
Daniel Sockwell c281418f25
Enforce type safety in config (#63)
* Add type-safe wrapper types to deployement_cfg

* Before deleting redundnat macros

* Store error messages as data

* Significant progress on type safety

* Add type safety to RedisConfig
2019-10-08 20:35:26 -04:00
Daniel Sockwell e19524738b
Add WS keepalive ping (#62)
* Modify code to use forked Warp

* Add keepalive WS ping
2019-10-05 18:18:11 -04:00
Daniel Sockwell 9d96907406
Functional config (#59) 2019-10-03 18:02:23 -04:00
Daniel Sockwell e8145275b5
Config refactor (#57)
* Refactor configuration

* Fix bug with incorrect Host env variable

* Improve logging of REDIS_NAMESPACE

* Update test for Postgres configuration

* Conform Redis config to Postgres changes
2019-10-03 00:34:41 -04:00
Daniel Sockwell 11661d2fdc
Redis config (#56)
* Add most Redis config variables

* Add REDIS_NAMESPACE env var

* Fix Clippy lints
2019-10-02 00:03:18 -04:00
Daniel Sockwell 5b663d110e
Unicode fix (#54)
* Increase detail of error logging; bump version

* Fix panic caused by spliting a message inside a Unicode char
2019-10-01 09:28:43 -04:00
Daniel Sockwell ceb38c2689
Add initial benchmarks (#50) 2019-09-11 17:28:27 -04:00
Daniel Sockwell 0dec8c4124
Solve SendErrors (#47)
This commit solves the SendErrors that were triggered by attempting
to use a WebSocket connection after it had been closed by the client
2019-09-11 00:13:45 -04:00
Daniel Sockwell 11163237bc
Prepare binary release (#46) 2019-09-10 12:11:09 -04:00
Daniel Sockwell 7fb7a3e5c9
Temporarily remove Postgres SSL support (#45)
Remove Postgres SSL support, which was not working at the moment and
was preventing flogatt from running on servers without openssl.

We should re-enable SSL support at a later time.
2019-09-10 11:46:05 -04:00
Daniel Sockwell 0a8abde664
Read `access_token` from WS header (#44) 2019-09-10 08:51:36 -04:00
Daniel Sockwell ecfdda093c
Add tests for websocket routes (#38)
* Refactor organazation of SSE

This commit refactors how SSE requests are handled to bring them into
line with how WS requests are handled and increase consistency.

* Add websocket tests

* Bump version to 0.2.0

Bump version and update name from ragequit to flodgatt.

* Add test for non-existant endpoints

* Update documentation for recent changes``
2019-09-09 13:06:24 -04:00
Daniel Sockwell a27fb0d605
Postgres ssl (#36)
* Upgrade postgres dependency to support ssl

* Clean up configuration code

* Add support for SSL with postgres [WIP]
2019-09-04 21:31:52 -04:00
Daniel Sockwell 866f3ee34d Update documentation and restructure code 2019-07-08 15:21:02 -04:00
Daniel Sockwell d6ae45b292 Code reorganization 2019-07-08 07:31:42 -04:00
Daniel Sockwell 280cc60be9 Add hard-coded "sec-websocket-protocol" response header 2019-07-04 09:33:50 -04:00
Daniel Sockwell 769af09221 Remove outdated files and update dependencies 2019-05-10 06:23:07 -04:00
Daniel Sockwell 9e921c1c97 Add ability for multiple clients to connect to the same pub/sub connection 2019-04-28 17:28:57 -04:00
Daniel Sockwell 13d9cf17f6 Connect to postgres for basic auth 2019-04-18 15:47:08 -04:00
Daniel Sockwell 65fded3eb1 Commit Cargo.lock
Cargo.lock should be tracked with version control for binaries but
I neglected to commit it in the previous commit.
2019-04-18 10:14:46 -04:00
Daniel Sockwell 9434260fa7 Add logging with `pretty_env_log`
This commit adds basic logging at both the `info` level (for establishing
a new stream) and at the `debug` level (for streaming JSON).
2019-04-18 10:10:01 -04:00
Daniel Sockwell 36f9d1d7c1 Add dependencies neccessary for SSE/Warp 2019-04-15 14:48:09 -04:00