Sockets Golang http服务器应用程序有多个套接字(关闭\u等待)
这个应用程序可以工作几天。但在某些时刻,应用程序有许多处于关闭等待状态的套接字,无法接收新客户端 可能是某种洪水(例如:同步洪水) netstat-ant | grep CLOSE | u WAIT | wc 3258 19548 260640 3258-插座处于关闭等待状态 更新: 编写一些处理程序:Sockets Golang http服务器应用程序有多个套接字(关闭\u等待),sockets,go,flooding,Sockets,Go,Flooding,这个应用程序可以工作几天。但在某些时刻,应用程序有许多处于关闭等待状态的套接字,无法接收新客户端 可能是某种洪水(例如:同步洪水) netstat-ant | grep CLOSE | u WAIT | wc 3258 19548 260640 3258-插座处于关闭等待状态 更新: 编写一些处理程序: func GetScore(mongo *mgo.Session, redisConn redis.Conn, renderer handlers.Render) http.Handle
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正在等待本地应用程序关闭套接字,并且已经收到来自对等方的关闭消息 可能是某种洪水(例如:同步洪水) 不,这是你代码中的一个错误。你没有关闭某个插座 问题似乎不在代码中 问题出在您的代码中 代码测试了好几次 但不是在这个条件下,或者在产生这个问题的条件下 但是昨天我没有这个问题 所以昨天那些情况没有发生 (代码未更改。) 因此,错误一直存在。模拟情况: 当客户端超时工作时。服务器上的套接字不会在15秒内关闭。 在我的例子中,当应用程序有很多请求时,我有>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();
}
}
我不同意代码中的问题。我关闭我打开的所有东西。我确定。我无法关闭到http.Handler的连接,应用程序为我制作。至于其他方面,我肯定。我会努力模仿这个问题。谢谢,不管你同意与否,没有其他可能。这就是国家的意义。对等方已关闭:此端尚未关闭。在代码重构(删除互斥)后,我看不出问题。注释不用于扩展讨论;这段对话已经结束。