Skip to content
This repository has been archived by the owner on Sep 7, 2024. It is now read-only.

Commit

Permalink
fix: removing unnecessary sleep in network queries (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
kehiy authored Jul 13, 2024
1 parent a838763 commit 0c90cd3
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 26 deletions.
14 changes: 12 additions & 2 deletions cmd/commands/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"net"
"os"
"time"

cobra "github.com/spf13/cobra"
)
Expand All @@ -18,6 +19,7 @@ func PingCommand(parentCmd *cobra.Command) {
address := ping.Flags().StringP("address", "a", "localhost:7070", "remote address of your time trace instance.")
username := ping.Flags().StringP("username", "u", "root", "username of the user you are going to connect with.")
password := ping.Flags().StringP("password", "p", "", "password of user trying to connect with.")
verbose := ping.Flags().BoolP("verbose", "v", false, "verbose mode.")

ping.Run = func(cmd *cobra.Command, args []string) {
conn, err := net.Dial("tcp", *address)
Expand All @@ -30,12 +32,20 @@ func PingCommand(parentCmd *cobra.Command) {

do(conn, conQuery)

details := ""

t := time.Now()
response := do(conn, "PING")

if *verbose {
details = fmt.Sprintf("It toked %v to get the response", time.Since(t).Abs().String())
}

if response == "PONG" {
cmd.Println("PONG, everything is ok.")
cmd.Printf("PONG, everything is ok.\n%s\n", details)
os.Exit(0)
} else {
ExitOnError(cmd, fmt.Errorf("something went wrong: %v", response))
ExitOnError(cmd, fmt.Errorf("something went wrong: %v\n%s", response, details))
}
}
}
24 changes: 0 additions & 24 deletions cmd/commands/repl.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import (
"os"
"os/exec"
"runtime"
"time"

"github.com/peterh/liner"
cobra "github.com/spf13/cobra"
Expand Down Expand Up @@ -100,29 +99,6 @@ func ConnectCommand(parentCmd *cobra.Command) {
}
}

func do(conn net.Conn, q string) string {
resBuf := make([]byte, 1024)
query := []byte(q)

if len(query) < 1 {
return "INVALID"
}

_, err := conn.Write(query)
if err != nil {
return err.Error()
}

time.Sleep(time.Second * 1)

n, err := conn.Read(resBuf)
if err != nil {
return err.Error()
}

return string(resBuf[:n])
}

func cleanTerminal() {
cf, ok := clear[runtime.GOOS]
if ok {
Expand Down
23 changes: 23 additions & 0 deletions cmd/commands/util.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package commands

import (
"net"
"os"

"github.com/spf13/cobra"
Expand All @@ -10,3 +11,25 @@ func ExitOnError(cmd *cobra.Command, err error) {
cmd.PrintErrln(err)
os.Exit(1)
}

func do(conn net.Conn, q string) string {
resBuf := make([]byte, 1024)

query := []byte(q)

if len(query) < 1 {
return "INVALID"
}

_, err := conn.Write(query)
if err != nil {
return err.Error()
}

n, err := conn.Read(resBuf)
if err != nil {
return err.Error()
}

return string(resBuf[:n])
}

0 comments on commit 0c90cd3

Please sign in to comment.