Skip to content
This repository was archived by the owner on Apr 26, 2024. It is now read-only.

Commit eb2c7e5

Browse files
authored
Clean-up type hints in server config (#10915)
By using attrs instead of dicts to store configuration. Also updates some of the attrs classes to use proper type hints and auto_attribs.
1 parent c3ccad7 commit eb2c7e5

File tree

3 files changed

+54
-55
lines changed

3 files changed

+54
-55
lines changed

changelog.d/10915.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Clean-up configuration helper classes for the `ServerConfig` class.

synapse/config/server.py

Lines changed: 49 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
import os.path
2020
import re
2121
from textwrap import indent
22-
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple
22+
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple, Union
2323

2424
import attr
2525
import yaml
@@ -184,49 +184,74 @@ def generate_ip_set(
184184

185185
@attr.s(frozen=True)
186186
class HttpResourceConfig:
187-
names = attr.ib(
188-
type=List[str],
187+
names: List[str] = attr.ib(
189188
factory=list,
190189
validator=attr.validators.deep_iterable(attr.validators.in_(KNOWN_RESOURCES)), # type: ignore
191190
)
192-
compress = attr.ib(
193-
type=bool,
191+
compress: bool = attr.ib(
194192
default=False,
195193
validator=attr.validators.optional(attr.validators.instance_of(bool)), # type: ignore[arg-type]
196194
)
197195

198196

199-
@attr.s(frozen=True)
197+
@attr.s(slots=True, frozen=True, auto_attribs=True)
200198
class HttpListenerConfig:
201199
"""Object describing the http-specific parts of the config of a listener"""
202200

203-
x_forwarded = attr.ib(type=bool, default=False)
204-
resources = attr.ib(type=List[HttpResourceConfig], factory=list)
205-
additional_resources = attr.ib(type=Dict[str, dict], factory=dict)
206-
tag = attr.ib(type=str, default=None)
201+
x_forwarded: bool = False
202+
resources: List[HttpResourceConfig] = attr.ib(factory=list)
203+
additional_resources: Dict[str, dict] = attr.ib(factory=dict)
204+
tag: Optional[str] = None
207205

208206

209-
@attr.s(frozen=True)
207+
@attr.s(slots=True, frozen=True, auto_attribs=True)
210208
class ListenerConfig:
211209
"""Object describing the configuration of a single listener."""
212210

213-
port = attr.ib(type=int, validator=attr.validators.instance_of(int))
214-
bind_addresses = attr.ib(type=List[str])
215-
type = attr.ib(type=str, validator=attr.validators.in_(KNOWN_LISTENER_TYPES))
216-
tls = attr.ib(type=bool, default=False)
211+
port: int = attr.ib(validator=attr.validators.instance_of(int))
212+
bind_addresses: List[str]
213+
type: str = attr.ib(validator=attr.validators.in_(KNOWN_LISTENER_TYPES))
214+
tls: bool = False
217215

218216
# http_options is only populated if type=http
219-
http_options = attr.ib(type=Optional[HttpListenerConfig], default=None)
217+
http_options: Optional[HttpListenerConfig] = None
220218

221219

222-
@attr.s(frozen=True)
220+
@attr.s(slots=True, frozen=True, auto_attribs=True)
223221
class ManholeConfig:
224222
"""Object describing the configuration of the manhole"""
225223

226-
username = attr.ib(type=str, validator=attr.validators.instance_of(str))
227-
password = attr.ib(type=str, validator=attr.validators.instance_of(str))
228-
priv_key = attr.ib(type=Optional[Key])
229-
pub_key = attr.ib(type=Optional[Key])
224+
username: str = attr.ib(validator=attr.validators.instance_of(str))
225+
password: str = attr.ib(validator=attr.validators.instance_of(str))
226+
priv_key: Optional[Key]
227+
pub_key: Optional[Key]
228+
229+
230+
@attr.s(slots=True, frozen=True, auto_attribs=True)
231+
class RetentionConfig:
232+
"""Object describing the configuration of the manhole"""
233+
234+
interval: int
235+
shortest_max_lifetime: Optional[int]
236+
longest_max_lifetime: Optional[int]
237+
238+
239+
@attr.s(frozen=True)
240+
class LimitRemoteRoomsConfig:
241+
enabled: bool = attr.ib(validator=attr.validators.instance_of(bool), default=False)
242+
complexity: Union[float, int] = attr.ib(
243+
validator=attr.validators.instance_of(
244+
(float, int) # type: ignore[arg-type] # noqa
245+
),
246+
default=1.0,
247+
)
248+
complexity_error: str = attr.ib(
249+
validator=attr.validators.instance_of(str),
250+
default=ROOM_COMPLEXITY_TOO_GREAT,
251+
)
252+
admins_can_join: bool = attr.ib(
253+
validator=attr.validators.instance_of(bool), default=False
254+
)
230255

231256

232257
class ServerConfig(Config):
@@ -519,7 +544,7 @@ def read_config(self, config, **kwargs):
519544
" greater than 'allowed_lifetime_max'"
520545
)
521546

522-
self.retention_purge_jobs: List[Dict[str, Optional[int]]] = []
547+
self.retention_purge_jobs: List[RetentionConfig] = []
523548
for purge_job_config in retention_config.get("purge_jobs", []):
524549
interval_config = purge_job_config.get("interval")
525550

@@ -553,20 +578,12 @@ def read_config(self, config, **kwargs):
553578
)
554579

555580
self.retention_purge_jobs.append(
556-
{
557-
"interval": interval,
558-
"shortest_max_lifetime": shortest_max_lifetime,
559-
"longest_max_lifetime": longest_max_lifetime,
560-
}
581+
RetentionConfig(interval, shortest_max_lifetime, longest_max_lifetime)
561582
)
562583

563584
if not self.retention_purge_jobs:
564585
self.retention_purge_jobs = [
565-
{
566-
"interval": self.parse_duration("1d"),
567-
"shortest_max_lifetime": None,
568-
"longest_max_lifetime": None,
569-
}
586+
RetentionConfig(self.parse_duration("1d"), None, None)
570587
]
571588

572589
self.listeners = [parse_listener_def(x) for x in config.get("listeners", [])]
@@ -591,25 +608,6 @@ def read_config(self, config, **kwargs):
591608
self.gc_thresholds = read_gc_thresholds(config.get("gc_thresholds", None))
592609
self.gc_seconds = self.read_gc_intervals(config.get("gc_min_interval", None))
593610

594-
@attr.s
595-
class LimitRemoteRoomsConfig:
596-
enabled = attr.ib(
597-
validator=attr.validators.instance_of(bool), default=False
598-
)
599-
complexity = attr.ib(
600-
validator=attr.validators.instance_of(
601-
(float, int) # type: ignore[arg-type] # noqa
602-
),
603-
default=1.0,
604-
)
605-
complexity_error = attr.ib(
606-
validator=attr.validators.instance_of(str),
607-
default=ROOM_COMPLEXITY_TOO_GREAT,
608-
)
609-
admins_can_join = attr.ib(
610-
validator=attr.validators.instance_of(bool), default=False
611-
)
612-
613611
self.limit_remote_rooms = LimitRemoteRoomsConfig(
614612
**(config.get("limit_remote_rooms") or {})
615613
)

synapse/handlers/pagination.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,16 @@ def __init__(self, hs: "HomeServer"):
9292

9393
if hs.config.worker.run_background_tasks and hs.config.retention_enabled:
9494
# Run the purge jobs described in the configuration file.
95-
for job in hs.config.retention_purge_jobs:
95+
for job in hs.config.server.retention_purge_jobs:
9696
logger.info("Setting up purge job with config: %s", job)
9797

9898
self.clock.looping_call(
9999
run_as_background_process,
100-
job["interval"],
100+
job.interval,
101101
"purge_history_for_rooms_in_range",
102102
self.purge_history_for_rooms_in_range,
103-
job["shortest_max_lifetime"],
104-
job["longest_max_lifetime"],
103+
job.shortest_max_lifetime,
104+
job.longest_max_lifetime,
105105
)
106106

107107
async def purge_history_for_rooms_in_range(

0 commit comments

Comments
 (0)