mirror of
https://github.com/element-hq/dendrite.git
synced 2025-09-13 12:52:24 +03:00
unix socket support (#2974)
### Pull Request Checklist <!-- Please read https://matrix-org.github.io/dendrite/development/contributing before submitting your pull request --> * [x] I have added Go unit tests or [Complement integration tests](https://github.com/matrix-org/complement) for this PR _or_ I have justified why this PR doesn't need tests * [x] Pull request includes a [sign off below using a legally identifiable name](https://matrix-org.github.io/dendrite/development/contributing#sign-off) _or_ I have already signed off privately Signed-off-by: `Boris Rybalkin <ribalkin@gmail.com>` I need this for Syncloud project (https://github.com/syncloud/platform) where I run multiple apps behind an nginx on the same RPi like device so unix socket is very convenient to not have port conflicts between apps. Also someone opened this Issue: https://github.com/matrix-org/dendrite/issues/2924 --------- Co-authored-by: kegsay <kegan@matrix.org> Co-authored-by: Till <2353100+S7evinK@users.noreply.github.com>
This commit is contained in:
parent
6c20f8f742
commit
6b1c9eafa9
6 changed files with 171 additions and 29 deletions
|
@ -20,9 +20,11 @@ import (
|
|||
"database/sql"
|
||||
"embed"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"html/template"
|
||||
"io"
|
||||
"io/fs"
|
||||
"net"
|
||||
"net/http"
|
||||
_ "net/http/pprof"
|
||||
|
@ -85,8 +87,6 @@ type BaseDendrite struct {
|
|||
startupLock sync.Mutex
|
||||
}
|
||||
|
||||
const NoListener = ""
|
||||
|
||||
const HTTPServerTimeout = time.Minute * 5
|
||||
|
||||
type BaseDendriteOptions int
|
||||
|
@ -345,18 +345,17 @@ func (b *BaseDendrite) ConfigureAdminEndpoints() {
|
|||
// SetupAndServeHTTP sets up the HTTP server to serve client & federation APIs
|
||||
// and adds a prometheus handler under /_dendrite/metrics.
|
||||
func (b *BaseDendrite) SetupAndServeHTTP(
|
||||
externalHTTPAddr config.HTTPAddress,
|
||||
externalHTTPAddr config.ServerAddress,
|
||||
certFile, keyFile *string,
|
||||
) {
|
||||
// Manually unlocked right before actually serving requests,
|
||||
// as we don't return from this method (defer doesn't work).
|
||||
b.startupLock.Lock()
|
||||
externalAddr, _ := externalHTTPAddr.Address()
|
||||
|
||||
externalRouter := mux.NewRouter().SkipClean(true).UseEncodedPath()
|
||||
|
||||
externalServ := &http.Server{
|
||||
Addr: string(externalAddr),
|
||||
Addr: externalHTTPAddr.Address,
|
||||
WriteTimeout: HTTPServerTimeout,
|
||||
Handler: externalRouter,
|
||||
BaseContext: func(_ net.Listener) context.Context {
|
||||
|
@ -419,7 +418,7 @@ func (b *BaseDendrite) SetupAndServeHTTP(
|
|||
|
||||
b.startupLock.Unlock()
|
||||
|
||||
if externalAddr != NoListener {
|
||||
if externalHTTPAddr.Enabled() {
|
||||
go func() {
|
||||
var externalShutdown atomic.Bool // RegisterOnShutdown can be called more than once
|
||||
logrus.Infof("Starting external listener on %s", externalServ.Addr)
|
||||
|
@ -437,9 +436,30 @@ func (b *BaseDendrite) SetupAndServeHTTP(
|
|||
}
|
||||
}
|
||||
} else {
|
||||
if err := externalServ.ListenAndServe(); err != nil {
|
||||
if err != http.ErrServerClosed {
|
||||
logrus.WithError(err).Fatal("failed to serve HTTP")
|
||||
if externalHTTPAddr.IsUnixSocket() {
|
||||
err := os.Remove(externalHTTPAddr.Address)
|
||||
if err != nil && !errors.Is(err, fs.ErrNotExist) {
|
||||
logrus.WithError(err).Fatal("failed to remove existing unix socket")
|
||||
}
|
||||
listener, err := net.Listen(externalHTTPAddr.Network(), externalHTTPAddr.Address)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Fatal("failed to serve unix socket")
|
||||
}
|
||||
err = os.Chmod(externalHTTPAddr.Address, externalHTTPAddr.UnixSocketPermission)
|
||||
if err != nil {
|
||||
logrus.WithError(err).Fatal("failed to set unix socket permissions")
|
||||
}
|
||||
if err := externalServ.Serve(listener); err != nil {
|
||||
if err != http.ErrServerClosed {
|
||||
logrus.WithError(err).Fatal("failed to serve unix socket")
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
if err := externalServ.ListenAndServe(); err != nil {
|
||||
if err != http.ErrServerClosed {
|
||||
logrus.WithError(err).Fatal("failed to serve HTTP")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue