From d4281c3265518f03c3482fb28425c39cba1ea29f Mon Sep 17 00:00:00 2001 From: Mehrdad Mirsamie Date: Mon, 14 Aug 2023 13:55:26 +0330 Subject: [PATCH] retry failed ICE for 3 times --- controllers/room.go | 83 ++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 78 insertions(+), 5 deletions(-) diff --git a/controllers/room.go b/controllers/room.go index 1fb5757..d1ab8b6 100644 --- a/controllers/room.go +++ b/controllers/room.go @@ -3,13 +3,15 @@ package controllers import ( "bytes" "encoding/json" - "github.com/gin-gonic/gin" + "fmt" "io" "net/http" + "time" + + "github.com/gin-gonic/gin" "sourcecode.social/greatape/goldgorilla/models" "sourcecode.social/greatape/goldgorilla/models/dto" "sourcecode.social/greatape/goldgorilla/repositories" - "time" ) type RoomController struct { @@ -43,20 +45,91 @@ func (c *RoomController) CreatePeer(ctx *gin.Context) { c.helper.Response(ctx, struct{}{}, http.StatusNoContent) } -func (c *RoomController) AddICECandidate(ctx *gin.Context) { +func (c *RoomController) RetryAddICECandidate(ctx *gin.Context, tries int) { + time.Sleep(1 * time.Second) + tries++ + fmt.Println("----RetryAddICECandidate----") var reqModel dto.AddPeerICECandidateReqModel badReqSt := 400 - if err := ctx.ShouldBindJSON(&reqModel); c.helper.HandleIfErr(ctx, err, &badReqSt) { + err1 := ctx.ShouldBindJSON(&reqModel) + // if err1 != nil && tries < 3 { + // // var reqGlobal interface{} + + // // err2 := ctx.ShouldBindJSON(&reqGlobal) + // // fmt.Println("Err2: ", err2) + // // fmt.Println("reqGlobal: ", reqGlobal) + // c.RetryAddICECandidate(ctx, tries) + // return + + // } + if err1 != nil { + c.helper.HandleIfErr(ctx, err1, &badReqSt) + fmt.Println("Err1: ", err1) return } + // if err := ctx.ShouldBindJSON(&reqModel); c.helper.HandleIfErr(ctx, err, &badReqSt) { + // fmt.Println("Err1: ", err) + // time.Sleep(1 * time.Second) + // c.RetryAddICECandidate(ctx, tries) + // return + // } if !reqModel.Validate() { c.helper.ResponseUnprocessableEntity(ctx) + fmt.Println("Validate") return } err := c.repo.AddPeerIceCandidate(reqModel.RoomId, reqModel.ID, reqModel.ICECandidate) - if c.helper.HandleIfErr(ctx, err, nil) { + if err != nil && tries < 3 { + c.RetryAddICECandidate(ctx, tries) return } + if err != nil { + c.helper.HandleIfErr(ctx, err, &badReqSt) + fmt.Println("Err: ", err) + return + } + // if c.helper.HandleIfErr(ctx, err, nil) { + // fmt.Println("Err2: ", err) + // time.Sleep(1 * time.Second) + // c.RetryAddICECandidate(ctx, tries) + // return + // } + c.helper.Response(ctx, struct{}{}, http.StatusNoContent) +} + +func (c *RoomController) AddICECandidate(ctx *gin.Context) { + fmt.Println("----AddICECandidate----") + // bd, _ := io.ReadAll(ctx.Request.Body) + // fmt.Println("bd: ", string(bd)) + var reqModel dto.AddPeerICECandidateReqModel + // badReqSt := 400 + err1 := ctx.ShouldBindJSON(&reqModel) + if err1 != nil { + // c.RetryAddICECandidate(ctx, 0) + return + } + // if err := ctx.ShouldBindJSON(&reqModel); c.helper.HandleIfErr(ctx, err, &badReqSt) { + // fmt.Println("Err1: ", err) + // time.Sleep(1 * time.Second) + // c.AddICECandidate(ctx) + // return + // } + if !reqModel.Validate() { + c.helper.ResponseUnprocessableEntity(ctx) + fmt.Println("Validate") + return + } + err := c.repo.AddPeerIceCandidate(reqModel.RoomId, reqModel.ID, reqModel.ICECandidate) + if err != nil { + c.RetryAddICECandidate(ctx, 0) + return + } + // if c.helper.HandleIfErr(ctx, err, nil) { + // fmt.Println("Err2: ", err) + // time.Sleep(1 * time.Second) + // c.AddICECandidate(ctx) + // return + // } c.helper.Response(ctx, struct{}{}, http.StatusNoContent) }