Context in Erlang
Here’s the translation of the Go code to Erlang, formatted in Markdown suitable for Hugo:
Our example demonstrates how to create a simple HTTP server in Erlang. This server showcases the usage of processes for handling cancellation, which is similar to the concept of context.Context
in other languages. In Erlang, processes carry their own state and can communicate with each other, allowing for controlled cancellation and timeout handling.
In this Erlang example, we create an HTTP server that listens on port 8090. The handle_hello
function is called when a request is made to the “/hello” endpoint.
Instead of using a context, we use Erlang’s built-in process management capabilities. The handle_hello
function spawns a new process to do the work and waits for it to complete or for a timeout to occur.
The do_work
function simulates some work by sleeping for 10 seconds before sending a message back to the parent process.
The parent process in handle_hello
uses a receive
block with a timeout. If the work completes within 10 seconds, it sends a success response. If a timeout occurs, it terminates the worker process and sends an error response.
To run the server:
You can then make a request to the server:
If you interrupt the curl command before it completes (e.g., by pressing Ctrl+C), you’ll see the cancellation message in the server logs.
This example demonstrates how Erlang’s process model can be used to handle timeouts and cancellations in a way that’s conceptually similar to contexts in other languages, while leveraging Erlang’s built-in concurrency features.