I am currently working on a program which calls a long running function using a c library with cgo
. I cannot edit the library to allow for timeouts using c. My only solution so far was to leave a zombie goroutine running
func Timeout(timeout time.Duration, runFunc func()) bool {
var wg = new(sync.WaitGroup)
c := make(chan interface{})
wg.Add(1)
go func() {
defer close(c)
wg.Wait()
}()
go func() {
runFunc()
c <- nil
wg.Done()
}()
select {
case <-c:
return false
case <-time.After(timeout):
return true
}
}
with the long running function working but this is for a long-running server which can lead to massive memory leaks/wasted cpu cycles over time.