Skip to content

Commit

Permalink
fixed map concurrent protection
Browse files Browse the repository at this point in the history
  • Loading branch information
Flowing committed Feb 9, 2020
1 parent 7a5ca97 commit ec544be
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
3 changes: 3 additions & 0 deletions server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
grpc "github.com/FlowingSPDG/gotv-plus-go/server/src/grpc"
"github.com/FlowingSPDG/gotv-plus-go/server/src/handlers"
"github.com/gin-gonic/gin"
"io/ioutil"
"log"
"net/http"
)
Expand All @@ -23,6 +24,8 @@ func init() {
log.Printf("DEBUG MODE : %v\n", *debug)
if *debug == true {
gin.SetMode(gin.ReleaseMode)
} else {
gin.DefaultWriter = ioutil.Discard
}
handlers.InitMatchEngine(*auth, uint32(*delay))
go grpc.StartGRPC(*grpcaddr)
Expand Down
23 changes: 23 additions & 0 deletions server/src/handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,7 @@ func PostBodyByIDHandler(c *gin.Context) {
c.String(http.StatusBadRequest, "fragment,tps,protocol should be float or int")
return
}
log.Printf("Received START Fragment. Register match... Token[%s] Tps[%f] Protocol[%d]\n", t, tps, protocol)
Matches.Register(&Match{
ID: id,
Token: t,
Expand All @@ -224,6 +225,13 @@ func PostBodyByIDHandler(c *gin.Context) {
// Fragment: uint32(fragment),
})
m, err := Matches.GetMatchByToken(t)
if err != nil {
log.Printf("ERR : %v\n", err)
c.String(http.StatusNotFound, "")
return
}
m.Lock()
defer m.Unlock()
m.Startframe[uint32(fragment)] = &Startframe{
At: time.Now(),
Body: reqBody,
Expand All @@ -236,6 +244,8 @@ func PostBodyByIDHandler(c *gin.Context) {
c.String(http.StatusResetContent, "")
return
}
m.Lock()
defer m.Unlock()
tick, err := strconv.Atoi(c.Query("tick"))
if err != nil {
c.String(http.StatusBadRequest, "tick should be float or int")
Expand All @@ -258,6 +268,8 @@ func PostBodyByIDHandler(c *gin.Context) {
c.String(http.StatusResetContent, "")
return
}
m.Lock()
defer m.Unlock()
endtick, err := strconv.Atoi(c.Query("endtick"))
if err != nil {
c.String(http.StatusBadRequest, "endtick should be float or int")
Expand Down Expand Up @@ -330,6 +342,13 @@ func PostBodyHandler(c *gin.Context) {
// Fragment: uint32(fragment),
})
m, err := Matches.GetMatchByToken(t)
if err != nil {
log.Printf("ERR : %v\n", err)
c.String(http.StatusNotFound, "")
return
}
m.Lock()
defer m.Unlock()
m.Startframe[uint32(fragment)] = &Startframe{
At: time.Now(),
Body: reqBody,
Expand All @@ -342,6 +361,8 @@ func PostBodyHandler(c *gin.Context) {
c.String(http.StatusResetContent, "")
return
}
m.Lock()
defer m.Unlock()
tick, err := strconv.Atoi(c.Query("tick"))
if err != nil {
c.String(http.StatusBadRequest, "tick should be float or int")
Expand All @@ -364,6 +385,8 @@ func PostBodyHandler(c *gin.Context) {
c.String(http.StatusResetContent, "")
return
}
m.Lock()
defer m.Unlock()
endtick, err := strconv.Atoi(c.Query("endtick"))
if err != nil {
c.String(http.StatusBadRequest, "endtick should be float or int")
Expand Down

0 comments on commit ec544be

Please sign in to comment.