From 29a20d1da7a7f4a04d1e456d72e49424d45d5f4c Mon Sep 17 00:00:00 2001 From: Kegsay Date: Fri, 5 Jun 2020 09:28:15 +0100 Subject: [PATCH] General cleanup when making components (#1098) * Remove ParseMonolith/LoadMonolith * cleanup which components need to be made --- cmd/dendrite-appservice-server/main.go | 2 +- cmd/dendrite-client-api-server/main.go | 7 ++--- cmd/dendrite-edu-server/main.go | 2 +- cmd/dendrite-federation-api-server/main.go | 12 ++------ cmd/dendrite-federation-sender-server/main.go | 5 ++-- cmd/dendrite-key-server/main.go | 2 +- cmd/dendrite-media-api-server/main.go | 2 +- cmd/dendrite-monolith-server/main.go | 2 +- cmd/dendrite-public-rooms-api-server/main.go | 4 +-- cmd/dendrite-room-server/main.go | 2 +- cmd/dendrite-server-key-api-server/main.go | 2 +- cmd/dendrite-sync-api-server/main.go | 2 +- internal/basecomponent/flags.go | 23 ++------------- internal/config/config.go | 28 ++----------------- roomserver/inthttp/client.go | 6 +--- 15 files changed, 22 insertions(+), 79 deletions(-) diff --git a/cmd/dendrite-appservice-server/main.go b/cmd/dendrite-appservice-server/main.go index c16505fd..4fc2a173 100644 --- a/cmd/dendrite-appservice-server/main.go +++ b/cmd/dendrite-appservice-server/main.go @@ -21,7 +21,7 @@ import ( ) func main() { - cfg := basecomponent.ParseFlags() + cfg := basecomponent.ParseFlags(false) base := basecomponent.NewBaseDendrite(cfg, "AppServiceAPI", true) defer base.Close() // nolint: errcheck diff --git a/cmd/dendrite-client-api-server/main.go b/cmd/dendrite-client-api-server/main.go index 2eec84ab..8ad4231d 100644 --- a/cmd/dendrite-client-api-server/main.go +++ b/cmd/dendrite-client-api-server/main.go @@ -16,14 +16,12 @@ package main import ( "github.com/matrix-org/dendrite/clientapi" - "github.com/matrix-org/dendrite/eduserver" - "github.com/matrix-org/dendrite/eduserver/cache" "github.com/matrix-org/dendrite/internal/basecomponent" "github.com/matrix-org/dendrite/internal/transactions" ) func main() { - cfg := basecomponent.ParseFlags() + cfg := basecomponent.ParseFlags(false) base := basecomponent.NewBaseDendrite(cfg, "ClientAPI", true) defer base.Close() // nolint: errcheck @@ -38,8 +36,7 @@ func main() { asQuery := base.AppserviceHTTPClient() rsAPI := base.RoomserverHTTPClient() fsAPI := base.FederationSenderHTTPClient() - rsAPI.SetFederationSenderAPI(fsAPI) - eduInputAPI := eduserver.SetupEDUServerComponent(base, cache.New(), deviceDB) + eduInputAPI := base.EDUServerClient() clientapi.SetupClientAPIComponent( base, deviceDB, accountDB, federation, keyRing, diff --git a/cmd/dendrite-edu-server/main.go b/cmd/dendrite-edu-server/main.go index ca0460f8..ceeb1365 100644 --- a/cmd/dendrite-edu-server/main.go +++ b/cmd/dendrite-edu-server/main.go @@ -22,7 +22,7 @@ import ( ) func main() { - cfg := basecomponent.ParseFlags() + cfg := basecomponent.ParseFlags(false) base := basecomponent.NewBaseDendrite(cfg, "EDUServerAPI", true) defer func() { if err := base.Close(); err != nil { diff --git a/cmd/dendrite-federation-api-server/main.go b/cmd/dendrite-federation-api-server/main.go index 604dd4e3..0c4d8b85 100644 --- a/cmd/dendrite-federation-api-server/main.go +++ b/cmd/dendrite-federation-api-server/main.go @@ -16,31 +16,25 @@ package main import ( "github.com/matrix-org/dendrite/clientapi/producers" - "github.com/matrix-org/dendrite/eduserver" - "github.com/matrix-org/dendrite/eduserver/cache" "github.com/matrix-org/dendrite/federationapi" "github.com/matrix-org/dendrite/internal/basecomponent" ) func main() { - cfg := basecomponent.ParseFlags() + cfg := basecomponent.ParseFlags(false) base := basecomponent.NewBaseDendrite(cfg, "FederationAPI", true) defer base.Close() // nolint: errcheck accountDB := base.CreateAccountsDB() deviceDB := base.CreateDeviceDB() federation := base.CreateFederationClient() - serverKeyAPI := base.ServerKeyAPIClient() keyRing := serverKeyAPI.KeyRing() - fsAPI := base.FederationSenderHTTPClient() - rsAPI := base.RoomserverHTTPClient() asAPI := base.AppserviceHTTPClient() - rsAPI.SetFederationSenderAPI(fsAPI) - eduInputAPI := eduserver.SetupEDUServerComponent(base, cache.New(), deviceDB) - eduProducer := producers.NewEDUServerProducer(eduInputAPI) + // TODO: this isn't a producer + eduProducer := producers.NewEDUServerProducer(base.EDUServerClient()) federationapi.SetupFederationAPIComponent( base, accountDB, deviceDB, federation, keyRing, diff --git a/cmd/dendrite-federation-sender-server/main.go b/cmd/dendrite-federation-sender-server/main.go index aeaa837d..651eb257 100644 --- a/cmd/dendrite-federation-sender-server/main.go +++ b/cmd/dendrite-federation-sender-server/main.go @@ -20,7 +20,7 @@ import ( ) func main() { - cfg := basecomponent.ParseFlags() + cfg := basecomponent.ParseFlags(false) base := basecomponent.NewBaseDendrite(cfg, "FederationSender", true) defer base.Close() // nolint: errcheck @@ -30,10 +30,9 @@ func main() { keyRing := serverKeyAPI.KeyRing() rsAPI := base.RoomserverHTTPClient() - fsAPI := federationsender.SetupFederationSenderComponent( + federationsender.SetupFederationSenderComponent( base, federation, rsAPI, keyRing, ) - rsAPI.SetFederationSenderAPI(fsAPI) base.SetupAndServeHTTP(string(base.Cfg.Bind.FederationSender), string(base.Cfg.Listen.FederationSender)) diff --git a/cmd/dendrite-key-server/main.go b/cmd/dendrite-key-server/main.go index 17fee2c6..a8accbcb 100644 --- a/cmd/dendrite-key-server/main.go +++ b/cmd/dendrite-key-server/main.go @@ -20,7 +20,7 @@ import ( ) func main() { - cfg := basecomponent.ParseFlags() + cfg := basecomponent.ParseFlags(false) base := basecomponent.NewBaseDendrite(cfg, "KeyServer", true) defer base.Close() // nolint: errcheck diff --git a/cmd/dendrite-media-api-server/main.go b/cmd/dendrite-media-api-server/main.go index 1a51717a..01855938 100644 --- a/cmd/dendrite-media-api-server/main.go +++ b/cmd/dendrite-media-api-server/main.go @@ -20,7 +20,7 @@ import ( ) func main() { - cfg := basecomponent.ParseFlags() + cfg := basecomponent.ParseFlags(false) base := basecomponent.NewBaseDendrite(cfg, "MediaAPI", true) defer base.Close() // nolint: errcheck diff --git a/cmd/dendrite-monolith-server/main.go b/cmd/dendrite-monolith-server/main.go index 78919f2b..57e8f66f 100644 --- a/cmd/dendrite-monolith-server/main.go +++ b/cmd/dendrite-monolith-server/main.go @@ -49,7 +49,7 @@ var ( ) func main() { - cfg := basecomponent.ParseMonolithFlags() + cfg := basecomponent.ParseFlags(true) if *enableHTTPAPIs { // If the HTTP APIs are enabled then we need to update the Listen // statements in the configuration so that we know where to find diff --git a/cmd/dendrite-public-rooms-api-server/main.go b/cmd/dendrite-public-rooms-api-server/main.go index 18a813ba..0cad3670 100644 --- a/cmd/dendrite-public-rooms-api-server/main.go +++ b/cmd/dendrite-public-rooms-api-server/main.go @@ -22,15 +22,13 @@ import ( ) func main() { - cfg := basecomponent.ParseFlags() + cfg := basecomponent.ParseFlags(false) base := basecomponent.NewBaseDendrite(cfg, "PublicRoomsAPI", true) defer base.Close() // nolint: errcheck deviceDB := base.CreateDeviceDB() - fsAPI := base.FederationSenderHTTPClient() rsAPI := base.RoomserverHTTPClient() - rsAPI.SetFederationSenderAPI(fsAPI) publicRoomsDB, err := storage.NewPublicRoomsServerDatabase(string(base.Cfg.Database.PublicRoomsAPI), base.Cfg.DbProperties(), cfg.Matrix.ServerName) if err != nil { diff --git a/cmd/dendrite-room-server/main.go b/cmd/dendrite-room-server/main.go index 65a1e9ae..79745cde 100644 --- a/cmd/dendrite-room-server/main.go +++ b/cmd/dendrite-room-server/main.go @@ -20,7 +20,7 @@ import ( ) func main() { - cfg := basecomponent.ParseFlags() + cfg := basecomponent.ParseFlags(false) base := basecomponent.NewBaseDendrite(cfg, "RoomServerAPI", true) defer base.Close() // nolint: errcheck federation := base.CreateFederationClient() diff --git a/cmd/dendrite-server-key-api-server/main.go b/cmd/dendrite-server-key-api-server/main.go index 07573412..e206978c 100644 --- a/cmd/dendrite-server-key-api-server/main.go +++ b/cmd/dendrite-server-key-api-server/main.go @@ -20,7 +20,7 @@ import ( ) func main() { - cfg := basecomponent.ParseFlags() + cfg := basecomponent.ParseFlags(false) base := basecomponent.NewBaseDendrite(cfg, "ServerKeyAPI", true) defer base.Close() // nolint: errcheck diff --git a/cmd/dendrite-sync-api-server/main.go b/cmd/dendrite-sync-api-server/main.go index eeab115a..bb56b4ab 100644 --- a/cmd/dendrite-sync-api-server/main.go +++ b/cmd/dendrite-sync-api-server/main.go @@ -20,7 +20,7 @@ import ( ) func main() { - cfg := basecomponent.ParseFlags() + cfg := basecomponent.ParseFlags(false) base := basecomponent.NewBaseDendrite(cfg, "SyncAPI", true) defer base.Close() // nolint: errcheck diff --git a/internal/basecomponent/flags.go b/internal/basecomponent/flags.go index ecef81f6..117df079 100644 --- a/internal/basecomponent/flags.go +++ b/internal/basecomponent/flags.go @@ -25,33 +25,14 @@ import ( var configPath = flag.String("config", "dendrite.yaml", "The path to the config file. For more information, see the config file in this repository.") // ParseFlags parses the commandline flags and uses them to create a config. -// If running as a monolith use `ParseMonolithFlags` instead. -func ParseFlags() *config.Dendrite { +func ParseFlags(monolith bool) *config.Dendrite { flag.Parse() if *configPath == "" { logrus.Fatal("--config must be supplied") } - cfg, err := config.Load(*configPath) - - if err != nil { - logrus.Fatalf("Invalid config file: %s", err) - } - - return cfg -} - -// ParseMonolithFlags parses the commandline flags and uses them to create a -// config. Should only be used if running a monolith. See `ParseFlags`. -func ParseMonolithFlags() *config.Dendrite { - flag.Parse() - - if *configPath == "" { - logrus.Fatal("--config must be supplied") - } - - cfg, err := config.LoadMonolithic(*configPath) + cfg, err := config.Load(*configPath, monolith) if err != nil { logrus.Fatalf("Invalid config file: %s", err) diff --git a/internal/config/config.go b/internal/config/config.go index 59799585..858646ab 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -370,11 +370,9 @@ type LogrusHook struct { // It implements the error interface. type configErrors []string -// Load a yaml config file for a server run as multiple processes. +// Load a yaml config file for a server run as multiple processes or as a monolith. // Checks the config to ensure that it is valid. -// The checks are different if the server is run as a monolithic process instead -// of being split into multiple components -func Load(configPath string) (*Dendrite, error) { +func Load(configPath string, monolith bool) (*Dendrite, error) { configData, err := ioutil.ReadFile(configPath) if err != nil { return nil, err @@ -385,27 +383,7 @@ func Load(configPath string) (*Dendrite, error) { } // Pass the current working directory and ioutil.ReadFile so that they can // be mocked in the tests - monolithic := false - return loadConfig(basePath, configData, ioutil.ReadFile, monolithic) -} - -// LoadMonolithic loads a yaml config file for a server run as a single monolith. -// Checks the config to ensure that it is valid. -// The checks are different if the server is run as a monolithic process instead -// of being split into multiple components -func LoadMonolithic(configPath string) (*Dendrite, error) { - configData, err := ioutil.ReadFile(configPath) - if err != nil { - return nil, err - } - basePath, err := filepath.Abs(".") - if err != nil { - return nil, err - } - // Pass the current working directory and ioutil.ReadFile so that they can - // be mocked in the tests - monolithic := true - return loadConfig(basePath, configData, ioutil.ReadFile, monolithic) + return loadConfig(basePath, configData, ioutil.ReadFile, monolith) } func loadConfig( diff --git a/roomserver/inthttp/client.go b/roomserver/inthttp/client.go index f8f922fb..99db9e1e 100644 --- a/roomserver/inthttp/client.go +++ b/roomserver/inthttp/client.go @@ -45,7 +45,6 @@ const ( type httpRoomserverInternalAPI struct { roomserverURL string httpClient *http.Client - fsAPI fsInputAPI.FederationSenderInternalAPI immutableCache caching.ImmutableCache } @@ -66,11 +65,8 @@ func NewRoomserverClient( }, nil } -// SetFederationSenderInputAPI passes in a federation sender input API reference -// so that we can avoid the chicken-and-egg problem of both the roomserver input API -// and the federation sender input API being interdependent. +// SetFederationSenderInputAPI no-ops in HTTP client mode as there is no chicken/egg scenario func (h *httpRoomserverInternalAPI) SetFederationSenderAPI(fsAPI fsInputAPI.FederationSenderInternalAPI) { - h.fsAPI = fsAPI } // SetRoomAlias implements RoomserverAliasAPI