I have some goroutine logics like this:
go func() {
do_things_0()
do_things_1()
do_things_2()
do_things_3()
...
...
} ()
When the service receives a request, it will create such goroutine. And the goroutine maybe memory consuming and needs to run more than 30 minutes.
Sometimes, the service may notice the lack of memory, and needs to terminate some goroutines.
My questions are:
- How can I terminate the goroutine in the above example?
- Is there any way to know the used memory of each goroutine?
Update
- I read other SO answers that goroutine can't be killed outside
- I suppose that send a signal to the channel handled by the goroutine to make the goroutine quit is only suitable for the
for loop based logics
. - I am looking for some best practice to close the goroutine for the
flow based logics
.