Set default HTTP server timeout to 5 minutes now, block again when joining

This commit is contained in:
Neil Alexander 2020-04-20 16:18:39 +01:00
parent f6246588f4
commit 9736ce7bbf
3 changed files with 33 additions and 29 deletions

View file

@ -15,7 +15,6 @@
package routing package routing
import ( import (
"context"
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
@ -385,23 +384,13 @@ func (r joinRoomReq) joinRoomUsingServer(roomID string, server gomatrixserverlib
"num_state_events": len(respSendJoin.StateEvents), "num_state_events": len(respSendJoin.StateEvents),
}).Info("Room join signature and auth verification passed") }).Info("Room join signature and auth verification passed")
// By this point we've verified all of the signatures, retrieved all of the if err = r.producer.SendEventWithState(
// missing auth events and verified that everything checks out. Nothing r.req.Context(),
// *should* go wrong in the roomserver after this point, so rather than have gomatrixserverlib.RespState(respSendJoin.RespState),
// the client block on the roomserver taking in all of the new events, we event.Headered(respMakeJoin.RoomVersion),
// should be okay to do this in a goroutine and return the successful join ); err != nil {
// back to the client. util.GetLogger(r.req.Context()).WithError(err).Error("r.producer.SendEventWithState")
// TODO: Verify that this is really the case. }
go func() {
ctx := context.Background()
if err = r.producer.SendEventWithState(
ctx,
gomatrixserverlib.RespState(respSendJoin.RespState),
event.Headered(respMakeJoin.RoomVersion),
); err != nil {
util.GetLogger(ctx).WithError(err).Error("r.producer.SendEventWithState")
}
}()
return &util.JSONResponse{ return &util.JSONResponse{
Code: http.StatusOK, Code: http.StatusOK,

View file

@ -90,16 +90,26 @@ func main() {
// Expose the matrix APIs directly rather than putting them under a /api path. // Expose the matrix APIs directly rather than putting them under a /api path.
go func() { go func() {
logrus.Info("Listening on ", *httpBindAddr) serv := http.Server{
logrus.Fatal(http.ListenAndServe(*httpBindAddr, nil)) Addr: *httpBindAddr,
WriteTimeout: basecomponent.HTTPServerTimeout,
}
logrus.Info("Listening on ", serv.Addr)
logrus.Fatal(serv.ListenAndServe())
}() }()
// Handle HTTPS if certificate and key are provided // Handle HTTPS if certificate and key are provided
go func() { if *certFile != "" && *keyFile != "" {
if *certFile != "" && *keyFile != "" { go func() {
serv := http.Server{
Addr: *httpBindAddr,
WriteTimeout: basecomponent.HTTPServerTimeout,
}
logrus.Info("Listening on ", *httpsBindAddr) logrus.Info("Listening on ", *httpsBindAddr)
logrus.Fatal(http.ListenAndServeTLS(*httpsBindAddr, *certFile, *keyFile, nil)) logrus.Fatal(serv.ListenAndServeTLS(*certFile, *keyFile))
} }()
}() }
// We want to block forever to let the HTTP and HTTPS handler serve the APIs // We want to block forever to let the HTTP and HTTPS handler serve the APIs
select {} select {}

View file

@ -60,6 +60,9 @@ type BaseDendrite struct {
KafkaProducer sarama.SyncProducer KafkaProducer sarama.SyncProducer
} }
const HTTPServerTimeout = time.Minute * 5
const HTTPClientTimeout = time.Second * 30
// NewBaseDendrite creates a new instance to be used by a component. // NewBaseDendrite creates a new instance to be used by a component.
// The componentName is used for logging purposes, and should be a friendly name // The componentName is used for logging purposes, and should be a friendly name
// of the compontent running, e.g. "SyncAPI" // of the compontent running, e.g. "SyncAPI"
@ -80,14 +83,12 @@ func NewBaseDendrite(cfg *config.Dendrite, componentName string) *BaseDendrite {
kafkaConsumer, kafkaProducer = setupKafka(cfg) kafkaConsumer, kafkaProducer = setupKafka(cfg)
} }
const defaultHTTPTimeout = 30 * time.Second
return &BaseDendrite{ return &BaseDendrite{
componentName: componentName, componentName: componentName,
tracerCloser: closer, tracerCloser: closer,
Cfg: cfg, Cfg: cfg,
APIMux: mux.NewRouter().UseEncodedPath(), APIMux: mux.NewRouter().UseEncodedPath(),
httpClient: &http.Client{Timeout: defaultHTTPTimeout}, httpClient: &http.Client{Timeout: HTTPClientTimeout},
KafkaConsumer: kafkaConsumer, KafkaConsumer: kafkaConsumer,
KafkaProducer: kafkaProducer, KafkaProducer: kafkaProducer,
} }
@ -212,8 +213,12 @@ func (b *BaseDendrite) SetupAndServeHTTP(bindaddr string, listenaddr string) {
common.SetupHTTPAPI(http.DefaultServeMux, common.WrapHandlerInCORS(b.APIMux), b.Cfg) common.SetupHTTPAPI(http.DefaultServeMux, common.WrapHandlerInCORS(b.APIMux), b.Cfg)
logrus.Infof("Starting %s server on %s", b.componentName, addr) logrus.Infof("Starting %s server on %s", b.componentName, addr)
err := http.ListenAndServe(addr, nil) serv := http.Server{
Addr: addr,
WriteTimeout: HTTPServerTimeout,
}
err := serv.ListenAndServe()
if err != nil { if err != nil {
logrus.WithError(err).Fatal("failed to serve http") logrus.WithError(err).Fatal("failed to serve http")
} }