2020-08-10 14:18:37 +00:00
|
|
|
package config
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
|
|
// Defined Kafka topics.
|
|
|
|
const (
|
|
|
|
TopicOutputTypingEvent = "OutputTypingEvent"
|
|
|
|
TopicOutputSendToDeviceEvent = "OutputSendToDeviceEvent"
|
|
|
|
TopicOutputKeyChangeEvent = "OutputKeyChangeEvent"
|
|
|
|
TopicOutputRoomEvent = "OutputRoomEvent"
|
|
|
|
TopicOutputClientData = "OutputClientData"
|
2020-11-09 18:46:11 +00:00
|
|
|
TopicOutputReceiptEvent = "OutputReceiptEvent"
|
2020-08-10 14:18:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Kafka struct {
|
|
|
|
// A list of kafka addresses to connect to.
|
|
|
|
Addresses []string `yaml:"addresses"`
|
2020-08-11 12:21:26 +00:00
|
|
|
// The prefix to use for Kafka topic names for this homeserver - really only
|
|
|
|
// useful if running more than one Dendrite on the same Kafka deployment.
|
|
|
|
TopicPrefix string `yaml:"topic_prefix"`
|
2020-08-10 14:18:37 +00:00
|
|
|
// Whether to use naffka instead of kafka.
|
|
|
|
// Naffka can only be used when running dendrite as a single monolithic server.
|
|
|
|
// Kafka can be used both with a monolithic server and when running the
|
|
|
|
// components as separate servers.
|
|
|
|
UseNaffka bool `yaml:"use_naffka"`
|
|
|
|
// The Naffka database is used internally by the naffka library, if used.
|
|
|
|
Database DatabaseOptions `yaml:"naffka_database"`
|
2020-10-27 14:11:37 +00:00
|
|
|
// The max size a Kafka message passed between consumer/producer can have
|
|
|
|
// Equals roughly max.message.bytes / fetch.message.max.bytes in Kafka
|
|
|
|
MaxMessageBytes *int `yaml:"max_message_bytes"`
|
2020-08-10 14:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (k *Kafka) TopicFor(name string) string {
|
|
|
|
return fmt.Sprintf("%s%s", k.TopicPrefix, name)
|
|
|
|
}
|
|
|
|
|
2021-11-24 11:57:39 +00:00
|
|
|
func (c *Kafka) Defaults(generate bool) {
|
2020-08-10 14:18:37 +00:00
|
|
|
c.UseNaffka = true
|
2021-03-08 13:18:29 +00:00
|
|
|
c.Database.Defaults(10)
|
2021-11-24 11:57:39 +00:00
|
|
|
if generate {
|
|
|
|
c.Addresses = []string{"localhost:2181"}
|
|
|
|
c.Database.ConnectionString = DataSource("file:naffka.db")
|
|
|
|
}
|
2020-08-10 14:18:37 +00:00
|
|
|
c.TopicPrefix = "Dendrite"
|
2020-10-27 14:11:37 +00:00
|
|
|
|
|
|
|
maxBytes := 1024 * 1024 * 8 // about 8MB
|
|
|
|
c.MaxMessageBytes = &maxBytes
|
2020-08-10 14:18:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Kafka) Verify(configErrs *ConfigErrors, isMonolith bool) {
|
|
|
|
if c.UseNaffka {
|
|
|
|
if !isMonolith {
|
|
|
|
configErrs.Add("naffka can only be used in a monolithic server")
|
|
|
|
}
|
|
|
|
checkNotEmpty(configErrs, "global.kafka.database.connection_string", string(c.Database.ConnectionString))
|
|
|
|
} else {
|
|
|
|
// If we aren't using naffka then we need to have at least one kafka
|
|
|
|
// server to talk to.
|
|
|
|
checkNotZero(configErrs, "global.kafka.addresses", int64(len(c.Addresses)))
|
|
|
|
}
|
|
|
|
checkNotEmpty(configErrs, "global.kafka.topic_prefix", string(c.TopicPrefix))
|
2020-10-27 14:11:37 +00:00
|
|
|
checkPositive(configErrs, "global.kafka.max_message_bytes", int64(*c.MaxMessageBytes))
|
2020-08-10 14:18:37 +00:00
|
|
|
}
|