dongwen9051 2018-05-11 10:40
浏览 101


I'm new to Go programming and I try to build API for a multiplayer game. If I make GET req to the http://localhost:8080/create_game/gameName . Server response to the request when ticker is done. I need to get response from a server instantly but I got it when ticker is over and game is timeouted and deleted.

Here is my code:

var clients = make(map[*websocket.Conn]bool)
var broadcast = make(chan Game)

//GAME_TIMEOUT in seconds
const GAME_TIMEOUT = 20

//ID generating
var genID = 0

var games = []Game{}

var msg json.RawMessage

var upgrader = websocket.Upgrader{
    ReadBufferSize:  1024,
    WriteBufferSize: 1024,
    CheckOrigin: func(r *http.Request) bool {
        return true

type GameToSend struct {
    Type string `json:"type"`
    ID   int    `json:"id"`
    Name string `json:"name"`
type Game struct {
    ID    int              `json:"id"`
    Name  string           `json:"name"`
    Timer <-chan time.Time `json:"timestamp"`

func main() {
    router := mux.NewRouter()
    router.HandleFunc("/create_game/{name}", createGame)
    router.HandleFunc("/game_events", handleConnections)
    http.ListenAndServe(":8080", router)

func handleConnections(w http.ResponseWriter, r *http.Request) {

    conn, err := upgrader.Upgrade(w, r, nil)
    if err != nil {
    defer conn.Close()
    clients[conn] = true
    for _, game := range games {
        conn.WriteJSON(GameToSend{"game.created", game.ID, game.Name})
    for {
        err := conn.ReadJSON(&msg)
        if err != nil {
        } else {

func broadcastGame(game GameToSend) {
    for conn := range clients {

func createGame(w http.ResponseWriter, r *http.Request) {
    params := mux.Vars(r)
    game := Game{genID, params["name"], time.NewTimer(GAME_TIMEOUT * time.Second).C}
    games = append(games, game)
    broadcastGame(GameToSend{"game.created", game.ID, game.Name})

func deleteGame(actionType string, i int) {
    for index, game := range games {
        if game.ID == i {
            broadcastGame(GameToSend{actionType, games[index].ID, games[index].Name})
            games = games[:index+copy(games[index:], games[index+1:])]

func checkTimeout(id int) {
    for _, game := range games {
        if game.ID == id {
            deleteGame("game.timeout", id)

any idea how to fix it?

  • 写回答

1条回答 默认 最新

  • dtkl55257 2018-05-11 11:22

    There are a number of things you could change, but one of them is in general don't run potentially long running tasks in the code that is responding to clients. Imagine you have millions of games, so many that each call to checkTimeout takes a second to iterate over all the games. Every request would have a one second delay to get a complete response, not great. It would be better to signal another goroutine should iterate over the games and clean up expired ones, that way you can return immediately to the client while the clean up happens in the background.

    Turns out I misread what you are doing, but the advice is still valid. In this case though the "long running function" actually waits until the timer expires before returning. That could be significantly longer than one second! Were you meaning to run that in a goroutine? go checkTimeout(genID)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?



    • ¥80 用PE可以更改原win11系统的环境变量吗?
    • ¥20 win10商店接入问题
    • ¥15 java 这种树形框吗
    • ¥40 找同学帮敲Python代码
    • ¥15 MYSQL 订单的商品明细重复计算问题
    • ¥15 微信实时共享位置修改
    • ¥100 TG的session协议号转成直登号号后客户端登录几分钟后自动退出设备
    • ¥50 共模反馈回路的小信号增益
    • ¥15 arduino ssd1306函数与tone函数放歌代码不兼容问题
    • ¥70 0.96版本hbase的row_key里含有双引号,无法deleteall