mirror of
https://github.com/matrix-org/dendrite
synced 2024-12-25 04:53:48 +01:00
9de7efa0b0
* Remove dependency on saramajetstream & sarama Signed-off-by: Till Faelligen <tfaelligen@gmail.com> * Remove internal.ContinualConsumer from federationapi * Remove internal.ContinualConsumer from syncapi * Remove internal.ContinualConsumer from keyserver * Move to new Prepare function * Remove saramajetstream & sarama dependency * Delete unneeded file * Remove duplicate import * Log error instead of silently irgnoring it * Move `OffsetNewest` and `OffsetOldest` into keyserver types, change them to be more sane values * Fix comments Co-authored-by: Neil Alexander <neilalexander@users.noreply.github.com>
94 lines
2.4 KiB
Go
94 lines
2.4 KiB
Go
package jetstream
|
|
|
|
import (
|
|
"strings"
|
|
"sync"
|
|
"time"
|
|
|
|
"github.com/matrix-org/dendrite/setup/config"
|
|
"github.com/sirupsen/logrus"
|
|
|
|
natsserver "github.com/nats-io/nats-server/v2/server"
|
|
natsclient "github.com/nats-io/nats.go"
|
|
)
|
|
|
|
var natsServer *natsserver.Server
|
|
var natsServerMutex sync.Mutex
|
|
|
|
func Prepare(cfg *config.JetStream) natsclient.JetStreamContext {
|
|
// check if we need an in-process NATS Server
|
|
if len(cfg.Addresses) != 0 {
|
|
return setupNATS(cfg, nil)
|
|
}
|
|
natsServerMutex.Lock()
|
|
if natsServer == nil {
|
|
var err error
|
|
natsServer, err = natsserver.NewServer(&natsserver.Options{
|
|
ServerName: "monolith",
|
|
DontListen: true,
|
|
JetStream: true,
|
|
StoreDir: string(cfg.StoragePath),
|
|
NoSystemAccount: true,
|
|
AllowNewAccounts: false,
|
|
MaxPayload: 16 * 1024 * 1024,
|
|
})
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
natsServer.ConfigureLogger()
|
|
go natsServer.Start()
|
|
}
|
|
natsServerMutex.Unlock()
|
|
if !natsServer.ReadyForConnections(time.Second * 10) {
|
|
logrus.Fatalln("NATS did not start in time")
|
|
}
|
|
nc, err := natsclient.Connect("", natsclient.InProcessServer(natsServer))
|
|
if err != nil {
|
|
logrus.Fatalln("Failed to create NATS client")
|
|
}
|
|
return setupNATS(cfg, nc)
|
|
}
|
|
|
|
func setupNATS(cfg *config.JetStream, nc *natsclient.Conn) natsclient.JetStreamContext {
|
|
if nc == nil {
|
|
var err error
|
|
nc, err = natsclient.Connect(strings.Join(cfg.Addresses, ","))
|
|
if err != nil {
|
|
logrus.WithError(err).Panic("Unable to connect to NATS")
|
|
return nil
|
|
}
|
|
}
|
|
|
|
s, err := nc.JetStream()
|
|
if err != nil {
|
|
logrus.WithError(err).Panic("Unable to get JetStream context")
|
|
return nil
|
|
}
|
|
|
|
for _, stream := range streams { // streams are defined in streams.go
|
|
name := cfg.TopicFor(stream.Name)
|
|
info, err := s.StreamInfo(name)
|
|
if err != nil && err != natsclient.ErrStreamNotFound {
|
|
logrus.WithError(err).Fatal("Unable to get stream info")
|
|
}
|
|
if info == nil {
|
|
stream.Subjects = []string{name}
|
|
|
|
// If we're trying to keep everything in memory (e.g. unit tests)
|
|
// then overwrite the storage policy.
|
|
if cfg.InMemory {
|
|
stream.Storage = natsclient.MemoryStorage
|
|
}
|
|
|
|
// Namespace the streams without modifying the original streams
|
|
// array, otherwise we end up with namespaces on namespaces.
|
|
namespaced := *stream
|
|
namespaced.Name = name
|
|
if _, err = s.AddStream(&namespaced); err != nil {
|
|
logrus.WithError(err).WithField("stream", name).Fatal("Unable to add stream")
|
|
}
|
|
}
|
|
}
|
|
|
|
return s
|
|
}
|