Skip to content

Commit 4ec3fdf

Browse files
committed
Fix: enforce singleton patten openstack connection
1 parent c0d41dc commit 4ec3fdf

File tree

1 file changed

+14
-4
lines changed
  • src/openstack_mcp_server/tools

1 file changed

+14
-4
lines changed

src/openstack_mcp_server/tools/base.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import atexit
2+
13
import openstack
24

35
from openstack import connection
@@ -10,18 +12,26 @@ class OpenStackConnectionManager:
1012

1113
_connection: connection.Connection | None = None
1214

13-
# TODO: Try/Catch disconnection by token expired case
1415
@classmethod
1516
def get_connection(cls) -> connection.Connection:
16-
"""OpenStack Connection)"""
17+
"""OpenStack Connection"""
1718
if cls._connection is None:
1819
openstack.enable_logging(debug=config.MCP_DEBUG_MODE)
1920
cls._connection = openstack.connect(cloud=config.MCP_CLOUD_NAME)
21+
atexit.register(cls._cleanup)
2022
return cls._connection
2123

22-
# TODO: Close connection
24+
@classmethod
25+
def _cleanup(cls) -> None:
26+
"""Cleanup method called at program exit"""
27+
if cls._connection is not None:
28+
cls._connection.close()
29+
cls._connection = None
30+
31+
32+
_openstack_connection_manager = OpenStackConnectionManager()
2333

2434

2535
def get_openstack_conn():
2636
"""Get OpenStack Connection"""
27-
return OpenStackConnectionManager.get_connection()
37+
return _openstack_connection_manager.get_connection()

0 commit comments

Comments
 (0)