普通网友 2016-12-04 13:47
浏览 175
已采纳

Golang HTTP服务器应用程序有许多套接字(CLOSE_WAIT)

App can works few days. But in some moment, app has many socket with CLOSE_WAIT state, and cannot receive new client.

Maybe it is some kind of flooding(Example: Sync-flood)?

netstat -ant | grep CLOSE_WAIT | wc

3258 19548 260640

3258 - socket in CLOSE_WAIT state

Update:

Code some handler:

 func GetScore(mongo *mgo.Session, redisConn redis.Conn, renderer handlers.Render) http.Handler {

         mutex := sync.Mutex{}

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

           id := bson.ObjectIdHex(r.FormValue("id"))
           banner := models.Banner{}
           err := mongo.DB("db").C("collection").FindId(id).One(&banner)
           if err != nil {
             log.Panicf("Banner selecting error: %s", err);
           }
           mutex.Lock();
           sports, _ := util.GetSports(redisConn)
           mutex.Unlock();
           sport, _ := sports.FindSport(banner.SportId)
           comp, err := sport.FindCompetition(banner.CompetitionId)
           if err != nil {
               comp, _ = sport.FindCompetition(0);
               log.Println("Competition not found");
           }
           game, err := comp.FindGame(banner.GameId)

           if err != nil {
           game, _ = comp.FindGame(0)
           }
           mutex.Lock();
           scores := util.GetScore(redisConn, game.ID)
           mutex.Unlock();
           game.Score1 = scores[0]
           game.Score2 = scores[1]
           w.Header().Set("Content-Type", "application/json;application/json;charset=utf-8")
           renderer.RenderJson(w, 200, &game)
       }



       func GetScore(redisConn redis.Conn, gameId int) ([]float32) {

         redisKey :=  fmt.Sprintf("game-%d", gameId);
         bBody, err := redis.Bytes(redisConn.Do("GET", redisKey))

         if err != nil || len(bBody) == 0 {
           response, err := http.DefaultClient.Get(fmt.Sprintf("%s%d", GameApi, gameId))

           if err != nil {
             log.Panicf("GetScore error: %s", err)
           }

           bBody, _ = ioutil.ReadAll(response.Body);
           redisConn.Send("SET", redisKey, bBody)
           redisConn.Send("EXPIRE", redisKey, 60 * 5)
           redisConn.Flush()
         }
         events := GameJson{};
         err = json.Unmarshal(bBody, &events)
         if err != nil {
           log.Panicf("GetScore json error: %s", err)
         }

         var event []struct {
           Name string `json:"name"`
           Price float32 `json:"price"`
}
         if len(events.AllEvents.P1XP2) != 0 {
           event = events.AllEvents.P1XP2[0].Events
         } else {
           event = events.AllEvents.Other[0].Events
         }

         return []float32{event[0].Price, event[1].Price}
       }
  • 写回答

2条回答 默认 最新

  • dpmwy80068 2016-12-04 21:45
    关注

    CLOSE-WAIT means that TCP is waiting for the local application to close the socket, having already received a close from the peer.

    Maybe it is some kind of flooding(Example: Sync-flood)?

    No. It is a bug in your code. You aren't closing a socket somewhere.

    The problem seems to be not in the code.

    The problem is in your code.

    Code tested several times.

    But not for this condition, or under the conditions that produce this problem.

    But yesterday I did not have this problem.

    So yesterday those condtiions did not occur.

    (Code was not changed.)

    So the bug has always been there.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。