Golang: a web service that monitors goroutines

Suppose I am writing a REST web service in golang. Inside, I have a few workers doing things. Such a goroutine is triggered by an HTTP API request. Of course, I would like to somehow monitor the progress of these gutturals. There will usually be a channel for news, bugs, etc. And the main program will be doing select

on these channels.

However, since the even loop of the main program is busy http.ListenAndServe()

, I see no way to achieve this. Given that this seems like a fairly common problem, I am wondering if there is a design pattern I am missing.

[EDIT] Some more technical details. Therefore I have a class Resource

that manages the resource pool. Resource.DoSomething()

is a long operation, so it will be a blow of the guttrum in the background. The channel will be open to allow status.

When an HTTP request comes in, this one runs DoSomething()

. But I have no context for polling the status. My silly plan currently is to poll the channel for input when the next HTTP request comes in. This should be enough, but not ideal - I'd like to know what's going on in this goroutine right away.

+3


source to share


1 answer


Run a Go procedure to specifically run a select that contains monitoring by placing your own loop inside it for

:



func main() {

    go func() {
        for {
            select {
            //TODO: Insert Code Here?
            }
        }
    }()

    err := http.ListenAndServe()

    if err != nil {
        //TODO:
    }
}

      

+2


source







All Articles