Skip to content

Commit

Permalink
Made concurrent requests avaialbe
Browse files Browse the repository at this point in the history
  • Loading branch information
Utkarsh Shukla committed Dec 29, 2024
1 parent 792e9e3 commit 88c6f0b
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 51 deletions.
92 changes: 46 additions & 46 deletions app/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,17 +108,17 @@ func runPrometheusHTTPServer(ctx context.Context, port uint16, profile bool) {
mux.HandleFunc("/", healthcheck)
mux.HandleFunc("/health", healthcheck)
// if profile {
logger.Infof("Prometheus handler includes /debug/pprof endpoints")
mux.HandleFunc("/debug/pprof/", pprofhttp.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprofhttp.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprofhttp.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprofhttp.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprofhttp.Trace)
logger.Infof("Prometheus handler includes /debug/pprof endpoints")
mux.HandleFunc("/debug/pprof/", pprofhttp.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprofhttp.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprofhttp.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprofhttp.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprofhttp.Trace)
// }

server := &http.Server{
// Addr: fmt.Sprintf(":%d", 9102),
Addr: ":9102",
Addr: ":9102",
Handler: mux,
}
logger.Fatal(server.ListenAndServe())
Expand Down Expand Up @@ -383,42 +383,42 @@ func generateSomeControlTokens(c *ControllerConfig, names string) {

// The below code acts as a workaround for carina controller issue, it will restart the contoller in case http server on port 9002 is unresponsive.
// TODO: the below solution is only temporary, to be removed once we get the real solution.
func healthCheckRunRequestFlow() {
// Define the timeout for API response and interval for execution
timeout := 10 * time.Second
interval := 60 * time.Second
ctx2, _ := context.WithCancel(context.Background())
logger := logging.WithContext(ctx2).Sugar()
logger.Infof("Started local healthcheck goroutine.")
// Start a goroutine to periodically check the API response
ticker := time.NewTicker(interval)
defer ticker.Stop()
for {
select {
case <-ticker.C:
// Create a context with timeout
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()

// Make the HTTP request with a timeout
req, err := http.NewRequestWithContext(ctx, "GET", "http://localhost:9002/api/v1/applications", nil)
if err != nil {
// fmt.Println("Error creating request:", err)
os.Exit(0)
}

client := &http.Client{
Timeout: timeout,
}

resp, err := client.Do(req)
if err != nil {
// fmt.Println("Error making request or timeout occurred:", err)
os.Exit(0)
}
defer resp.Body.Close()

}
}
}
// func healthCheckRunRequestFlow() {
// // Define the timeout for API response and interval for execution
// timeout := 10 * time.Second
// interval := 60 * time.Second
// ctx2, _ := context.WithCancel(context.Background())
// logger := logging.WithContext(ctx2).Sugar()
// logger.Infof("Started local healthcheck goroutine.")
// // Start a goroutine to periodically check the API response
// ticker := time.NewTicker(interval)
// defer ticker.Stop()

// for {
// select {
// case <-ticker.C:
// // Create a context with timeout
// ctx, cancel := context.WithTimeout(context.Background(), timeout)
// defer cancel()

// // Make the HTTP request with a timeout
// req, err := http.NewRequestWithContext(ctx, "GET", "http://localhost:9002/api/v1/applications", nil)
// if err != nil {
// // fmt.Println("Error creating request:", err)
// os.Exit(0)
// }

// client := &http.Client{
// Timeout: timeout,
// }

// resp, err := client.Do(req)
// if err != nil {
// // fmt.Println("Error making request or timeout occurred:", err)
// os.Exit(0)
// }
// defer resp.Body.Close()

// }
// }
// }
10 changes: 5 additions & 5 deletions internal/serviceconfig/service_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ func RunHTTPSServer(ctx context.Context, em EchoManager, routes Destinations, tl
server := &http.Server{
Addr: fmt.Sprintf(":%d", service.Port),
Handler: mux,
ReadTimeout: 5 * time.Second,
WriteTimeout: 10 * time.Second,
IdleTimeout: 120 * time.Second,
// ReadTimeout: 5 * time.Second,
// WriteTimeout: 10 * time.Second,
// IdleTimeout: 120 * time.Second,
}
// defer func() {
// logger.Infow("RunHTTPSServer Server stopped! with service",service.Name)
Expand Down Expand Up @@ -269,9 +269,9 @@ func runAPIHandler(em EchoManager, routes Destinations, ep SearchSpec, w http.Re
streamID := ulid.GlobalContext.Ulid()
echo := em.MakeRequester(ctx, ep, streamID)

defer func() {
go func() {
echo.RunRequest(ctx, session, body, w, r)
cancel()
echo.Shutdown(ctx)
}()
echo.RunRequest(ctx, session, body, w, r)
}

0 comments on commit 88c6f0b

Please sign in to comment.