File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 22
33import asyncio
44
5- from dqliteclient .connection import DqliteConnection
5+ from dqliteclient .connection import DqliteConnection , _parse_address
66from dqliteclient .exceptions import ClusterError , DqliteConnectionError , OperationalError
77from dqliteclient .node_store import MemoryNodeStore , NodeInfo , NodeStore
88from dqliteclient .protocol import DqliteProtocol
@@ -65,8 +65,7 @@ async def find_leader(self) -> str:
6565
6666 async def _query_leader (self , address : str ) -> str | None :
6767 """Query a node for the current leader."""
68- host , port_str = address .rsplit (":" , 1 )
69- port = int (port_str )
68+ host , port = _parse_address (address )
7069
7170 try :
7271 reader , writer = await asyncio .wait_for (
Original file line number Diff line number Diff line change 99from dqliteclient .protocol import DqliteProtocol
1010
1111
12+ def _parse_address (address : str ) -> tuple [str , int ]:
13+ """Parse a host:port address string, handling IPv6 brackets."""
14+ if address .startswith ("[" ):
15+ # Bracketed IPv6: [host]:port
16+ bracket_end = address .index ("]" )
17+ host = address [1 :bracket_end ]
18+ port_str = address [bracket_end + 2 :] # Skip ']:
19+ else :
20+ host , port_str = address .rsplit (":" , 1 )
21+ return host , int (port_str )
22+
23+
1224class DqliteConnection :
1325 """High-level async connection to a dqlite database."""
1426
@@ -48,8 +60,7 @@ async def connect(self) -> None:
4860 if self ._protocol is not None :
4961 return
5062
51- host , port_str = self ._address .rsplit (":" , 1 )
52- port = int (port_str )
63+ host , port = _parse_address (self ._address )
5364
5465 try :
5566 reader , writer = await asyncio .wait_for (
Original file line number Diff line number Diff line change 88from dqliteclient .exceptions import DqliteConnectionError
99
1010
11+ class TestParseAddress :
12+ def test_ipv4 (self ) -> None :
13+ from dqliteclient .connection import _parse_address
14+
15+ assert _parse_address ("localhost:9001" ) == ("localhost" , 9001 )
16+
17+ def test_ipv4_ip (self ) -> None :
18+ from dqliteclient .connection import _parse_address
19+
20+ assert _parse_address ("192.168.1.1:9001" ) == ("192.168.1.1" , 9001 )
21+
22+ def test_ipv6_bracketed (self ) -> None :
23+ from dqliteclient .connection import _parse_address
24+
25+ assert _parse_address ("[::1]:9001" ) == ("::1" , 9001 )
26+
27+ def test_ipv6_full_bracketed (self ) -> None :
28+ from dqliteclient .connection import _parse_address
29+
30+ assert _parse_address ("[2001:db8::1]:9001" ) == ("2001:db8::1" , 9001 )
31+
32+
1133class TestDqliteConnection :
1234 def test_init (self ) -> None :
1335 conn = DqliteConnection ("localhost:9001" , database = "test" , timeout = 5.0 )
You can’t perform that action at this time.
0 commit comments