mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-28 06:05:06 +03:00
51 lines
1.5 KiB
Go
51 lines
1.5 KiB
Go
// Package util contains miscellaneous utilities used by yggdrasil.
|
|
// In particular, this includes a crypto worker pool, Cancellation machinery, and a sync.Pool used to reuse []byte.
|
|
package util
|
|
|
|
// These are misc. utility functions that didn't really fit anywhere else
|
|
|
|
import (
|
|
"time"
|
|
)
|
|
|
|
// Any logger that satisfies this interface is suitable for Yggdrasil.
|
|
type Logger interface {
|
|
Printf(string, ...interface{})
|
|
Println(...interface{})
|
|
Infof(string, ...interface{})
|
|
Infoln(...interface{})
|
|
Warnf(string, ...interface{})
|
|
Warnln(...interface{})
|
|
Errorf(string, ...interface{})
|
|
Errorln(...interface{})
|
|
Debugf(string, ...interface{})
|
|
Debugln(...interface{})
|
|
}
|
|
|
|
// TimerStop stops a timer and makes sure the channel is drained, returns true if the timer was stopped before firing.
|
|
func TimerStop(t *time.Timer) bool {
|
|
stopped := t.Stop()
|
|
select {
|
|
case <-t.C:
|
|
default:
|
|
}
|
|
return stopped
|
|
}
|
|
|
|
// FuncTimeout runs the provided function in a separate goroutine, and returns true if the function finishes executing before the timeout passes, or false if the timeout passes.
|
|
// It includes no mechanism to stop the function if the timeout fires, so the user is expected to do so on their own (such as with a Cancellation or a context).
|
|
func FuncTimeout(timeout time.Duration, f func()) bool {
|
|
success := make(chan struct{})
|
|
go func() {
|
|
defer close(success)
|
|
f()
|
|
}()
|
|
timer := time.NewTimer(timeout)
|
|
defer TimerStop(timer)
|
|
select {
|
|
case <-success:
|
|
return true
|
|
case <-timer.C:
|
|
return false
|
|
}
|
|
}
|