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

Golang http server app have many Socket (CLOSE_WAIT)

应用程序可以运行几天。但是在某些时刻,应用程序有许多处于CLOSE_WAIT状态的套接字,并且无法接收新客户端。

可能是某种泛洪(示例:同步泛洪)?

网络统计-ant | grep CLOSE_WAIT |厕所

3258 19548 260640

3258 - 套接字处于 CLOSE_WAIT 状态

更新:

编写一些处理程序:

 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}
       }

CLOSE-WAIT 意味着 TCP 正在等待本地应用程序关闭套接字,已经收到来自对等方的关闭。

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

没有。这是您代码中的错误。您没有在某处关闭套接字。

The problem seems to be not in the code.

问题出在你的代码中。

Code tested several times.

但不是针对此条件,或在产生此问题的条件下。

But yesterday I did not have this problem.

所以昨天那些情况没有发生。

(Code was not changed.)

所以错误一直存在。

情境模拟:

当客户端超时有效时。服务器上的套接字不会关闭 15 秒。 就我而言,当应用程序有很多请求时,我在 CLOSE_WAIT STATE.

中有 > 10k 套接字

互斥模拟:

time.Sleep(30 * time.Second)

服务器:

package main

import "net/http"
import "time";

func main() {

   http.ListenAndServe(":81", http.HandlerFunc(func (w http.ResponseWriter, r *http.Request) {
        println("Sleep")
        time.Sleep(30 * time.Second)
        println("After sleep");
   }));

}

客户:

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main {

    public static void main(String[] args) throws IOException, InterruptedException {
        HttpURLConnection connection = (HttpURLConnection) new URL("http://link").openConnection();
        connection.setRequestMethod("GET");
        connection.setDoOutput(false);
        connection.setDoInput(true);
        connection.setReadTimeout(15000);
        connection.getInputStream().read();
        System.out.println("Close");
        connection.disconnect();
    }
}