39 lines
851 B
Go
39 lines
851 B
Go
// Package faultbarrier provides one-shot synchronization for controlled process-failure experiments.
|
|
package faultbarrier
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"time"
|
|
)
|
|
|
|
// Wait claims an armed phase and blocks until its hit file disappears.
|
|
// A nonempty id identifies a newly staged attachment before its job exists.
|
|
func Wait(ctx context.Context, base, id string) error {
|
|
if err := os.Rename(base+".arm", base+".hit"); err != nil {
|
|
if os.IsNotExist(err) {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
if id != "" {
|
|
if err := os.WriteFile(base+".id", []byte(id), 0600); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
t := time.NewTicker(50 * time.Millisecond)
|
|
defer t.Stop()
|
|
for {
|
|
if _, err := os.Stat(base + ".hit"); os.IsNotExist(err) {
|
|
return nil
|
|
} else if err != nil {
|
|
return err
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return ctx.Err()
|
|
case <-t.C:
|
|
}
|
|
}
|
|
}
|