aboutsummaryrefslogtreecommitdiff
path: root/setup/base
diff options
context:
space:
mode:
authorTill <2353100+S7evinK@users.noreply.github.com>2023-03-22 09:21:32 +0100
committerGitHub <noreply@github.com>2023-03-22 09:21:32 +0100
commit5e85a00cb36c3d343cd5b6f6a18435989724a135 (patch)
treeb22c34dd0a6cdc04025b90086843f9084a876412 /setup/base
parentec6879e5ae2919c903707475ce8d72244b2a6847 (diff)
Remove `BaseDendrite` (#3023)
Removes `BaseDendrite` to, hopefully, make testing and composing of components easier in the future.
Diffstat (limited to 'setup/base')
-rw-r--r--setup/base/base.go242
-rw-r--r--setup/base/base_test.go20
-rw-r--r--setup/base/sanity_other.go2
-rw-r--r--setup/base/sanity_unix.go2
4 files changed, 63 insertions, 203 deletions
diff --git a/setup/base/base.go b/setup/base/base.go
index 8c9b06d0..e5fd6fed 100644
--- a/setup/base/base.go
+++ b/setup/base/base.go
@@ -22,30 +22,24 @@ import (
"errors"
"fmt"
"html/template"
- "io"
"io/fs"
"net"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
- "sync"
"syscall"
"time"
- "github.com/getsentry/sentry-go"
sentryhttp "github.com/getsentry/sentry-go/http"
- "github.com/matrix-org/dendrite/setup/jetstream"
"github.com/matrix-org/gomatrixserverlib"
"github.com/prometheus/client_golang/prometheus/promhttp"
"go.uber.org/atomic"
- "github.com/matrix-org/dendrite/internal"
- "github.com/matrix-org/dendrite/internal/httputil"
- "github.com/matrix-org/dendrite/internal/sqlutil"
-
"github.com/gorilla/mux"
"github.com/kardianos/minwinsvc"
+ "github.com/matrix-org/dendrite/internal"
+ "github.com/matrix-org/dendrite/internal/httputil"
"github.com/sirupsen/logrus"
@@ -56,154 +50,22 @@ import (
//go:embed static/*.gotmpl
var staticContent embed.FS
-// BaseDendrite is a base for creating new instances of dendrite. It parses
-// command line flags and config, and exposes methods for creating various
-// resources. All errors are handled by logging then exiting, so all methods
-// should only be used during start up.
-// Must be closed when shutting down.
-type BaseDendrite struct {
- *process.ProcessContext
- tracerCloser io.Closer
- Routers httputil.Routers
- NATS *jetstream.NATSInstance
- Cfg *config.Dendrite
- DNSCache *gomatrixserverlib.DNSCache
- ConnectionManager sqlutil.Connections
- EnableMetrics bool
- startupLock sync.Mutex
-}
-
const HTTPServerTimeout = time.Minute * 5
-type BaseDendriteOptions int
-
-const (
- DisableMetrics BaseDendriteOptions = iota
-)
-
-// NewBaseDendrite creates a new instance to be used by a component.
-func NewBaseDendrite(cfg *config.Dendrite, options ...BaseDendriteOptions) *BaseDendrite {
- platformSanityChecks()
- enableMetrics := true
- for _, opt := range options {
- switch opt {
- case DisableMetrics:
- enableMetrics = false
- }
- }
-
- configErrors := &config.ConfigErrors{}
- cfg.Verify(configErrors)
- if len(*configErrors) > 0 {
- for _, err := range *configErrors {
- logrus.Errorf("Configuration error: %s", err)
- }
- logrus.Fatalf("Failed to start due to configuration errors")
- }
-
- internal.SetupStdLogging()
- internal.SetupHookLogging(cfg.Logging)
- internal.SetupPprof()
-
- logrus.Infof("Dendrite version %s", internal.VersionString())
-
- if !cfg.ClientAPI.RegistrationDisabled && cfg.ClientAPI.OpenRegistrationWithoutVerificationEnabled {
- logrus.Warn("Open registration is enabled")
- }
-
- closer, err := cfg.SetupTracing()
- if err != nil {
- logrus.WithError(err).Panicf("failed to start opentracing")
- }
-
- if cfg.Global.Sentry.Enabled {
- logrus.Info("Setting up Sentry for debugging...")
- err = sentry.Init(sentry.ClientOptions{
- Dsn: cfg.Global.Sentry.DSN,
- Environment: cfg.Global.Sentry.Environment,
- Debug: true,
- ServerName: string(cfg.Global.ServerName),
- Release: "dendrite@" + internal.VersionString(),
- AttachStacktrace: true,
- })
- if err != nil {
- logrus.WithError(err).Panic("failed to start Sentry")
- }
- }
-
- var dnsCache *gomatrixserverlib.DNSCache
- if cfg.Global.DNSCache.Enabled {
- dnsCache = gomatrixserverlib.NewDNSCache(
- cfg.Global.DNSCache.CacheSize,
- cfg.Global.DNSCache.CacheLifetime,
- )
- logrus.Infof(
- "DNS cache enabled (size %d, lifetime %s)",
- cfg.Global.DNSCache.CacheSize,
- cfg.Global.DNSCache.CacheLifetime,
- )
- }
-
- // If we're in monolith mode, we'll set up a global pool of database
- // connections. A component is welcome to use this pool if they don't
- // have a separate database config of their own.
- cm := sqlutil.NewConnectionManager()
- if cfg.Global.DatabaseOptions.ConnectionString != "" {
- if cfg.Global.DatabaseOptions.ConnectionString.IsSQLite() {
- logrus.Panic("Using a global database connection pool is not supported with SQLite databases")
- }
- _, _, err := cm.Connection(&cfg.Global.DatabaseOptions)
- if err != nil {
- logrus.WithError(err).Panic("Failed to set up global database connections")
- }
- logrus.Debug("Using global database connection pool")
- }
-
- // Ideally we would only use SkipClean on routes which we know can allow '/' but due to
- // https://github.com/gorilla/mux/issues/460 we have to attach this at the top router.
- // When used in conjunction with UseEncodedPath() we get the behaviour we want when parsing
- // path parameters:
- // /foo/bar%2Fbaz == [foo, bar%2Fbaz] (from UseEncodedPath)
- // /foo/bar%2F%2Fbaz == [foo, bar%2F%2Fbaz] (from SkipClean)
- // In particular, rooms v3 event IDs are not urlsafe and can include '/' and because they
- // are randomly generated it results in flakey tests.
- // We need to be careful with media APIs if they read from a filesystem to make sure they
- // are not inadvertently reading paths without cleaning, else this could introduce a
- // directory traversal attack e.g /../../../etc/passwd
-
- return &BaseDendrite{
- ProcessContext: process.NewProcessContext(),
- tracerCloser: closer,
- Cfg: cfg,
- DNSCache: dnsCache,
- Routers: httputil.NewRouters(),
- NATS: &jetstream.NATSInstance{},
- ConnectionManager: cm,
- EnableMetrics: enableMetrics,
- }
-}
-
-// Close implements io.Closer
-func (b *BaseDendrite) Close() error {
- b.ProcessContext.ShutdownDendrite()
- b.ProcessContext.WaitForShutdown()
- return b.tracerCloser.Close()
-}
-
// CreateClient creates a new client (normally used for media fetch requests).
// Should only be called once per component.
-func (b *BaseDendrite) CreateClient() *gomatrixserverlib.Client {
- if b.Cfg.Global.DisableFederation {
+func CreateClient(cfg *config.Dendrite, dnsCache *gomatrixserverlib.DNSCache) *gomatrixserverlib.Client {
+ if cfg.Global.DisableFederation {
return gomatrixserverlib.NewClient(
gomatrixserverlib.WithTransport(noOpHTTPTransport),
)
}
opts := []gomatrixserverlib.ClientOption{
- gomatrixserverlib.WithSkipVerify(b.Cfg.FederationAPI.DisableTLSValidation),
+ gomatrixserverlib.WithSkipVerify(cfg.FederationAPI.DisableTLSValidation),
gomatrixserverlib.WithWellKnownSRVLookups(true),
}
- if b.Cfg.Global.DNSCache.Enabled {
- opts = append(opts, gomatrixserverlib.WithDNSCache(b.DNSCache))
+ if cfg.Global.DNSCache.Enabled && dnsCache != nil {
+ opts = append(opts, gomatrixserverlib.WithDNSCache(dnsCache))
}
client := gomatrixserverlib.NewClient(opts...)
client.SetUserAgent(fmt.Sprintf("Dendrite/%s", internal.VersionString()))
@@ -212,20 +74,20 @@ func (b *BaseDendrite) CreateClient() *gomatrixserverlib.Client {
// CreateFederationClient creates a new federation client. Should only be called
// once per component.
-func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationClient {
- identities := b.Cfg.Global.SigningIdentities()
- if b.Cfg.Global.DisableFederation {
+func CreateFederationClient(cfg *config.Dendrite, dnsCache *gomatrixserverlib.DNSCache) *gomatrixserverlib.FederationClient {
+ identities := cfg.Global.SigningIdentities()
+ if cfg.Global.DisableFederation {
return gomatrixserverlib.NewFederationClient(
identities, gomatrixserverlib.WithTransport(noOpHTTPTransport),
)
}
opts := []gomatrixserverlib.ClientOption{
gomatrixserverlib.WithTimeout(time.Minute * 5),
- gomatrixserverlib.WithSkipVerify(b.Cfg.FederationAPI.DisableTLSValidation),
- gomatrixserverlib.WithKeepAlives(!b.Cfg.FederationAPI.DisableHTTPKeepalives),
+ gomatrixserverlib.WithSkipVerify(cfg.FederationAPI.DisableTLSValidation),
+ gomatrixserverlib.WithKeepAlives(!cfg.FederationAPI.DisableHTTPKeepalives),
}
- if b.Cfg.Global.DNSCache.Enabled {
- opts = append(opts, gomatrixserverlib.WithDNSCache(b.DNSCache))
+ if cfg.Global.DNSCache.Enabled {
+ opts = append(opts, gomatrixserverlib.WithDNSCache(dnsCache))
}
client := gomatrixserverlib.NewFederationClient(
identities, opts...,
@@ -234,12 +96,12 @@ func (b *BaseDendrite) CreateFederationClient() *gomatrixserverlib.FederationCli
return client
}
-func (b *BaseDendrite) ConfigureAdminEndpoints() {
- b.Routers.DendriteAdmin.HandleFunc("/monitor/up", func(w http.ResponseWriter, r *http.Request) {
+func ConfigureAdminEndpoints(processContext *process.ProcessContext, routers httputil.Routers) {
+ routers.DendriteAdmin.HandleFunc("/monitor/up", func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(200)
})
- b.Routers.DendriteAdmin.HandleFunc("/monitor/health", func(w http.ResponseWriter, r *http.Request) {
- if isDegraded, reasons := b.ProcessContext.IsDegraded(); isDegraded {
+ routers.DendriteAdmin.HandleFunc("/monitor/health", func(w http.ResponseWriter, r *http.Request) {
+ if isDegraded, reasons := processContext.IsDegraded(); isDegraded {
w.WriteHeader(503)
_ = json.NewEncoder(w).Encode(struct {
Warnings []string `json:"warnings"`
@@ -254,14 +116,13 @@ func (b *BaseDendrite) ConfigureAdminEndpoints() {
// SetupAndServeHTTP sets up the HTTP server to serve client & federation APIs
// and adds a prometheus handler under /_dendrite/metrics.
-func (b *BaseDendrite) SetupAndServeHTTP(
+func SetupAndServeHTTP(
+ processContext *process.ProcessContext,
+ cfg *config.Dendrite,
+ routers httputil.Routers,
externalHTTPAddr config.ServerAddress,
certFile, keyFile *string,
) {
- // Manually unlocked right before actually serving requests,
- // as we don't return from this method (defer doesn't work).
- b.startupLock.Lock()
-
externalRouter := mux.NewRouter().SkipClean(true).UseEncodedPath()
externalServ := &http.Server{
@@ -269,7 +130,7 @@ func (b *BaseDendrite) SetupAndServeHTTP(
WriteTimeout: HTTPServerTimeout,
Handler: externalRouter,
BaseContext: func(_ net.Listener) context.Context {
- return b.ProcessContext.Context()
+ return processContext.Context()
},
}
@@ -278,11 +139,11 @@ func (b *BaseDendrite) SetupAndServeHTTP(
http.Redirect(w, r, httputil.PublicStaticPath, http.StatusFound)
})
- if b.Cfg.Global.Metrics.Enabled {
- externalRouter.Handle("/metrics", httputil.WrapHandlerInBasicAuth(promhttp.Handler(), b.Cfg.Global.Metrics.BasicAuth))
+ if cfg.Global.Metrics.Enabled {
+ externalRouter.Handle("/metrics", httputil.WrapHandlerInBasicAuth(promhttp.Handler(), cfg.Global.Metrics.BasicAuth))
}
- b.ConfigureAdminEndpoints()
+ ConfigureAdminEndpoints(processContext, routers)
// Parse and execute the landing page template
tmpl := template.Must(template.ParseFS(staticContent, "static/*.gotmpl"))
@@ -293,38 +154,36 @@ func (b *BaseDendrite) SetupAndServeHTTP(
logrus.WithError(err).Fatal("failed to execute landing page template")
}
- b.Routers.Static.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
+ routers.Static.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(landingPage.Bytes())
})
var clientHandler http.Handler
- clientHandler = b.Routers.Client
- if b.Cfg.Global.Sentry.Enabled {
+ clientHandler = routers.Client
+ if cfg.Global.Sentry.Enabled {
sentryHandler := sentryhttp.New(sentryhttp.Options{
Repanic: true,
})
- clientHandler = sentryHandler.Handle(b.Routers.Client)
+ clientHandler = sentryHandler.Handle(routers.Client)
}
var federationHandler http.Handler
- federationHandler = b.Routers.Federation
- if b.Cfg.Global.Sentry.Enabled {
+ federationHandler = routers.Federation
+ if cfg.Global.Sentry.Enabled {
sentryHandler := sentryhttp.New(sentryhttp.Options{
Repanic: true,
})
- federationHandler = sentryHandler.Handle(b.Routers.Federation)
+ federationHandler = sentryHandler.Handle(routers.Federation)
}
- externalRouter.PathPrefix(httputil.DendriteAdminPathPrefix).Handler(b.Routers.DendriteAdmin)
+ externalRouter.PathPrefix(httputil.DendriteAdminPathPrefix).Handler(routers.DendriteAdmin)
externalRouter.PathPrefix(httputil.PublicClientPathPrefix).Handler(clientHandler)
- if !b.Cfg.Global.DisableFederation {
- externalRouter.PathPrefix(httputil.PublicKeyPathPrefix).Handler(b.Routers.Keys)
+ if !cfg.Global.DisableFederation {
+ externalRouter.PathPrefix(httputil.PublicKeyPathPrefix).Handler(routers.Keys)
externalRouter.PathPrefix(httputil.PublicFederationPathPrefix).Handler(federationHandler)
}
- externalRouter.PathPrefix(httputil.SynapseAdminPathPrefix).Handler(b.Routers.SynapseAdmin)
- externalRouter.PathPrefix(httputil.PublicMediaPathPrefix).Handler(b.Routers.Media)
- externalRouter.PathPrefix(httputil.PublicWellKnownPrefix).Handler(b.Routers.WellKnown)
- externalRouter.PathPrefix(httputil.PublicStaticPath).Handler(b.Routers.Static)
-
- b.startupLock.Unlock()
+ externalRouter.PathPrefix(httputil.SynapseAdminPathPrefix).Handler(routers.SynapseAdmin)
+ externalRouter.PathPrefix(httputil.PublicMediaPathPrefix).Handler(routers.Media)
+ externalRouter.PathPrefix(httputil.PublicWellKnownPrefix).Handler(routers.WellKnown)
+ externalRouter.PathPrefix(httputil.PublicStaticPath).Handler(routers.Static)
externalRouter.NotFoundHandler = httputil.NotFoundCORSHandler
externalRouter.MethodNotAllowedHandler = httputil.NotAllowedHandler
@@ -333,10 +192,10 @@ func (b *BaseDendrite) SetupAndServeHTTP(
go func() {
var externalShutdown atomic.Bool // RegisterOnShutdown can be called more than once
logrus.Infof("Starting external listener on %s", externalServ.Addr)
- b.ProcessContext.ComponentStarted()
+ processContext.ComponentStarted()
externalServ.RegisterOnShutdown(func() {
if externalShutdown.CompareAndSwap(false, true) {
- b.ProcessContext.ComponentFinished()
+ processContext.ComponentFinished()
logrus.Infof("Stopped external HTTP listener")
}
})
@@ -378,32 +237,27 @@ func (b *BaseDendrite) SetupAndServeHTTP(
}()
}
- minwinsvc.SetOnExit(b.ProcessContext.ShutdownDendrite)
- <-b.ProcessContext.WaitForShutdown()
+ minwinsvc.SetOnExit(processContext.ShutdownDendrite)
+ <-processContext.WaitForShutdown()
logrus.Infof("Stopping HTTP listeners")
_ = externalServ.Shutdown(context.Background())
logrus.Infof("Stopped HTTP listeners")
}
-func (b *BaseDendrite) WaitForShutdown() {
+func WaitForShutdown(processCtx *process.ProcessContext) {
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
select {
case <-sigs:
- case <-b.ProcessContext.WaitForShutdown():
+ case <-processCtx.WaitForShutdown():
}
signal.Reset(syscall.SIGINT, syscall.SIGTERM)
logrus.Warnf("Shutdown signal received")
- b.ProcessContext.ShutdownDendrite()
- b.ProcessContext.WaitForComponentsToFinish()
- if b.Cfg.Global.Sentry.Enabled {
- if !sentry.Flush(time.Second * 5) {
- logrus.Warnf("failed to flush all Sentry events!")
- }
- }
+ processCtx.ShutdownDendrite()
+ processCtx.WaitForComponentsToFinish()
logrus.Warnf("Dendrite is exiting now")
}
diff --git a/setup/base/base_test.go b/setup/base/base_test.go
index aa406db2..bba967b9 100644
--- a/setup/base/base_test.go
+++ b/setup/base/base_test.go
@@ -13,8 +13,10 @@ import (
"time"
"github.com/matrix-org/dendrite/internal"
+ "github.com/matrix-org/dendrite/internal/httputil"
+ basepkg "github.com/matrix-org/dendrite/setup/base"
"github.com/matrix-org/dendrite/setup/config"
- "github.com/matrix-org/dendrite/test/testrig"
+ "github.com/matrix-org/dendrite/setup/process"
"github.com/stretchr/testify/assert"
)
@@ -30,8 +32,10 @@ func TestLandingPage_Tcp(t *testing.T) {
})
assert.NoError(t, err)
- b, _, _ := testrig.Base(nil)
- defer b.Close()
+ processCtx := process.NewProcessContext()
+ routers := httputil.NewRouters()
+ cfg := config.Dendrite{}
+ cfg.Defaults(config.DefaultOpts{Generate: true, SingleDatabase: true})
// hack: create a server and close it immediately, just to get a random port assigned
s := httptest.NewServer(nil)
@@ -40,7 +44,7 @@ func TestLandingPage_Tcp(t *testing.T) {
// start base with the listener and wait for it to be started
address, err := config.HTTPAddress(s.URL)
assert.NoError(t, err)
- go b.SetupAndServeHTTP(address, nil, nil)
+ go basepkg.SetupAndServeHTTP(processCtx, &cfg, routers, address, nil, nil)
time.Sleep(time.Millisecond * 10)
// When hitting /, we should be redirected to /_matrix/static, which should contain the landing page
@@ -70,15 +74,17 @@ func TestLandingPage_UnixSocket(t *testing.T) {
})
assert.NoError(t, err)
- b, _, _ := testrig.Base(nil)
- defer b.Close()
+ processCtx := process.NewProcessContext()
+ routers := httputil.NewRouters()
+ cfg := config.Dendrite{}
+ cfg.Defaults(config.DefaultOpts{Generate: true, SingleDatabase: true})
tempDir := t.TempDir()
socket := path.Join(tempDir, "socket")
// start base with the listener and wait for it to be started
address, err := config.UnixSocketAddress(socket, "755")
assert.NoError(t, err)
- go b.SetupAndServeHTTP(address, nil, nil)
+ go basepkg.SetupAndServeHTTP(processCtx, &cfg, routers, address, nil, nil)
time.Sleep(time.Millisecond * 100)
client := &http.Client{
diff --git a/setup/base/sanity_other.go b/setup/base/sanity_other.go
index 48fe6e1f..d35c2e87 100644
--- a/setup/base/sanity_other.go
+++ b/setup/base/sanity_other.go
@@ -3,6 +3,6 @@
package base
-func platformSanityChecks() {
+func PlatformSanityChecks() {
// Nothing to do yet.
}
diff --git a/setup/base/sanity_unix.go b/setup/base/sanity_unix.go
index c630d3f1..0403df1a 100644
--- a/setup/base/sanity_unix.go
+++ b/setup/base/sanity_unix.go
@@ -9,7 +9,7 @@ import (
"github.com/sirupsen/logrus"
)
-func platformSanityChecks() {
+func PlatformSanityChecks() {
// Dendrite needs a relatively high number of file descriptors in order
// to function properly, particularly when federating with lots of servers.
// If we run out of file descriptors, we might run into problems accessing