I have a function that will potentially be called thousands of times per second (it sort of behaves like middle ware, it’s called on every HTTP request at a specific endpoint). This function runs some user-supplied code in a goroutine. This code is compiled in a way that all it is able to do is take some input, perform calculations, and return some output. It is not given access to any functions that could access the file system or the terminal for instance, so that is not an issue. What is an issue, however, is that this code has the potential to hang / loop forever, or consume a ton of memory on the server (such as spawning a ton of heap objects in an infinite loop in an attempt to crash the server). I want to regulate this by killing the goroutine if it runs for more than half a second, or killing it if it’s ram usage exceeds a megabyte. I have found a couple of ways to do both of these things, but all of them seem either hacky or will be hacky if used in conjunction with one another. So, what is the most idiomatic way in your opinion to do this? Of course, the main program needs to be able to use however much resources it needs which is why I am looking to do this at the level of each goroutine. OS-agnostic is preferred, but it absolutely must be able to work on a UNIX server.
Thank you for your time and, of course, feel free to ask for any additional info that may help you answer this question.