cmd/storj-sim: add "tool wait-for <address>"
For coordinating with other processes it can be useful to wait until another process is accepting requests on an address. Change-Id: Id623ed815149f14f9f0344e2f396ab70fc4dec6a
This commit is contained in:
parent
9ab824d3e6
commit
afc9545ff1
@ -4,11 +4,15 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
"storj.io/common/fpath"
|
||||
"storj.io/common/sync2"
|
||||
)
|
||||
|
||||
// Flags contains different flags for commands.
|
||||
@ -105,8 +109,51 @@ func main() {
|
||||
},
|
||||
)
|
||||
|
||||
toolCmd := &cobra.Command{
|
||||
Use: "tool",
|
||||
Short: "tools for working with storj-sim",
|
||||
}
|
||||
|
||||
toolCmd.AddCommand(
|
||||
func() *cobra.Command {
|
||||
cmd := &cobra.Command{
|
||||
Use: "wait-for <address>",
|
||||
Short: "waits for an address to accept connections",
|
||||
Args: cobra.ExactArgs(1),
|
||||
}
|
||||
retries := cmd.Flags().Int("retry", -1, "maximum retry count")
|
||||
interval := cmd.Flags().Duration("interval", 50*time.Millisecond, "how long to wait after each retry")
|
||||
|
||||
cmd.RunE = func(cmd *cobra.Command, args []string) (err error) {
|
||||
ctx, cancel := NewCLIContext(context.Background())
|
||||
defer cancel()
|
||||
defer fmt.Println()
|
||||
|
||||
target := args[0]
|
||||
|
||||
if *retries <= 0 {
|
||||
*retries = 1 << 31
|
||||
}
|
||||
for try := 0; try < *retries; try++ {
|
||||
if tryConnect(target) {
|
||||
return nil
|
||||
}
|
||||
fmt.Print(".")
|
||||
|
||||
if !sync2.Sleep(ctx, *interval) {
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
return fmt.Errorf("failed to connect to %q", target)
|
||||
}
|
||||
return cmd
|
||||
}(),
|
||||
)
|
||||
|
||||
rootCmd.AddCommand(
|
||||
networkCmd,
|
||||
toolCmd,
|
||||
)
|
||||
rootCmd.SilenceUsage = true
|
||||
err := rootCmd.Execute()
|
||||
|
@ -306,7 +306,7 @@ func (process *Process) Exec(ctx context.Context, command string) (err error) {
|
||||
func (process *Process) waitForAddress(maxStartupWait time.Duration) error {
|
||||
start := time.Now()
|
||||
for !process.Status.Started.Released() {
|
||||
if process.tryConnect() {
|
||||
if tryConnect(process.Info.Address) {
|
||||
return nil
|
||||
}
|
||||
|
||||
@ -321,8 +321,8 @@ func (process *Process) waitForAddress(maxStartupWait time.Duration) error {
|
||||
}
|
||||
|
||||
// tryConnect will try to connect to the process public address.
|
||||
func (process *Process) tryConnect() bool {
|
||||
conn, err := net.Dial("tcp", process.Info.Address)
|
||||
func tryConnect(address string) bool {
|
||||
conn, err := net.Dial("tcp", address)
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user