NetUtil.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. # Licensed to the Apache Software Foundation (ASF) under one or more
  2. # contributor license agreements. See the NOTICE file distributed with
  3. # this work for additional information regarding copyright ownership.
  4. # The ASF licenses this file to You under the Apache License, Version 2.0
  5. # (the "License"); you may not use this file except in compliance with
  6. # the License. You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. from urlparse import urlparse
  16. import time
  17. import logging
  18. import httplib
  19. logger = logging.getLogger()
  20. class NetUtil:
  21. CONNECT_SERVER_RETRY_INTERVAL_SEC = 10
  22. HEARTBEAT_IDDLE_INTERVAL_SEC = 10
  23. MINIMUM_INTERVAL_BETWEEN_HEARTBEATS = 0.1
  24. # Url within server to request during status check. This url
  25. # should return HTTP code 200
  26. SERVER_STATUS_REQUEST = "{0}/cert/ca"
  27. # For testing purposes
  28. DEBUG_STOP_RETRIES_FLAG = False
  29. def checkURL(self, url):
  30. """Try to connect to a given url. Result is True if url returns HTTP code 200, in any other case
  31. (like unreachable server or wrong HTTP code) result will be False
  32. """
  33. logger.info("Connecting to the following url " + url);
  34. try:
  35. parsedurl = urlparse(url)
  36. ca_connection = httplib.HTTPSConnection(parsedurl[1])
  37. ca_connection.request("GET", parsedurl[2])
  38. response = ca_connection.getresponse()
  39. status = response.status
  40. logger.info("Calling url received " + str(status))
  41. if status == 200:
  42. return True
  43. else:
  44. return False
  45. except Exception, e:
  46. logger.info("Failed to connect to " + str(url) + " due to " + str(e))
  47. return False
  48. def try_to_connect(self, server_url, max_retries, logger = None):
  49. """Try to connect to a given url, sleeping for CONNECT_SERVER_RETRY_INTERVAL_SEC seconds
  50. between retries. No more than max_retries is performed. If max_retries is -1, connection
  51. attempts will be repeated forever until server is not reachable
  52. Returns count of retries
  53. """
  54. if logger is not None:
  55. logger.info("DEBUG: Trying to connect to the server at " + server_url)
  56. retries = 0
  57. while (max_retries == -1 or retries < max_retries) and not self.DEBUG_STOP_RETRIES_FLAG:
  58. server_is_up = self.checkURL(self.SERVER_STATUS_REQUEST.format(server_url))
  59. if server_is_up:
  60. break
  61. else:
  62. if logger is not None:
  63. logger.info('Server at {0} is not reachable, sleeping for {1} seconds...'.format(server_url,
  64. self.CONNECT_SERVER_RETRY_INTERVAL_SEC))
  65. retries += 1
  66. time.sleep(self.CONNECT_SERVER_RETRY_INTERVAL_SEC)
  67. return retries