Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing port from ping-pong-mesh client #10

Merged
merged 5 commits into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 5 additions & 22 deletions workloads/ping-pong-mesh/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"net/http"
"net/url"
"os"
"strconv"
"time"
)

Expand All @@ -20,8 +19,7 @@ func main() {
}

type Env struct {
ServerAddress string
ServerPort int
ServerAddress string
}

func getEnvOrPanic(variable string) string {
Expand All @@ -32,24 +30,9 @@ func getEnvOrPanic(variable string) string {
return v
}

func getEnvIntWithDefault(variable string, defaultValue int) int {
v, ok := os.LookupEnv(variable)
if !ok {
return defaultValue
}

intValue, err := strconv.Atoi(v)
if err != nil {
return defaultValue
}

return intValue
}

func getEnv() *Env {
return &Env{
ServerAddress: getEnvOrPanic("PING_PONG_SERVICE_HOST"),
ServerPort: getEnvIntWithDefault("PING_PONG_SERVICE_PORT", 80),
ServerAddress: getEnvOrPanic("PING_PONG_SERVICE_HOST"),
}
}

Expand All @@ -60,17 +43,17 @@ func run(ctx context.Context, env *Env) error {

for {
slog.Info("ping...")
if err := ping(client, env.ServerAddress, env.ServerPort); err != nil {
if err := ping(client, env.ServerAddress); err != nil {
slog.Error("problem reaching server", "error", err)
}
time.Sleep(5 * time.Second)
}
}

func ping(client *http.Client, serverAddr string, serverPort int) error {
func ping(client *http.Client, serverAddr string) error {
r, err := client.Get((&url.URL{
Scheme: "http",
Host: fmt.Sprintf("%s:%d", serverAddr, serverPort),
Host: serverAddr,
}).String())

if err != nil {
Expand Down
Loading