@@ -61,13 +61,14 @@ class TelegramBridge(Bridge):
61
61
matrix_class = MatrixHandler
62
62
63
63
config : Config
64
+ context : Context
64
65
session_container : AlchemySessionContainer
65
66
bot : Bot
66
67
67
68
periodic_active_metrics_task : asyncio .Task
68
69
is_blocked : bool = False
69
70
70
- periodic_sync_task : asyncio .Task
71
+ periodic_sync_task : asyncio .Task = None
71
72
72
73
def prepare_db (self ) -> None :
73
74
super ().prepare_db ()
@@ -76,29 +77,28 @@ def prepare_db(self) -> None:
76
77
engine = self .db , table_base = Base , session = False ,
77
78
table_prefix = "telethon_" , manage_tables = False )
78
79
79
- def _prepare_website (self , context : Context ) -> None :
80
+ def _prepare_website (self ) -> None :
80
81
if self .config ["appservice.public.enabled" ]:
81
82
public_website = PublicBridgeWebsite (self .loop )
82
83
self .az .app .add_subapp (self .config ["appservice.public.prefix" ], public_website .app )
83
- context .public_website = public_website
84
+ self . context .public_website = public_website
84
85
85
86
if self .config ["appservice.provisioning.enabled" ]:
86
- provisioning_api = ProvisioningAPI (context )
87
+ provisioning_api = ProvisioningAPI (self . context )
87
88
self .az .app .add_subapp (self .config ["appservice.provisioning.prefix" ],
88
89
provisioning_api .app )
89
- context .provisioning_api = provisioning_api
90
+ self . context .provisioning_api = provisioning_api
90
91
91
92
def prepare_bridge (self ) -> None :
92
93
self .bot = init_bot (self .config )
93
- context = Context (self .az , self .config , self .loop , self .session_container , self , self .bot )
94
- self ._prepare_website (context )
95
- self .matrix = context .mx = MatrixHandler (context )
94
+ self . context = Context (self .az , self .config , self .loop , self .session_container , self , self .bot )
95
+ self ._prepare_website ()
96
+ self .matrix = self . context .mx = MatrixHandler (self . context )
96
97
97
- init_abstract_user (context )
98
- init_formatter (context )
99
- init_portal (context )
100
- self .add_startup_actions (init_puppet (context ))
101
- self .add_startup_actions (init_user (context ))
98
+ init_abstract_user (self .context )
99
+ init_formatter (self .context )
100
+ init_portal (self .context )
101
+ self .add_startup_actions (init_puppet (self .context ))
102
102
103
103
if self .bot :
104
104
self .add_startup_actions (self .bot .start ())
@@ -109,6 +109,22 @@ def prepare_bridge(self) -> None:
109
109
if self .config ['bridge.limits.enable_activity_tracking' ] is not False :
110
110
self .periodic_sync_task = self .loop .create_task (self ._loop_active_puppet_metric ())
111
111
112
+ async def start (self ) -> None :
113
+ await super ().start ()
114
+
115
+ semaphore = None
116
+ concurrency = self .config ['telegram.connection.concurrent_connections_startup' ]
117
+ if concurrency :
118
+ semaphore = asyncio .Semaphore (concurrency )
119
+ await semaphore .acquire ()
120
+
121
+ async def sem_task (task ):
122
+ if not semaphore :
123
+ return await task
124
+ async with semaphore :
125
+ return await task
126
+
127
+ await asyncio .gather (* (sem_task (task ) for task in init_user (self .context )))
112
128
113
129
async def resend_bridge_info (self ) -> None :
114
130
self .config ["bridge.resend_bridge_info" ] = False
0 commit comments