Yggdrasil demo updates

Squashed commit of the following:

commit 6c2c48f862c1b6f8e741c57804282eceffe02487
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date:   Fri Jul 10 16:28:09 2020 +0100

    Add README.md

commit 5eeefdadf8e3881dd7a32559a92be49bd7ddaf47
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date:   Fri Jul 10 10:18:50 2020 +0100

    Fix wedge in federation sender

commit e2ebffbfba25cf82378393940a613ec32bfb909f
Merge: 0883ef88 abf26c12
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date:   Fri Jul 10 09:51:23 2020 +0100

    Merge branch 'master' into neilalexander/yggdrasil

commit 0883ef8870e340f2ae9a0c37ed939dc2ab9911f6
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date:   Fri Jul 10 09:51:06 2020 +0100

    Adjust timeouts

commit ba2d53199910f13b60cc892debe96a962e8c9acb
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date:   Thu Jul 9 16:34:40 2020 +0100

    Try to wake up from peers/sessions properly

commit 73f42eb494741ba5b0e0cef43654708e3c8eb399
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date:   Thu Jul 9 15:43:38 2020 +0100

    Use TransactionWriter to reduce database lock issues on SQLite

commit 08bfe63241a18c58c539c91b9f52edccda63a611
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date:   Thu Jul 9 12:38:02 2020 +0100

    Un-wedge federation

    Squashed commit of the following:

    commit aee933f8785e7a7998105f6090f514d18051a1bd
    Author: Neil Alexander <neilalexander@users.noreply.github.com>
    Date:   Thu Jul 9 12:22:41 2020 +0100

        Un-goroutine the goroutines

    commit 478374e5d18a3056cac6682ef9095d41352d1295
    Author: Neil Alexander <neilalexander@users.noreply.github.com>
    Date:   Thu Jul 9 12:09:31 2020 +0100

        Reduce federation sender wedges

commit 40cc62c54d9e3a863868214c48b7c18e522a4772
Author: Neil Alexander <neilalexander@users.noreply.github.com>
Date:   Thu Jul 9 10:02:52 2020 +0100

    Handle switching in/out background more reliably
This commit is contained in:
Neil Alexander 2020-07-10 16:28:18 +01:00
parent abf26c12f1
commit 08e9d996b6
No known key found for this signature in database
GPG key ID: A02A2019A2BB0944
10 changed files with 128 additions and 75 deletions

View file

@ -3,6 +3,7 @@ package gobind
import (
"context"
"crypto/tls"
"encoding/hex"
"fmt"
"net"
"net/http"
@ -25,12 +26,17 @@ import (
"github.com/matrix-org/gomatrixserverlib"
"github.com/sirupsen/logrus"
"github.com/yggdrasil-network/yggdrasil-go/src/crypto"
"go.uber.org/atomic"
)
type DendriteMonolith struct {
logger logrus.Logger
YggdrasilNode *yggconn.Node
StorageDirectory string
listener net.Listener
httpServer *http.Server
httpListening atomic.Bool
yggListening atomic.Bool
}
func (m *DendriteMonolith) BaseURL() string {
@ -58,9 +64,10 @@ func (m *DendriteMonolith) DisconnectMulticastPeers() {
}
func (m *DendriteMonolith) Start() {
logger := logrus.Logger{
m.logger = logrus.Logger{
Out: BindLogger{},
}
m.logger.SetOutput(BindLogger{})
logrus.SetOutput(BindLogger{})
var err error
@ -162,38 +169,39 @@ func (m *DendriteMonolith) Start() {
base.UseHTTPAPIs,
)
ygg.NotifySessionNew(func(boxPubKey crypto.BoxPubKey) {
serv := gomatrixserverlib.ServerName(boxPubKey.String())
ygg.NewSession = func(serverName gomatrixserverlib.ServerName) {
logrus.Infof("Found new session %q", serverName)
time.Sleep(time.Second * 3)
req := &api.PerformServersAliveRequest{
Servers: []gomatrixserverlib.ServerName{serv},
Servers: []gomatrixserverlib.ServerName{serverName},
}
res := &api.PerformServersAliveResponse{}
if err := fsAPI.PerformServersAlive(context.TODO(), req, res); err != nil {
logrus.WithError(err).Warnf("Failed to notify server %q alive due to new session", serv)
} else {
logrus.Infof("Notified server %q alive due to new session", serv)
logrus.WithError(err).Warn("Failed to notify server alive due to new session")
}
})
}
ygg.NotifyLinkNew(func(boxPubKey crypto.BoxPubKey, linkType, remote string) {
serv := gomatrixserverlib.ServerName(boxPubKey.String())
ygg.NotifyLinkNew(func(_ crypto.BoxPubKey, sigPubKey crypto.SigPubKey, linkType, remote string) {
serverName := hex.EncodeToString(sigPubKey[:])
logrus.Infof("Found new peer %q", serverName)
time.Sleep(time.Second * 3)
req := &api.PerformServersAliveRequest{
Servers: []gomatrixserverlib.ServerName{serv},
Servers: []gomatrixserverlib.ServerName{
gomatrixserverlib.ServerName(serverName),
},
}
res := &api.PerformServersAliveResponse{}
if err := fsAPI.PerformServersAlive(context.TODO(), req, res); err != nil {
logrus.WithError(err).Warnf("Failed to notify server %q alive due to new peer", serv)
} else {
logrus.Infof("Notified server %q alive due to new peer", serv)
logrus.WithError(err).Warn("Failed to notify server alive due to new session")
}
})
// Build both ends of a HTTP multiplex.
httpServer := &http.Server{
m.httpServer = &http.Server{
Addr: ":0",
TLSNextProto: map[string]func(*http.Server, *tls.Conn, http.Handler){},
ReadTimeout: 15 * time.Second,
WriteTimeout: 45 * time.Second,
ReadTimeout: 30 * time.Second,
WriteTimeout: 30 * time.Second,
IdleTimeout: 60 * time.Second,
BaseContext: func(_ net.Listener) context.Context {
return context.Background()
@ -201,19 +209,33 @@ func (m *DendriteMonolith) Start() {
Handler: base.BaseMux,
}
go func() {
logger.Info("Listening on ", ygg.DerivedServerName())
logger.Fatal(httpServer.Serve(ygg))
}()
go func() {
logger.Info("Listening on ", m.BaseURL())
logger.Fatal(httpServer.Serve(m.listener))
}()
m.Resume()
}
func (m *DendriteMonolith) Stop() {
if err := m.listener.Close(); err != nil {
logrus.Warn("Error stopping listener:", err)
func (m *DendriteMonolith) Resume() {
logrus.Info("Resuming monolith")
if listener, err := net.Listen("tcp", "localhost:65432"); err == nil {
m.listener = listener
}
if m.yggListening.CAS(false, true) {
go func() {
m.logger.Info("Listening on ", m.YggdrasilNode.DerivedServerName())
m.logger.Fatal(m.httpServer.Serve(m.YggdrasilNode))
m.yggListening.Store(false)
}()
}
if m.httpListening.CAS(false, true) {
go func() {
m.logger.Info("Listening on ", m.BaseURL())
m.logger.Fatal(m.httpServer.Serve(m.listener))
m.httpListening.Store(false)
}()
}
}
func (m *DendriteMonolith) Suspend() {
m.logger.Info("Suspending monolith")
if err := m.httpServer.Close(); err != nil {
m.logger.Warn("Error stopping HTTP server:", err)
}
m.YggdrasilNode.Stop()
}