summaryrefslogtreecommitdiff
path: root/utilities/utilities.go
diff options
context:
space:
mode:
authorWill Hawkins <[email protected]>2023-04-19 09:44:57 -0400
committerWill Hawkins <[email protected]>2023-04-19 09:44:57 -0400
commite2bdd8109e9d707d30d518f4da334ddc2c747eb0 (patch)
tree370d9e575f108ad263e3338c84caf3731143602c /utilities/utilities.go
parentfd658c8e1ac37a770b62d5d5c53dfaaf9db256a6 (diff)
[Bugfix] Probers starting too soon caused panic
In the case where the scheduler allowed a delay between go threads in a particular order, it was possible that a self prober would attempt to use a connection before an HTTP connection was established. Fixes #45. (at least I hope!)
Diffstat (limited to 'utilities/utilities.go')
-rw-r--r--utilities/utilities.go20
1 files changed, 20 insertions, 0 deletions
diff --git a/utilities/utilities.go b/utilities/utilities.go
index 377be56..e75d373 100644
--- a/utilities/utilities.go
+++ b/utilities/utilities.go
@@ -15,6 +15,7 @@
package utilities
import (
+ "context"
"fmt"
"math"
"math/rand"
@@ -22,6 +23,7 @@ import (
"reflect"
"sort"
"strings"
+ "sync"
"sync/atomic"
"time"
@@ -209,3 +211,21 @@ func ApproximatelyEqual[T float32 | float64](truth T, maybe T, fudge T) bool {
func UserAgent() string {
return fmt.Sprintf("goresponsiveness/%s", GitVersion)
}
+
+func WaitWithContext(ctxt context.Context, condition *func() bool, mu *sync.Mutex, c *sync.Cond) bool {
+ mu.Lock()
+ for !(*condition)() && ctxt.Err() == nil {
+ c.Wait()
+ }
+ return ctxt.Err() == nil
+}
+
+func ContextSignaler(ctxt context.Context, st time.Duration, condition *func() bool, c *sync.Cond) {
+ for !(*condition)() && ctxt.Err() == nil {
+ time.Sleep(st)
+ }
+ if ctxt.Err() != nil {
+ c.Broadcast()
+ return
+ }
+}