Rust's Mutex, Atomics and UnsafeCell – Spooky Action at a Distance?
A defining feature of Rust is its concept of aliasing ⊕
mutability. This rule governs that at any time a value may either
have multiple immutable shared references, or a single mutable
unique reference, and never both. While this greatly helps in
producing fast, efficient and correct code, it can be limiting. To
this end, Rust also features types that bend these rules, like
Mutex
, RwLock
,
Cell
, RefCell
, and
the ominous UnsafeCell
types. In this post we
explore how these types interact with Rust's type system and concepts
of references and aliasing ⊕ mutability. We do so by looking at
how the AtomicUsize
and Mutex
types are implemented, how violating Rust's assumptions
can lead to incorrect optimizations by the compiler, and the
surprising global impact of synchronization primitives.
Read more...
Implementing Long-Running HTTP Connections in Phoenix (override Cowboy's idle_timeout)
The Elixir language's actor programming model and the Phoenix web framework are
particularly well-suited to implement long-running streaming HTTP connections,
such as used in Server-Sent Events (SSE). For example, this blog post on
Server-Sent Events with Elixir by Krister Viirsaar succinctly demonstrates how
an SSE endpoint can be implemented in Phoenix without using any external
libraries. However, the Cowboy HTTP server terminates idle connections after a
globally-configured idle timeout, which is only reset when new data is received
by clients. Also, SSE streams should send keep-alive messages regularly to
ensure that clients, reverse-proxies, and middle-boxes don't close such
connections. In this post, I will extend a minimal Phoenix SSE request endpoint
example by adding keep-alive messages and overriding the Cowboy HTTP server idle
timeout.
Read more...
Running Emacs in systemd's session.slice
I use Emacs and EXWM as my window manager. For this setup, running an
Emacs daemon as a systemd-user unit allows me to attach multiple
clients to this process (for instance, to work in the same session
through an SSH connection on my iPad), and have the daemon survive
restarts of my graphical session. However, all subprocesses started
from within Emacs—which are virtually all applications & shells on my
system—are then tracked within the same systemd scope. This means
that a single application consuming excessive amounts of memory can
bring down my entire user session (looking at you, Firefox). This post
documents how you can move this Emacs daemon from the systemd
app.slice
into the more appropriate session.slice
, and run
applications & shells from within Emacs in their own scopes in
app.slice
.
Read more...