From fa41fb0eebac10a2c84d07a8e750b07c9c08c815 Mon Sep 17 00:00:00 2001 From: Neil Alexander Date: Wed, 7 Oct 2020 14:57:36 +0100 Subject: [PATCH] Return 200 on join afer 15 seconds if nothing better has happened by that point --- clientapi/routing/joinroom.go | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/clientapi/routing/joinroom.go b/clientapi/routing/joinroom.go index c10113574..8ac10ced2 100644 --- a/clientapi/routing/joinroom.go +++ b/clientapi/routing/joinroom.go @@ -16,6 +16,7 @@ package routing import ( "net/http" + "time" "github.com/matrix-org/dendrite/clientapi/auth/authtypes" "github.com/matrix-org/dendrite/clientapi/httputil" @@ -73,17 +74,34 @@ func JoinRoomByIDOrAlias( } } - // Ask the roomserver to perform the join. - rsAPI.PerformJoin(req.Context(), &joinReq, &joinRes) - if joinRes.Error != nil { - return joinRes.Error.JSONResponse() - } - - return util.JSONResponse{ + // This is the default response that we'll return, assuming nothing + // goes wrong within the timeframe. + ok := util.JSONResponse{ Code: http.StatusOK, // TODO: Put the response struct somewhere internal. JSON: struct { RoomID string `json:"room_id"` }{joinRes.RoomID}, } + + // Ask the roomserver to perform the join. + done := make(chan util.JSONResponse, 1) + go func() { + defer close(done) + rsAPI.PerformJoin(req.Context(), &joinReq, &joinRes) + if joinRes.Error != nil { + done <- joinRes.Error.JSONResponse() + } else { + done <- ok + } + }() + + // Wait either for the join to finish, or for us to hit a reasonable + // timeout, at which point we'll just return a 200 to placate clients. + select { + case <-time.After(time.Second * 15): + return ok + case result := <-done: + return result + } }