project-ely/internal/channels/timeout.go

17 lines
428 B
Go

package channels
import (
"context"
"time"
)
// RunWithTimeout will run a function in a goroutine with a new timeout context.
// If the context times out, then we exit the goroutine the return the context error.
func RunWithTimeout(timeout time.Duration, function func() error) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
err := BlockingFunction(ctx, function)
return err
}