mirror of
https://github.com/hoernschen/dendrite.git
synced 2025-07-31 05:12:46 +00:00
One NATS instance per BaseDendrite
(#2438)
* One NATS instance per `BaseDendrite` * Fix roomserver
This commit is contained in:
parent
79e2fbc663
commit
09d754cfbf
11 changed files with 49 additions and 37 deletions
|
@ -41,6 +41,7 @@ import (
|
|||
"golang.org/x/net/http2/h2c"
|
||||
|
||||
"github.com/matrix-org/dendrite/internal"
|
||||
"github.com/matrix-org/dendrite/setup/jetstream"
|
||||
"github.com/matrix-org/dendrite/setup/process"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
|
@ -77,6 +78,7 @@ type BaseDendrite struct {
|
|||
InternalAPIMux *mux.Router
|
||||
DendriteAdminMux *mux.Router
|
||||
SynapseAdminMux *mux.Router
|
||||
NATS *jetstream.NATSInstance
|
||||
UseHTTPAPIs bool
|
||||
apiHttpClient *http.Client
|
||||
Cfg *config.Dendrite
|
||||
|
@ -240,6 +242,7 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string, options ...Base
|
|||
InternalAPIMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.InternalPathPrefix).Subrouter().UseEncodedPath(),
|
||||
DendriteAdminMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.DendriteAdminPathPrefix).Subrouter().UseEncodedPath(),
|
||||
SynapseAdminMux: mux.NewRouter().SkipClean(true).PathPrefix(httputil.SynapseAdminPathPrefix).Subrouter().UseEncodedPath(),
|
||||
NATS: &jetstream.NATSInstance{},
|
||||
apiHttpClient: &apiClient,
|
||||
Database: db, // set if monolith with global connection pool only
|
||||
DatabaseWriter: writer, // set if monolith with global connection pool only
|
||||
|
|
|
@ -13,31 +13,23 @@ import (
|
|||
"github.com/sirupsen/logrus"
|
||||
|
||||
natsserver "github.com/nats-io/nats-server/v2/server"
|
||||
"github.com/nats-io/nats.go"
|
||||
natsclient "github.com/nats-io/nats.go"
|
||||
)
|
||||
|
||||
var natsServer *natsserver.Server
|
||||
var natsServerMutex sync.Mutex
|
||||
|
||||
func PrepareForTests() (*process.ProcessContext, nats.JetStreamContext, *nats.Conn) {
|
||||
cfg := &config.Dendrite{}
|
||||
cfg.Defaults(true)
|
||||
cfg.Global.JetStream.InMemory = true
|
||||
pc := process.NewProcessContext()
|
||||
js, jc := Prepare(pc, &cfg.Global.JetStream)
|
||||
return pc, js, jc
|
||||
type NATSInstance struct {
|
||||
*natsserver.Server
|
||||
sync.Mutex
|
||||
}
|
||||
|
||||
func Prepare(process *process.ProcessContext, cfg *config.JetStream) (natsclient.JetStreamContext, *natsclient.Conn) {
|
||||
func (s *NATSInstance) Prepare(process *process.ProcessContext, cfg *config.JetStream) (natsclient.JetStreamContext, *natsclient.Conn) {
|
||||
// check if we need an in-process NATS Server
|
||||
if len(cfg.Addresses) != 0 {
|
||||
return setupNATS(process, cfg, nil)
|
||||
}
|
||||
natsServerMutex.Lock()
|
||||
if natsServer == nil {
|
||||
s.Lock()
|
||||
if s.Server == nil {
|
||||
var err error
|
||||
natsServer, err = natsserver.NewServer(&natsserver.Options{
|
||||
s.Server, err = natsserver.NewServer(&natsserver.Options{
|
||||
ServerName: "monolith",
|
||||
DontListen: true,
|
||||
JetStream: true,
|
||||
|
@ -49,23 +41,23 @@ func Prepare(process *process.ProcessContext, cfg *config.JetStream) (natsclient
|
|||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
natsServer.ConfigureLogger()
|
||||
s.ConfigureLogger()
|
||||
go func() {
|
||||
process.ComponentStarted()
|
||||
natsServer.Start()
|
||||
s.Start()
|
||||
}()
|
||||
go func() {
|
||||
<-process.WaitForShutdown()
|
||||
natsServer.Shutdown()
|
||||
natsServer.WaitForShutdown()
|
||||
s.Shutdown()
|
||||
s.WaitForShutdown()
|
||||
process.ComponentFinished()
|
||||
}()
|
||||
}
|
||||
natsServerMutex.Unlock()
|
||||
if !natsServer.ReadyForConnections(time.Second * 10) {
|
||||
s.Unlock()
|
||||
if !s.ReadyForConnections(time.Second * 10) {
|
||||
logrus.Fatalln("NATS did not start in time")
|
||||
}
|
||||
nc, err := natsclient.Connect("", natsclient.InProcessServer(natsServer))
|
||||
nc, err := natsclient.Connect("", natsclient.InProcessServer(s))
|
||||
if err != nil {
|
||||
logrus.Fatalln("Failed to create NATS client")
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue