@@ -26,6 +26,14 @@ def getnext(self):
26
26
self .pos = (self .pos + 1 ) % len (self .addrs )
27
27
return self .addrs [tmp ]
28
28
29
+ def parse_uri (uri_str ):
30
+ if not uri_str :
31
+ return
32
+ uri = uri_str .split (':' )
33
+ host = uri [0 ]
34
+ port = int (uri [1 ])
35
+ if host and port :
36
+ return {'host' : host , 'port' : port }
29
37
30
38
class MeshConnection (Connection ):
31
39
'''
@@ -37,22 +45,16 @@ class MeshConnection(Connection):
37
45
the list and switch nodes in case of unavailability of the current node.
38
46
39
47
'get_nodes_function_name' param of the constructor sets the name of a stored
40
- Lua function used to refresh the list of available nodes. A generic function
41
- for getting the list of nodes looks like this:
48
+ Lua function used to refresh the list of available nodes. The function takes
49
+ no parameters and returns a list of strings in format 'host:port'. A generic
50
+ function for getting the list of nodes looks like this:
42
51
43
52
.. code-block:: lua
44
53
45
- function get_nodes ()
54
+ function get_cluster_nodes ()
46
55
return {
47
- {
48
- host = '192.168.0.1',
49
- port = 3301
50
- },
51
- {
52
- host = '192.168.0.2',
53
- port = 3302
54
- },
55
-
56
+ '192.168.0.1:3301',
57
+ '192.168.0.2:3302',
56
58
-- ...
57
59
}
58
60
end
@@ -72,16 +74,15 @@ class MeshConnection(Connection):
72
74
73
75
for i = 1, #replicas do
74
76
local uri = uri_lib.parse(replicas[i])
75
- local port = tonumber(uri.service)
76
77
77
- if uri.host and port then
78
- table.insert(nodes, { host = uri.host, port = port } )
78
+ if uri.host and uri.service then
79
+ table.insert(nodes, uri.host .. ':' .. uri.service )
79
80
end
80
81
end
81
82
82
83
-- if your replication config doesn't contain the current node
83
84
-- you have to add it manually like this:
84
- table.insert(nodes, { host = '192.168.0.1', port = 3301 } )
85
+ table.insert(nodes, '192.168.0.1: 3301' )
85
86
86
87
return nodes
87
88
end
@@ -150,8 +151,14 @@ def refresh_nodes(self, cur_time):
150
151
if not (resp .data and resp .data [0 ]):
151
152
return
152
153
153
- addrs = resp .data [0 ]
154
- if type (addrs ) is list :
154
+ addrs_raw = resp .data [0 ]
155
+ if type (addrs_raw ) is list :
156
+ addrs = []
157
+ for uri_str in addrs_raw :
158
+ addr = parse_uri (uri_str )
159
+ if addr and not addr in addrs :
160
+ addrs .append (addr )
161
+
155
162
self .strategy = self .strategy_class (addrs )
156
163
self .last_nodes_refresh = cur_time
157
164
if not {'host' : self .host , 'port' : self .port } in addrs :
0 commit comments