blob: c467171d275002768f9fc03dbdf26880eeed9bd4 (
plain) (
tree)
|
|
package api
import (
"context"
"fmt"
"time"
)
// Use this to call external services. It will handle timeout and request cancellation gracefully.
func CallExternalService(tag string, timeout time.Duration, routine func() *Error) *Error {
routineDone := make(chan *Error)
go func() {
routineDone <- routine()
}()
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
select {
case err := <-routineDone:
return err
case <-ctx.Done():
return &Error{ExternalServiceTimeout, "external service timeout", fmt.Errorf("'%v' routine timeouted", tag)}
}
}
var ErrorChan chan error
func ErrorMonitoring() {
for {
err := <-ErrorChan
log.Errorf("error: %v", err)
}
}
func init() {
ErrorChan = make(chan error)
go ErrorMonitoring()
}
|