forked from MirrorHub/synapse
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.
This commit is contained in:
parent
c3ccad7785
commit
eb2c7e51c4
3 changed files with 54 additions and 55 deletions
1
changelog.d/10915.misc
Normal file
1
changelog.d/10915.misc
Normal file
|
@ -0,0 +1 @@
|
||||||
|
Clean-up configuration helper classes for the `ServerConfig` class.
|
|
@ -19,7 +19,7 @@ import logging
|
||||||
import os.path
|
import os.path
|
||||||
import re
|
import re
|
||||||
from textwrap import indent
|
from textwrap import indent
|
||||||
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple
|
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple, Union
|
||||||
|
|
||||||
import attr
|
import attr
|
||||||
import yaml
|
import yaml
|
||||||
|
@ -184,49 +184,74 @@ KNOWN_RESOURCES = {
|
||||||
|
|
||||||
@attr.s(frozen=True)
|
@attr.s(frozen=True)
|
||||||
class HttpResourceConfig:
|
class HttpResourceConfig:
|
||||||
names = attr.ib(
|
names: List[str] = attr.ib(
|
||||||
type=List[str],
|
|
||||||
factory=list,
|
factory=list,
|
||||||
validator=attr.validators.deep_iterable(attr.validators.in_(KNOWN_RESOURCES)), # type: ignore
|
validator=attr.validators.deep_iterable(attr.validators.in_(KNOWN_RESOURCES)), # type: ignore
|
||||||
)
|
)
|
||||||
compress = attr.ib(
|
compress: bool = attr.ib(
|
||||||
type=bool,
|
|
||||||
default=False,
|
default=False,
|
||||||
validator=attr.validators.optional(attr.validators.instance_of(bool)), # type: ignore[arg-type]
|
validator=attr.validators.optional(attr.validators.instance_of(bool)), # type: ignore[arg-type]
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@attr.s(frozen=True)
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
||||||
class HttpListenerConfig:
|
class HttpListenerConfig:
|
||||||
"""Object describing the http-specific parts of the config of a listener"""
|
"""Object describing the http-specific parts of the config of a listener"""
|
||||||
|
|
||||||
x_forwarded = attr.ib(type=bool, default=False)
|
x_forwarded: bool = False
|
||||||
resources = attr.ib(type=List[HttpResourceConfig], factory=list)
|
resources: List[HttpResourceConfig] = attr.ib(factory=list)
|
||||||
additional_resources = attr.ib(type=Dict[str, dict], factory=dict)
|
additional_resources: Dict[str, dict] = attr.ib(factory=dict)
|
||||||
tag = attr.ib(type=str, default=None)
|
tag: Optional[str] = None
|
||||||
|
|
||||||
|
|
||||||
@attr.s(frozen=True)
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
||||||
class ListenerConfig:
|
class ListenerConfig:
|
||||||
"""Object describing the configuration of a single listener."""
|
"""Object describing the configuration of a single listener."""
|
||||||
|
|
||||||
port = attr.ib(type=int, validator=attr.validators.instance_of(int))
|
port: int = attr.ib(validator=attr.validators.instance_of(int))
|
||||||
bind_addresses = attr.ib(type=List[str])
|
bind_addresses: List[str]
|
||||||
type = attr.ib(type=str, validator=attr.validators.in_(KNOWN_LISTENER_TYPES))
|
type: str = attr.ib(validator=attr.validators.in_(KNOWN_LISTENER_TYPES))
|
||||||
tls = attr.ib(type=bool, default=False)
|
tls: bool = False
|
||||||
|
|
||||||
# http_options is only populated if type=http
|
# http_options is only populated if type=http
|
||||||
http_options = attr.ib(type=Optional[HttpListenerConfig], default=None)
|
http_options: Optional[HttpListenerConfig] = None
|
||||||
|
|
||||||
|
|
||||||
@attr.s(frozen=True)
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
||||||
class ManholeConfig:
|
class ManholeConfig:
|
||||||
"""Object describing the configuration of the manhole"""
|
"""Object describing the configuration of the manhole"""
|
||||||
|
|
||||||
username = attr.ib(type=str, validator=attr.validators.instance_of(str))
|
username: str = attr.ib(validator=attr.validators.instance_of(str))
|
||||||
password = attr.ib(type=str, validator=attr.validators.instance_of(str))
|
password: str = attr.ib(validator=attr.validators.instance_of(str))
|
||||||
priv_key = attr.ib(type=Optional[Key])
|
priv_key: Optional[Key]
|
||||||
pub_key = attr.ib(type=Optional[Key])
|
pub_key: Optional[Key]
|
||||||
|
|
||||||
|
|
||||||
|
@attr.s(slots=True, frozen=True, auto_attribs=True)
|
||||||
|
class RetentionConfig:
|
||||||
|
"""Object describing the configuration of the manhole"""
|
||||||
|
|
||||||
|
interval: int
|
||||||
|
shortest_max_lifetime: Optional[int]
|
||||||
|
longest_max_lifetime: Optional[int]
|
||||||
|
|
||||||
|
|
||||||
|
@attr.s(frozen=True)
|
||||||
|
class LimitRemoteRoomsConfig:
|
||||||
|
enabled: bool = attr.ib(validator=attr.validators.instance_of(bool), default=False)
|
||||||
|
complexity: Union[float, int] = attr.ib(
|
||||||
|
validator=attr.validators.instance_of(
|
||||||
|
(float, int) # type: ignore[arg-type] # noqa
|
||||||
|
),
|
||||||
|
default=1.0,
|
||||||
|
)
|
||||||
|
complexity_error: str = attr.ib(
|
||||||
|
validator=attr.validators.instance_of(str),
|
||||||
|
default=ROOM_COMPLEXITY_TOO_GREAT,
|
||||||
|
)
|
||||||
|
admins_can_join: bool = attr.ib(
|
||||||
|
validator=attr.validators.instance_of(bool), default=False
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
class ServerConfig(Config):
|
class ServerConfig(Config):
|
||||||
|
@ -519,7 +544,7 @@ class ServerConfig(Config):
|
||||||
" greater than 'allowed_lifetime_max'"
|
" greater than 'allowed_lifetime_max'"
|
||||||
)
|
)
|
||||||
|
|
||||||
self.retention_purge_jobs: List[Dict[str, Optional[int]]] = []
|
self.retention_purge_jobs: List[RetentionConfig] = []
|
||||||
for purge_job_config in retention_config.get("purge_jobs", []):
|
for purge_job_config in retention_config.get("purge_jobs", []):
|
||||||
interval_config = purge_job_config.get("interval")
|
interval_config = purge_job_config.get("interval")
|
||||||
|
|
||||||
|
@ -553,20 +578,12 @@ class ServerConfig(Config):
|
||||||
)
|
)
|
||||||
|
|
||||||
self.retention_purge_jobs.append(
|
self.retention_purge_jobs.append(
|
||||||
{
|
RetentionConfig(interval, shortest_max_lifetime, longest_max_lifetime)
|
||||||
"interval": interval,
|
|
||||||
"shortest_max_lifetime": shortest_max_lifetime,
|
|
||||||
"longest_max_lifetime": longest_max_lifetime,
|
|
||||||
}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
if not self.retention_purge_jobs:
|
if not self.retention_purge_jobs:
|
||||||
self.retention_purge_jobs = [
|
self.retention_purge_jobs = [
|
||||||
{
|
RetentionConfig(self.parse_duration("1d"), None, None)
|
||||||
"interval": self.parse_duration("1d"),
|
|
||||||
"shortest_max_lifetime": None,
|
|
||||||
"longest_max_lifetime": None,
|
|
||||||
}
|
|
||||||
]
|
]
|
||||||
|
|
||||||
self.listeners = [parse_listener_def(x) for x in config.get("listeners", [])]
|
self.listeners = [parse_listener_def(x) for x in config.get("listeners", [])]
|
||||||
|
@ -591,25 +608,6 @@ class ServerConfig(Config):
|
||||||
self.gc_thresholds = read_gc_thresholds(config.get("gc_thresholds", None))
|
self.gc_thresholds = read_gc_thresholds(config.get("gc_thresholds", None))
|
||||||
self.gc_seconds = self.read_gc_intervals(config.get("gc_min_interval", None))
|
self.gc_seconds = self.read_gc_intervals(config.get("gc_min_interval", None))
|
||||||
|
|
||||||
@attr.s
|
|
||||||
class LimitRemoteRoomsConfig:
|
|
||||||
enabled = attr.ib(
|
|
||||||
validator=attr.validators.instance_of(bool), default=False
|
|
||||||
)
|
|
||||||
complexity = attr.ib(
|
|
||||||
validator=attr.validators.instance_of(
|
|
||||||
(float, int) # type: ignore[arg-type] # noqa
|
|
||||||
),
|
|
||||||
default=1.0,
|
|
||||||
)
|
|
||||||
complexity_error = attr.ib(
|
|
||||||
validator=attr.validators.instance_of(str),
|
|
||||||
default=ROOM_COMPLEXITY_TOO_GREAT,
|
|
||||||
)
|
|
||||||
admins_can_join = attr.ib(
|
|
||||||
validator=attr.validators.instance_of(bool), default=False
|
|
||||||
)
|
|
||||||
|
|
||||||
self.limit_remote_rooms = LimitRemoteRoomsConfig(
|
self.limit_remote_rooms = LimitRemoteRoomsConfig(
|
||||||
**(config.get("limit_remote_rooms") or {})
|
**(config.get("limit_remote_rooms") or {})
|
||||||
)
|
)
|
||||||
|
|
|
@ -92,16 +92,16 @@ class PaginationHandler:
|
||||||
|
|
||||||
if hs.config.worker.run_background_tasks and hs.config.retention_enabled:
|
if hs.config.worker.run_background_tasks and hs.config.retention_enabled:
|
||||||
# Run the purge jobs described in the configuration file.
|
# Run the purge jobs described in the configuration file.
|
||||||
for job in hs.config.retention_purge_jobs:
|
for job in hs.config.server.retention_purge_jobs:
|
||||||
logger.info("Setting up purge job with config: %s", job)
|
logger.info("Setting up purge job with config: %s", job)
|
||||||
|
|
||||||
self.clock.looping_call(
|
self.clock.looping_call(
|
||||||
run_as_background_process,
|
run_as_background_process,
|
||||||
job["interval"],
|
job.interval,
|
||||||
"purge_history_for_rooms_in_range",
|
"purge_history_for_rooms_in_range",
|
||||||
self.purge_history_for_rooms_in_range,
|
self.purge_history_for_rooms_in_range,
|
||||||
job["shortest_max_lifetime"],
|
job.shortest_max_lifetime,
|
||||||
job["longest_max_lifetime"],
|
job.longest_max_lifetime,
|
||||||
)
|
)
|
||||||
|
|
||||||
async def purge_history_for_rooms_in_range(
|
async def purge_history_for_rooms_in_range(
|
||||||
|
|
Loading…
Reference in a new issue