aboutsummaryrefslogtreecommitdiff
path: root/setup
diff options
context:
space:
mode:
authorNeil Alexander <neilalexander@users.noreply.github.com>2021-01-22 14:16:59 +0000
committerGitHub <noreply@github.com>2021-01-22 14:16:59 +0000
commit805a74892efcd18fdcaa14a68a1ba7dfa9486172 (patch)
tree1c7623674910540be5162fe0ec0cf49d7f193f9f /setup
parent745ee20b9049dd9bdfc1c7e84cb2a4aa4fd66f4f (diff)
DNS caching (#1728)
* Allow configuring DNS cache * Update sample configs * Fix build errors * Fix time resolution * Default 5m * In seconds * Use WithDNScache * Correct field name * Update go.mod/go.sum to matrix-org/gomatrixserverlib#251
Diffstat (limited to 'setup')
-rw-r--r--setup/base.go28
-rw-r--r--setup/config/config_global.go25
2 files changed, 51 insertions, 2 deletions
diff --git a/setup/base.go b/setup/base.go
index acbf2d35..021dd2b3 100644
--- a/setup/base.go
+++ b/setup/base.go
@@ -73,6 +73,7 @@ type BaseDendrite struct {
httpClient *http.Client
Cfg *config.Dendrite
Caches *caching.Caches
+ DNSCache *gomatrixserverlib.DNSCache
// KafkaConsumer sarama.Consumer
// KafkaProducer sarama.SyncProducer
}
@@ -111,6 +112,20 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string, useHTTPAPIs boo
logrus.WithError(err).Warnf("Failed to create cache")
}
+ var dnsCache *gomatrixserverlib.DNSCache
+ if cfg.Global.DNSCache.Enabled {
+ lifetime := time.Second * cfg.Global.DNSCache.CacheLifetime
+ dnsCache = gomatrixserverlib.NewDNSCache(
+ cfg.Global.DNSCache.CacheSize,
+ lifetime,
+ )
+ logrus.Infof(
+ "DNS cache enabled (size %d, lifetime %s)",
+ cfg.Global.DNSCache.CacheSize,
+ lifetime,
+ )
+ }
+
apiClient := http.Client{
Timeout: time.Minute * 10,
Transport: &http2.Transport{
@@ -152,6 +167,7 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string, useHTTPAPIs boo
tracerCloser: closer,
Cfg: cfg,
Caches: cache,
+ DNSCache: dnsCache,
PublicClientAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicClientPathPrefix).Subrouter().UseEncodedPath(),
PublicFederationAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicFederationPathPrefix).Subrouter().UseEncodedPath(),
PublicKeyAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.PublicKeyPathPrefix).Subrouter().UseEncodedPath(),
@@ -252,8 +268,12 @@ func (b *BaseDendrite) CreateClient() *gomatrixserverlib.Client {
if b.Cfg.Global.DisableFederation {
return gomatrixserverlib.NewClientWithTransport(noOpHTTPTransport)
}
+ opts := []interface{}{}
+ if b.Cfg.Global.DNSCache.Enabled {
+ opts = append(opts, gomatrixserverlib.WithDNSCache{DNSCache: b.DNSCache})
+ }
client := gomatrixserverlib.NewClient(
- b.Cfg.FederationSender.DisableTLSValidation,
+ b.Cfg.FederationSender.DisableTLSValidation, opts...,
)
client.SetUserAgent(fmt.Sprintf("Dendrite/%s", internal.VersionString()))
return client
@@ -268,9 +288,13 @@ func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationCli
b.Cfg.FederationSender.DisableTLSValidation, noOpHTTPTransport,
)
}
+ opts := []interface{}{}
+ if b.Cfg.Global.DNSCache.Enabled {
+ opts = append(opts, gomatrixserverlib.WithDNSCache{DNSCache: b.DNSCache})
+ }
client := gomatrixserverlib.NewFederationClientWithTimeout(
b.Cfg.Global.ServerName, b.Cfg.Global.KeyID, b.Cfg.Global.PrivateKey,
- b.Cfg.FederationSender.DisableTLSValidation, time.Minute*5,
+ b.Cfg.FederationSender.DisableTLSValidation, time.Minute*5, opts...,
)
client.SetUserAgent(fmt.Sprintf("Dendrite/%s", internal.VersionString()))
return client
diff --git a/setup/config/config_global.go b/setup/config/config_global.go
index 95652217..d4b068db 100644
--- a/setup/config/config_global.go
+++ b/setup/config/config_global.go
@@ -48,6 +48,9 @@ type Global struct {
// Metrics configuration
Metrics Metrics `yaml:"metrics"`
+
+ // DNS caching options for all outbound HTTP requests
+ DNSCache DNSCacheOptions `yaml:"dns_cache"`
}
func (c *Global) Defaults() {
@@ -59,6 +62,7 @@ func (c *Global) Defaults() {
c.Kafka.Defaults()
c.Metrics.Defaults()
+ c.DNSCache.Defaults()
}
func (c *Global) Verify(configErrs *ConfigErrors, isMonolith bool) {
@@ -67,6 +71,7 @@ func (c *Global) Verify(configErrs *ConfigErrors, isMonolith bool) {
c.Kafka.Verify(configErrs, isMonolith)
c.Metrics.Verify(configErrs, isMonolith)
+ c.DNSCache.Verify(configErrs, isMonolith)
}
type OldVerifyKeys struct {
@@ -140,3 +145,23 @@ func (c DatabaseOptions) MaxOpenConns() int {
func (c DatabaseOptions) ConnMaxLifetime() time.Duration {
return time.Duration(c.ConnMaxLifetimeSeconds) * time.Second
}
+
+type DNSCacheOptions struct {
+ // Whether the DNS cache is enabled or not
+ Enabled bool `yaml:"enabled"`
+ // How many entries to store in the DNS cache at a given time
+ CacheSize int `yaml:"cache_size"`
+ // How long a cache entry should be considered valid for
+ CacheLifetime time.Duration `yaml:"cache_lifetime"`
+}
+
+func (c *DNSCacheOptions) Defaults() {
+ c.Enabled = false
+ c.CacheSize = 256
+ c.CacheLifetime = time.Minute * 5
+}
+
+func (c *DNSCacheOptions) Verify(configErrs *ConfigErrors, isMonolith bool) {
+ checkPositive(configErrs, "cache_size", int64(c.CacheSize))
+ checkPositive(configErrs, "cache_lifetime", int64(c.CacheLifetime))
+}