@@ -509,14 +509,15 @@ func getKupoClient(c *ChainSync) (*kugo.Client, error) {
509509 return c .kupoClient , nil
510510 }
511511
512- // First validate the URL
512+ // Validate URL first
513513 _ , err := url .ParseRequestURI (c .kupoUrl )
514514 if err != nil {
515515 return nil , fmt .Errorf ("invalid kupo URL: %w" , err )
516516 }
517517
518518 KugoCustomLogger := logging .NewKugoCustomLogger (logging .LevelInfo )
519519
520+ // Create client with timeout
520521 k := kugo .New (
521522 kugo .WithEndpoint (c .kupoUrl ),
522523 kugo .WithLogger (KugoCustomLogger ),
@@ -528,29 +529,29 @@ func getKupoClient(c *ChainSync) (*kugo.Client, error) {
528529 }
529530
530531 healthUrl := strings .TrimRight (c .kupoUrl , "/" ) + "/health"
531- req , err := http .NewRequest (http .MethodGet , healthUrl , nil )
532+
533+ // Create context with timeout
534+ ctx , cancel := context .WithTimeout (context .Background (), 3 * time .Second )
535+ defer cancel ()
536+
537+ req , err := http .NewRequestWithContext (ctx , http .MethodGet , healthUrl , nil )
532538 if err != nil {
533539 return nil , fmt .Errorf ("failed to create health check request: %w" , err )
534540 }
535541
536542 resp , err := httpClient .Do (req )
537543 if err != nil {
538- var urlErr * url. Error
539- if errors . As ( err , & urlErr ) {
540- if urlErr . Timeout () {
541- return nil , fmt .Errorf ("kupo health check timed out after 2 seconds" )
542- }
543- if strings . Contains ( err . Error (), "no such host" ) {
544- return nil , fmt . Errorf ( "failed to connect to kupo: %w" , err )
545- }
544+ // Handle different error types
545+ switch {
546+ case errors . Is ( err , context . DeadlineExceeded ):
547+ return nil , fmt .Errorf ("kupo health check timed out after 3 seconds" )
548+ case strings . Contains ( err . Error (), "no such host" ):
549+ return nil , fmt . Errorf ( "failed to resolve kupo host: %w" , err )
550+ default :
551+ return nil , fmt . Errorf ( "failed to perform health check: %w" , err )
546552 }
547- return nil , fmt .Errorf ("failed to perform health check: %w" , err )
548553 }
549- defer func () {
550- if resp != nil && resp .Body != nil {
551- resp .Body .Close ()
552- }
553- }()
554+ defer resp .Body .Close ()
554555
555556 if resp .StatusCode != http .StatusOK {
556557 return nil , fmt .Errorf ("health check failed with status code: %d" , resp .StatusCode )
0 commit comments