2020-04-22 20:38:22 +02:00
|
|
|
use crate::request;
|
2020-04-01 21:35:24 +02:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum RedisConnErr {
|
|
|
|
ConnectionErr { addr: String, inner: std::io::Error },
|
|
|
|
InvalidRedisReply(String),
|
|
|
|
UnknownRedisErr(std::io::Error),
|
|
|
|
IncorrectPassword(String),
|
|
|
|
MissingPassword,
|
|
|
|
NotRedis(String),
|
2020-04-22 20:38:22 +02:00
|
|
|
TimelineErr(request::TimelineErr),
|
2020-04-01 21:35:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RedisConnErr {
|
2020-04-22 20:38:22 +02:00
|
|
|
pub(super) fn with_addr<T: AsRef<str>>(address: T, inner: std::io::Error) -> Self {
|
2020-04-01 21:35:24 +02:00
|
|
|
Self::ConnectionErr {
|
|
|
|
addr: address.as_ref().to_string(),
|
|
|
|
inner,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for RedisConnErr {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
use RedisConnErr::*;
|
|
|
|
let msg = match self {
|
|
|
|
ConnectionErr { addr, inner } => format!(
|
|
|
|
"Error connecting to Redis at {}.\n\
|
|
|
|
Connection Error: {}",
|
|
|
|
addr, inner
|
|
|
|
),
|
|
|
|
InvalidRedisReply(unexpected_reply) => format!(
|
Improve handling of large Redis input (#143)
* Implement faster buffered input
This commit implements a modified ring buffer for input from Redis.
Specifically, Flodgatt now limits the amount of data it fetches from
Redis in one syscall to 8 KiB (two pages on most systems). Flodgatt
will process all complete messages it receives from Redis and then
re-use the same buffer for the next time it retrieves data. If
Flodgatt received a partial message, it will copy the partial message
to the beginning of the buffer before its next read.
This change has little effect on Flodgatt under light load (because it
was rare for Redis to have more than 8 KiB of messages available at
any one time). However, my hope is that this will significantly
reduce memory use on the largest instances.
* Improve handling of backpresure
This commit alters how Flodgatt behaves if it receives enough messages
for a single client to fill that clients channel. (Because the clients
regularly send their messages, should only occur if a single client
receives a large number of messages nearly simultaneously; this is
rare, but could occur, especially on large instances).
Previously, Flodgatt would drop messages in the rare case when the
client's channel was full. Now, Flodgatt will pause the current Redis
poll and yield control back to the client streams, allowing the
clients to empty their channels; Flodgatt will then resume polling
Redis/sending the messages it previously received. With the approach,
Flodgatt will never drop messages.
However, the risk to this approach is that, by never dropping
messages, Flodgatt does not have any way to reduce the amount of work
it needs to do when under heavy load – it delays the work slightly,
but doesn't reduce it. What this means is that it would be
*theoretically* possible for Flodgatt to fall increasingly behind, if
it is continuously receiving more messages than it can process. Due
to how quickly Flodgatt can process messages, though, I suspect this
would only come up if an admin were running Flodgatt in a
*significantly* resource constrained environment, but I wanted to
mention it for the sake of completeness.
This commit also adds a new /status/backpressure endpoint that
displays the current length of the Redis input buffer (which should
typically be low or 0). Like the other /status endpoints, this
endpoint is only enabled when Flodgatt is compiled with the
`stub_status` feature.
2020-04-27 22:03:05 +02:00
|
|
|
"Received and unexpected reply from Redis: `{}`",
|
2020-04-01 21:35:24 +02:00
|
|
|
unexpected_reply
|
|
|
|
),
|
|
|
|
UnknownRedisErr(io_err) => {
|
|
|
|
format!("Unexpected failure communicating with Redis: {}", io_err)
|
|
|
|
}
|
|
|
|
IncorrectPassword(attempted_password) => format!(
|
|
|
|
"Incorrect Redis password. You supplied `{}`.\n \
|
|
|
|
Please supply correct password with REDIS_PASSWORD environmental variable.",
|
|
|
|
attempted_password
|
|
|
|
),
|
|
|
|
MissingPassword => "Invalid authentication for Redis. Redis is configured to require \
|
|
|
|
a password, but you did not provide one. \n\
|
|
|
|
Set a password using the REDIS_PASSWORD environmental variable."
|
|
|
|
.to_string(),
|
|
|
|
NotRedis(addr) => format!(
|
|
|
|
"The server at {} is not a Redis server. Please update the REDIS_HOST and/or \
|
|
|
|
REDIS_PORT environmental variables and try again.",
|
|
|
|
addr
|
|
|
|
),
|
2020-04-11 04:36:03 +02:00
|
|
|
TimelineErr(inner) => format!("{}", inner),
|
2020-04-01 21:35:24 +02:00
|
|
|
};
|
|
|
|
write!(f, "{}", msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 20:38:22 +02:00
|
|
|
impl From<request::TimelineErr> for RedisConnErr {
|
|
|
|
fn from(e: request::TimelineErr) -> RedisConnErr {
|
2020-04-11 04:36:03 +02:00
|
|
|
RedisConnErr::TimelineErr(e)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-01 21:35:24 +02:00
|
|
|
impl From<std::io::Error> for RedisConnErr {
|
|
|
|
fn from(e: std::io::Error) -> RedisConnErr {
|
|
|
|
RedisConnErr::UnknownRedisErr(e)
|
|
|
|
}
|
|
|
|
}
|