diff --git a/cmd/check.go b/cmd/check.go index 71a565f..8cb2441 100644 --- a/cmd/check.go +++ b/cmd/check.go @@ -3,11 +3,13 @@ package cmd import ( "fmt" + "sync" "time" "github.com/jjuarez/simple-prober/internal/codes" "github.com/jjuarez/simple-prober/internal/config" "github.com/jjuarez/simple-prober/internal/logger" + "github.com/jjuarez/simple-prober/internal/model" "github.com/jjuarez/simple-prober/internal/utils" log "github.com/sirupsen/logrus" "github.com/spf13/cobra" @@ -25,6 +27,31 @@ var ( logLevel string ) +func doTests(endpoints []model.Endpoint) map[string]bool { + start := time.Now() + defer func() { + log.Info(fmt.Sprintf("Execution time: %s", time.Since(start))) + }() + + connectionResults := make(map[string]bool, len(endpoints)) + wg := sync.WaitGroup{} + // Connection tests + for _, e := range endpoints { + wg.Add(1) + go func(e model.Endpoint) { + r, err := e.Connect(timeout) + if err != nil { + return + } + connectionResults[e.Name] = r + log.Debug(fmt.Sprintf("Tested: %s, with result: %v", e.Name, r)) + wg.Done() + }(e) + } + wg.Wait() + return connectionResults +} + // checkCmd represents the check command var checkCmd = &cobra.Command{ Use: "check", @@ -43,15 +70,8 @@ var checkCmd = &cobra.Command{ if err != nil { utils.ExitCommand(codes.ReadError, err) } - - // Connection tests - for _, e := range *eList { - r, err := e.TestConnection(timeout) - if err != nil { - log.Error(err) - } - log.Info(fmt.Sprintf("%v, %v\n", e, r)) - } + results := doTests(*eList) + log.Info(results) }, } @@ -59,13 +79,10 @@ func init() { var parameterTimeout int rootCmd.AddCommand(checkCmd) - // The log level rootCmd.PersistentFlags().StringVar(&logLevel, "loglevel", defaultLogLevel, "The log level") - // The confiuration file name rootCmd.PersistentFlags().StringVar(&configFileName, "config", defaultConfigFileName, "Config file (default is: config/endpoints.yaml)") - // The connection timeout rootCmd.PersistentFlags().IntVar(¶meterTimeout, "timeout", defaultTimeout, "Connection timeout") timeout = time.Duration(parameterTimeout) * time.Second diff --git a/internal/model/model.go b/internal/model/model.go index 4d8e90a..095f4c2 100644 --- a/internal/model/model.go +++ b/internal/model/model.go @@ -8,10 +8,12 @@ import ( "time" ) +// Stringable ... type Stringable interface { String() string } +// Printable ... type Printable interface { Println() } @@ -19,22 +21,20 @@ type Printable interface { // Endpoint The endpoint basic data. type Endpoint struct { Name string `yaml:"name,omitempty"` - Kind string `yaml:"kind,omitempty"` Address string `yaml:"address"` } // New Creates an Endpoint. -func New(name string, kind string, address string) *Endpoint { +func New(name string, address string) *Endpoint { return &Endpoint{ Name: name, - Kind: kind, Address: address, } } // String Implements the Stingable interface for Endpoint. func (e Endpoint) String() string { - return fmt.Sprintf("%T(name:%s, kind:%s, address:%s)", e, e.Name, e.Kind, e.Address) + return fmt.Sprintf("%T(name:%s, address:%s)", e, e.Name, e.Address) } // Println Implements the Printable interface for Endpoint. @@ -42,8 +42,8 @@ func (e Endpoint) Println() { fmt.Println(e.String()) } -// TestConnection ... -func (e Endpoint) TestConnection(timeout time.Duration) (bool, error) { +// Connect ... +func (e Endpoint) Connect(timeout time.Duration) (bool, error) { var dialer net.Dialer ctx, cancel := context.WithTimeout(context.Background(), timeout) defer cancel()