From what I know, the default http router as well as the gorilla/mux router put each incoming HTTP request entrypoint on its own goroutine. Like Node.js domains, where we can pin a request to a domain, can we pin a request in Golang to a goroutine, and if there is any panic in the goroutine, we respond with an error instead of shutting down the server?
In node.js, it might look like:
const domain = require('domain');
const http = require('http');
const server = http.createServer((req,res) => {
const d = domain.create();
d.once('error', e =>{
res.json({error:e}); // check res.headersSent to see if already sent
});
res.once('finish', () => {
d.removeAllListeners(); // prevent memory leak
});
d.run(() => {
handleRequest(req,res);
});
});
is there a way to do something similar with goroutines in Golang?