Skip to content

Commit

Permalink
add timeout to http clients
Browse files Browse the repository at this point in the history
  • Loading branch information
USA-RedDragon committed Feb 24, 2024
1 parent f354c85 commit 9ba9de2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
7 changes: 6 additions & 1 deletion cmd/notify.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package cmd
import (
"fmt"
"net/http"
"time"

"github.com/USA-RedDragon/aredn-manager/internal/config"
"github.com/spf13/cobra"
Expand All @@ -28,7 +29,11 @@ func runNotify(cmd *cobra.Command, _ []string) error {
}
req.Header.Add("Content-Type", "application/json")

resp, err := http.DefaultClient.Do(req)
client := http.Client{
Timeout: 5 * time.Second,
}

resp, err := client.Do(req)
if err != nil {
return err
}
Expand Down
8 changes: 6 additions & 2 deletions internal/metrics/olsr.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,17 @@ var (
)

func OLSRWatcher(db *gorm.DB) {
client := http.Client{
Timeout: 5 * time.Second,
}

for {
resp, err := http.DefaultClient.Get("http://localhost:9090/links")
resp, err := client.Get("http://localhost:9090/links")
if err != nil {
fmt.Printf("OLSRWatcher: Unable to get links: %v\n", err)
time.Sleep(1 * time.Second)
continue
}
defer resp.Body.Close()
var links apimodels.OlsrdLinks
err = json.NewDecoder(resp.Body).Decode(&links)
if err != nil {
Expand All @@ -101,6 +104,7 @@ func OLSRWatcher(db *gorm.DB) {
resp.Body.Close()
continue
}
resp.Body.Close()

foundInterfaces := []string{}

Expand Down
14 changes: 11 additions & 3 deletions internal/server/api/controllers/v1/aredn_compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"regexp"
"strings"
"syscall"
"time"

"github.com/USA-RedDragon/aredn-manager/internal/config"
"github.com/USA-RedDragon/aredn-manager/internal/db/models"
Expand All @@ -35,7 +36,11 @@ func GETMetrics(c *gin.Context) {
return
}

nodeResp, err := http.DefaultClient.Get(fmt.Sprintf("http://%s:9100/metrics", config.MetricsNodeExporterHost))
client := http.Client{
Timeout: 5 * time.Second,
}

nodeResp, err := client.Get(fmt.Sprintf("http://%s:9100/metrics", config.MetricsNodeExporterHost))
if err != nil {
fmt.Printf("GETMetrics: Unable to get node-exporter metrics: %v\n", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Try again later"})
Expand All @@ -50,7 +55,7 @@ func GETMetrics(c *gin.Context) {
n, err = nodeResp.Body.Read(buf)
}

metricsResp, err := http.DefaultClient.Get(fmt.Sprintf("http://localhost:%d/metrics", config.MetricsPort))
metricsResp, err := client.Get(fmt.Sprintf("http://localhost:%d/metrics", config.MetricsPort))
if err != nil {
fmt.Printf("GETMetrics: Unable to get metrics: %v\n", err)
c.JSON(http.StatusInternalServerError, gin.H{"error": "Try again later"})
Expand Down Expand Up @@ -249,8 +254,11 @@ func getHosts(parser *olsrd.HostsParser) []apimodels.Host {

func getLinkInfo() map[string]apimodels.LinkInfo {
ret := make(map[string]apimodels.LinkInfo)
client := http.Client{
Timeout: 5 * time.Second,
}
// http request http://localhost:9090/links
resp, err := http.DefaultClient.Get("http://localhost:9090/links")
resp, err := client.Get("http://localhost:9090/links")
if err != nil {
fmt.Printf("GETSysinfo: Unable to get links: %v\n", err)
return nil
Expand Down

0 comments on commit 9ba9de2

Please sign in to comment.