onlineStatus.go 762 B

123456789101112131415161718192021222324252627282930313233343536373839
  1. package loadbalance
  2. import (
  3. "net/http"
  4. "time"
  5. )
  6. // Return the last ping status to see if the target is online
  7. func (m *RouteManager) IsTargetOnline(matchingDomainOrIp string) bool {
  8. value, ok := m.LoadBalanceMap.Load(matchingDomainOrIp)
  9. if !ok {
  10. return false
  11. }
  12. isOnline, ok := value.(bool)
  13. return ok && isOnline
  14. }
  15. // Ping a target to see if it is online
  16. func PingTarget(targetMatchingDomainOrIp string, requireTLS bool) bool {
  17. client := &http.Client{
  18. Timeout: 10 * time.Second,
  19. }
  20. url := targetMatchingDomainOrIp
  21. if requireTLS {
  22. url = "https://" + url
  23. } else {
  24. url = "http://" + url
  25. }
  26. resp, err := client.Get(url)
  27. if err != nil {
  28. return false
  29. }
  30. defer resp.Body.Close()
  31. return resp.StatusCode >= 200 && resp.StatusCode <= 600
  32. }