mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-07-29 12:42:46 +00:00
Remove BaseDendrite
(#3023)
Removes `BaseDendrite` to, hopefully, make testing and composing of components easier in the future.
This commit is contained in:
parent
ec6879e5ae
commit
5e85a00cb3
68 changed files with 1186 additions and 1002 deletions
|
@ -21,7 +21,7 @@ import (
|
|||
"net/http"
|
||||
"strings"
|
||||
|
||||
"github.com/matrix-org/dendrite/setup/base"
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
"nhooyr.io/websocket"
|
||||
|
||||
|
@ -90,7 +90,7 @@ func createTransport(s *pineconeSessions.Sessions) *http.Transport {
|
|||
}
|
||||
|
||||
func CreateClient(
|
||||
base *base.BaseDendrite, s *pineconeSessions.Sessions,
|
||||
s *pineconeSessions.Sessions,
|
||||
) *gomatrixserverlib.Client {
|
||||
return gomatrixserverlib.NewClient(
|
||||
gomatrixserverlib.WithTransport(createTransport(s)),
|
||||
|
@ -98,10 +98,10 @@ func CreateClient(
|
|||
}
|
||||
|
||||
func CreateFederationClient(
|
||||
base *base.BaseDendrite, s *pineconeSessions.Sessions,
|
||||
cfg *config.Dendrite, s *pineconeSessions.Sessions,
|
||||
) *gomatrixserverlib.FederationClient {
|
||||
return gomatrixserverlib.NewFederationClient(
|
||||
base.Cfg.Global.SigningIdentities(),
|
||||
cfg.Global.SigningIdentities(),
|
||||
gomatrixserverlib.WithTransport(createTransport(s)),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -27,8 +27,11 @@ import (
|
|||
"github.com/matrix-org/dendrite/cmd/dendrite-demo-pinecone/monolith"
|
||||
"github.com/matrix-org/dendrite/cmd/dendrite-demo-yggdrasil/signing"
|
||||
"github.com/matrix-org/dendrite/internal"
|
||||
"github.com/matrix-org/dendrite/internal/httputil"
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/dendrite/setup"
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
"github.com/matrix-org/dendrite/setup/process"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
"github.com/sirupsen/logrus"
|
||||
|
||||
|
@ -87,9 +90,13 @@ func main() {
|
|||
}
|
||||
}
|
||||
|
||||
processCtx := process.NewProcessContext()
|
||||
cm := sqlutil.NewConnectionManager(processCtx, cfg.Global.DatabaseOptions)
|
||||
routers := httputil.NewRouters()
|
||||
|
||||
enableMetrics := true
|
||||
enableWebsockets := true
|
||||
p2pMonolith.SetupDendrite(cfg, *instancePort, *instanceRelayingEnabled, enableMetrics, enableWebsockets)
|
||||
p2pMonolith.SetupDendrite(processCtx, cfg, cm, routers, *instancePort, *instanceRelayingEnabled, enableMetrics, enableWebsockets)
|
||||
p2pMonolith.StartMonolith()
|
||||
p2pMonolith.WaitForShutdown()
|
||||
|
||||
|
|
|
@ -23,6 +23,7 @@ import (
|
|||
"net"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
@ -39,6 +40,7 @@ import (
|
|||
"github.com/matrix-org/dendrite/federationapi/producers"
|
||||
"github.com/matrix-org/dendrite/internal/caching"
|
||||
"github.com/matrix-org/dendrite/internal/httputil"
|
||||
"github.com/matrix-org/dendrite/internal/sqlutil"
|
||||
"github.com/matrix-org/dendrite/relayapi"
|
||||
relayAPI "github.com/matrix-org/dendrite/relayapi/api"
|
||||
"github.com/matrix-org/dendrite/roomserver"
|
||||
|
@ -46,6 +48,7 @@ import (
|
|||
"github.com/matrix-org/dendrite/setup/base"
|
||||
"github.com/matrix-org/dendrite/setup/config"
|
||||
"github.com/matrix-org/dendrite/setup/jetstream"
|
||||
"github.com/matrix-org/dendrite/setup/process"
|
||||
"github.com/matrix-org/dendrite/userapi"
|
||||
userAPI "github.com/matrix-org/dendrite/userapi/api"
|
||||
"github.com/matrix-org/gomatrixserverlib"
|
||||
|
@ -61,13 +64,13 @@ import (
|
|||
const SessionProtocol = "matrix"
|
||||
|
||||
type P2PMonolith struct {
|
||||
BaseDendrite *base.BaseDendrite
|
||||
Sessions *pineconeSessions.Sessions
|
||||
Multicast *pineconeMulticast.Multicast
|
||||
ConnManager *pineconeConnections.ConnectionManager
|
||||
Router *pineconeRouter.Router
|
||||
EventChannel chan pineconeEvents.Event
|
||||
RelayRetriever relay.RelayServerRetriever
|
||||
ProcessCtx *process.ProcessContext
|
||||
|
||||
dendrite setup.Monolith
|
||||
port int
|
||||
|
@ -77,6 +80,7 @@ type P2PMonolith struct {
|
|||
listener net.Listener
|
||||
httpListenAddr string
|
||||
stopHandlingEvents chan bool
|
||||
httpServerMu sync.Mutex
|
||||
}
|
||||
|
||||
func GenerateDefaultConfig(sk ed25519.PrivateKey, storageDir string, cacheDir string, dbPrefix string) *config.Dendrite {
|
||||
|
@ -121,53 +125,52 @@ func (p *P2PMonolith) SetupPinecone(sk ed25519.PrivateKey) {
|
|||
p.ConnManager = pineconeConnections.NewConnectionManager(p.Router, nil)
|
||||
}
|
||||
|
||||
func (p *P2PMonolith) SetupDendrite(cfg *config.Dendrite, port int, enableRelaying bool, enableMetrics bool, enableWebsockets bool) {
|
||||
if enableMetrics {
|
||||
p.BaseDendrite = base.NewBaseDendrite(cfg)
|
||||
} else {
|
||||
p.BaseDendrite = base.NewBaseDendrite(cfg, base.DisableMetrics)
|
||||
}
|
||||
p.port = port
|
||||
p.BaseDendrite.ConfigureAdminEndpoints()
|
||||
func (p *P2PMonolith) SetupDendrite(
|
||||
processCtx *process.ProcessContext, cfg *config.Dendrite, cm sqlutil.Connections, routers httputil.Routers,
|
||||
port int, enableRelaying bool, enableMetrics bool, enableWebsockets bool) {
|
||||
|
||||
federation := conn.CreateFederationClient(p.BaseDendrite, p.Sessions)
|
||||
p.port = port
|
||||
base.ConfigureAdminEndpoints(processCtx, routers)
|
||||
|
||||
federation := conn.CreateFederationClient(cfg, p.Sessions)
|
||||
|
||||
serverKeyAPI := &signing.YggdrasilKeys{}
|
||||
keyRing := serverKeyAPI.KeyRing()
|
||||
|
||||
caches := caching.NewRistrettoCache(cfg.Global.Cache.EstimatedMaxSize, cfg.Global.Cache.MaxAge, enableMetrics)
|
||||
rsAPI := roomserver.NewInternalAPI(p.BaseDendrite, caches)
|
||||
natsInstance := jetstream.NATSInstance{}
|
||||
rsAPI := roomserver.NewInternalAPI(processCtx, cfg, cm, &natsInstance, caches, enableMetrics)
|
||||
fsAPI := federationapi.NewInternalAPI(
|
||||
p.BaseDendrite, federation, rsAPI, caches, keyRing, true,
|
||||
processCtx, cfg, cm, &natsInstance, federation, rsAPI, caches, keyRing, true,
|
||||
)
|
||||
|
||||
userAPI := userapi.NewInternalAPI(p.BaseDendrite, rsAPI, federation)
|
||||
userAPI := userapi.NewInternalAPI(processCtx, cfg, cm, &natsInstance, rsAPI, federation)
|
||||
|
||||
asAPI := appservice.NewInternalAPI(p.BaseDendrite, userAPI, rsAPI)
|
||||
asAPI := appservice.NewInternalAPI(processCtx, cfg, &natsInstance, userAPI, rsAPI)
|
||||
|
||||
rsAPI.SetFederationAPI(fsAPI, keyRing)
|
||||
|
||||
userProvider := users.NewPineconeUserProvider(p.Router, p.Sessions, userAPI, federation)
|
||||
roomProvider := rooms.NewPineconeRoomProvider(p.Router, p.Sessions, fsAPI, federation)
|
||||
|
||||
js, _ := p.BaseDendrite.NATS.Prepare(p.BaseDendrite.ProcessContext, &p.BaseDendrite.Cfg.Global.JetStream)
|
||||
js, _ := natsInstance.Prepare(processCtx, &cfg.Global.JetStream)
|
||||
producer := &producers.SyncAPIProducer{
|
||||
JetStream: js,
|
||||
TopicReceiptEvent: p.BaseDendrite.Cfg.Global.JetStream.Prefixed(jetstream.OutputReceiptEvent),
|
||||
TopicSendToDeviceEvent: p.BaseDendrite.Cfg.Global.JetStream.Prefixed(jetstream.OutputSendToDeviceEvent),
|
||||
TopicTypingEvent: p.BaseDendrite.Cfg.Global.JetStream.Prefixed(jetstream.OutputTypingEvent),
|
||||
TopicPresenceEvent: p.BaseDendrite.Cfg.Global.JetStream.Prefixed(jetstream.OutputPresenceEvent),
|
||||
TopicDeviceListUpdate: p.BaseDendrite.Cfg.Global.JetStream.Prefixed(jetstream.InputDeviceListUpdate),
|
||||
TopicSigningKeyUpdate: p.BaseDendrite.Cfg.Global.JetStream.Prefixed(jetstream.InputSigningKeyUpdate),
|
||||
Config: &p.BaseDendrite.Cfg.FederationAPI,
|
||||
TopicReceiptEvent: cfg.Global.JetStream.Prefixed(jetstream.OutputReceiptEvent),
|
||||
TopicSendToDeviceEvent: cfg.Global.JetStream.Prefixed(jetstream.OutputSendToDeviceEvent),
|
||||
TopicTypingEvent: cfg.Global.JetStream.Prefixed(jetstream.OutputTypingEvent),
|
||||
TopicPresenceEvent: cfg.Global.JetStream.Prefixed(jetstream.OutputPresenceEvent),
|
||||
TopicDeviceListUpdate: cfg.Global.JetStream.Prefixed(jetstream.InputDeviceListUpdate),
|
||||
TopicSigningKeyUpdate: cfg.Global.JetStream.Prefixed(jetstream.InputSigningKeyUpdate),
|
||||
Config: &cfg.FederationAPI,
|
||||
UserAPI: userAPI,
|
||||
}
|
||||
relayAPI := relayapi.NewRelayInternalAPI(p.BaseDendrite, federation, rsAPI, keyRing, producer, enableRelaying, caches)
|
||||
relayAPI := relayapi.NewRelayInternalAPI(cfg, cm, federation, rsAPI, keyRing, producer, enableRelaying, caches)
|
||||
logrus.Infof("Relaying enabled: %v", relayAPI.RelayingEnabled())
|
||||
|
||||
p.dendrite = setup.Monolith{
|
||||
Config: p.BaseDendrite.Cfg,
|
||||
Client: conn.CreateClient(p.BaseDendrite, p.Sessions),
|
||||
Config: cfg,
|
||||
Client: conn.CreateClient(p.Sessions),
|
||||
FedClient: federation,
|
||||
KeyRing: keyRing,
|
||||
|
||||
|
@ -179,9 +182,10 @@ func (p *P2PMonolith) SetupDendrite(cfg *config.Dendrite, port int, enableRelayi
|
|||
ExtPublicRoomsProvider: roomProvider,
|
||||
ExtUserDirectoryProvider: userProvider,
|
||||
}
|
||||
p.dendrite.AddAllPublicRoutes(p.BaseDendrite, caches)
|
||||
p.ProcessCtx = processCtx
|
||||
p.dendrite.AddAllPublicRoutes(processCtx, cfg, routers, cm, &natsInstance, caches, enableMetrics)
|
||||
|
||||
p.setupHttpServers(userProvider, enableWebsockets)
|
||||
p.setupHttpServers(userProvider, routers, enableWebsockets)
|
||||
}
|
||||
|
||||
func (p *P2PMonolith) GetFederationAPI() federationAPI.FederationInternalAPI {
|
||||
|
@ -203,20 +207,22 @@ func (p *P2PMonolith) StartMonolith() {
|
|||
|
||||
func (p *P2PMonolith) Stop() {
|
||||
logrus.Info("Stopping monolith")
|
||||
_ = p.BaseDendrite.Close()
|
||||
p.ProcessCtx.ShutdownDendrite()
|
||||
p.WaitForShutdown()
|
||||
logrus.Info("Stopped monolith")
|
||||
}
|
||||
|
||||
func (p *P2PMonolith) WaitForShutdown() {
|
||||
p.BaseDendrite.WaitForShutdown()
|
||||
p.ProcessCtx.WaitForShutdown()
|
||||
p.closeAllResources()
|
||||
}
|
||||
|
||||
func (p *P2PMonolith) closeAllResources() {
|
||||
logrus.Info("Closing monolith resources")
|
||||
p.httpServerMu.Lock()
|
||||
if p.httpServer != nil {
|
||||
_ = p.httpServer.Shutdown(context.Background())
|
||||
p.httpServerMu.Unlock()
|
||||
}
|
||||
|
||||
select {
|
||||
|
@ -246,12 +252,12 @@ func (p *P2PMonolith) Addr() string {
|
|||
return p.httpListenAddr
|
||||
}
|
||||
|
||||
func (p *P2PMonolith) setupHttpServers(userProvider *users.PineconeUserProvider, enableWebsockets bool) {
|
||||
func (p *P2PMonolith) setupHttpServers(userProvider *users.PineconeUserProvider, routers httputil.Routers, enableWebsockets bool) {
|
||||
p.httpMux = mux.NewRouter().SkipClean(true).UseEncodedPath()
|
||||
p.httpMux.PathPrefix(httputil.PublicClientPathPrefix).Handler(p.BaseDendrite.Routers.Client)
|
||||
p.httpMux.PathPrefix(httputil.PublicMediaPathPrefix).Handler(p.BaseDendrite.Routers.Media)
|
||||
p.httpMux.PathPrefix(httputil.DendriteAdminPathPrefix).Handler(p.BaseDendrite.Routers.DendriteAdmin)
|
||||
p.httpMux.PathPrefix(httputil.SynapseAdminPathPrefix).Handler(p.BaseDendrite.Routers.SynapseAdmin)
|
||||
p.httpMux.PathPrefix(httputil.PublicClientPathPrefix).Handler(routers.Client)
|
||||
p.httpMux.PathPrefix(httputil.PublicMediaPathPrefix).Handler(routers.Media)
|
||||
p.httpMux.PathPrefix(httputil.DendriteAdminPathPrefix).Handler(routers.DendriteAdmin)
|
||||
p.httpMux.PathPrefix(httputil.SynapseAdminPathPrefix).Handler(routers.SynapseAdmin)
|
||||
|
||||
if enableWebsockets {
|
||||
wsUpgrader := websocket.Upgrader{
|
||||
|
@ -284,8 +290,8 @@ func (p *P2PMonolith) setupHttpServers(userProvider *users.PineconeUserProvider,
|
|||
|
||||
p.pineconeMux = mux.NewRouter().SkipClean(true).UseEncodedPath()
|
||||
p.pineconeMux.PathPrefix(users.PublicURL).HandlerFunc(userProvider.FederatedUserProfiles)
|
||||
p.pineconeMux.PathPrefix(httputil.PublicFederationPathPrefix).Handler(p.BaseDendrite.Routers.Federation)
|
||||
p.pineconeMux.PathPrefix(httputil.PublicMediaPathPrefix).Handler(p.BaseDendrite.Routers.Media)
|
||||
p.pineconeMux.PathPrefix(httputil.PublicFederationPathPrefix).Handler(routers.Federation)
|
||||
p.pineconeMux.PathPrefix(httputil.PublicMediaPathPrefix).Handler(routers.Media)
|
||||
|
||||
pHTTP := p.Sessions.Protocol(SessionProtocol).HTTP()
|
||||
pHTTP.Mux().Handle(users.PublicURL, p.pineconeMux)
|
||||
|
@ -295,6 +301,7 @@ func (p *P2PMonolith) setupHttpServers(userProvider *users.PineconeUserProvider,
|
|||
|
||||
func (p *P2PMonolith) startHTTPServers() {
|
||||
go func() {
|
||||
p.httpServerMu.Lock()
|
||||
// Build both ends of a HTTP multiplex.
|
||||
p.httpServer = &http.Server{
|
||||
Addr: ":0",
|
||||
|
@ -307,7 +314,7 @@ func (p *P2PMonolith) startHTTPServers() {
|
|||
},
|
||||
Handler: p.pineconeMux,
|
||||
}
|
||||
|
||||
p.httpServerMu.Unlock()
|
||||
pubkey := p.Router.PublicKey()
|
||||
pubkeyString := hex.EncodeToString(pubkey[:])
|
||||
logrus.Info("Listening on ", pubkeyString)
|
||||
|
@ -369,7 +376,7 @@ func (p *P2PMonolith) startEventHandler() {
|
|||
ServerNames: []gomatrixserverlib.ServerName{gomatrixserverlib.ServerName(e.PeerID)},
|
||||
}
|
||||
res := &federationAPI.PerformWakeupServersResponse{}
|
||||
if err := p.dendrite.FederationAPI.PerformWakeupServers(p.BaseDendrite.Context(), req, res); err != nil {
|
||||
if err := p.dendrite.FederationAPI.PerformWakeupServers(p.ProcessCtx.Context(), req, res); err != nil {
|
||||
eLog.WithError(err).Error("Failed to wakeup destination", e.PeerID)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue