35
35
36
36
37
37
def parse_uri (uri ):
38
+ # TODO: Support Unix sockets.
38
39
def parse_error (uri , msg ):
39
40
msg = 'URI "%s": %s' % (uri , msg )
40
41
return None , msg
@@ -59,31 +60,53 @@ def parse_error(uri, msg):
59
60
60
61
61
62
def validate_address (address ):
62
- messages = []
63
-
64
- if isinstance (address , dict ):
65
- if "host" not in address :
66
- messages .append ("host key must be set" )
67
- elif not isinstance (address ["host" ], string_types ):
68
- messages .append ("host value must be string type" )
69
-
70
- if "port" not in address :
71
- messages .append ("port is not set" )
72
- elif not isinstance (address ["port" ], int ):
73
- messages .append ("port value must be int type" )
74
- elif address ["port" ] == 0 :
75
- messages .append ("port value must not be zero" )
76
- elif address ["port" ] > 65535 :
77
- messages .append ("port value must not be above 65535" )
78
- else :
79
- messages .append ("address must be a dict" )
80
-
81
- if messages :
82
- messages_str = ', ' .join (messages )
83
- msg = 'Address %s: %s' % (str (address ), messages_str )
84
- return None , msg
85
-
86
- return True , None
63
+ def format_error (address , err ):
64
+ return None , 'Address %s: %s' % (str (address ), err )
65
+
66
+ if not isinstance (address , dict ):
67
+ return format_error (address , 'address must be a dict' )
68
+
69
+ if 'port' not in address or address ['port' ] is None :
70
+ return format_error (address , 'port is not set or None' )
71
+
72
+ if isinstance (address ['port' ], int ):
73
+ # Looks like an inet address.
74
+
75
+ # Validate host.
76
+ if 'host' not in address or address ['host' ] is None :
77
+ return format_error (address ,
78
+ 'host is mandatory for an inet address' )
79
+ if not isinstance (address ['host' ], string_types ):
80
+ return format_error (address ,
81
+ 'host must be a string for an inet address' )
82
+
83
+ # Validate port.
84
+ if not isinstance (address ['port' ], int ):
85
+ return format_error (address ,
86
+ 'port must be an int for an inet address' )
87
+ if address ['port' ] < 1 or address ['port' ] > 65535 :
88
+ return format_error (address , 'port must be in range [1, 65535] '
89
+ 'for an inet address' )
90
+
91
+ # Looks okay.
92
+ return True , None
93
+ elif isinstance (address ['port' ], string_types ):
94
+ # Looks like a unix address.
95
+
96
+ # Expect no host.
97
+ if 'host' in address and address ['host' ] is not None :
98
+ return format_error (
99
+ address , 'host must be unset or None for a unix address' )
100
+
101
+ # Validate port.
102
+ if not isinstance (address ['port' ], string_types ):
103
+ return format_error (address ,
104
+ 'port must be a string for a unix address' )
105
+
106
+ # Looks okay.
107
+ return True , None
108
+
109
+ return format_error (address , 'port must be an int or a string' )
87
110
88
111
89
112
class RoundRobinStrategy (object ):
0 commit comments