环境
我使用Indy组件TidHTTPServer在Delphi中创建了一个Web服务器。我正在使用随Indy版本10.5.8一起提供的Delphi XE2。服务器作为桌面应用程序运行,其表单显示连接及其请求的日志。它在Windows 7 Professional上运行。请求适用于Firebird数据库中的SQL数据。响应是JSON。所有流量都是HTTP。
挑战
当我用少量用户测试它时,一切都很好。现在我已经将它推广到大约400个用户,存在通信问题。服务器停止响应请求,我可以让它再次响应的唯一方法是重新启动它运行的机器,然后重新启动它。在高音量时间内更频繁地需要重新启动。
症状
使用Windows netstat我注意到,只要CLOSE_WAIT类型的TCP连接发生,服务器就会停止响应请求而我必须重新启动
测试程序
即使服务器上没有流量,我也可以模拟这种挂起。我创建了一个网页,发送多个请求,每个请求之间有一个延迟。
网页让我指定要发出的请求数,每个请求之间等待的时间,以及超时前等待的时间。即使在请求之间的一毫秒,服务器似乎也没有问题。
测试结果
如果我将每个请求的超时时间设置为一个非常小的数字,例如1毫秒,我可以让我的Delphi HTTP Server挂起。在1毫秒的超时时间,我的服务器的每次请求都会失败,正如我所料。超时时间很短,我的服务器无法快速响应。
我不明白的是,在我在客户端强制执行此超时后,即使是相对较少数量的请求(少于50个),我的Delphi Web服务器也不再响应任何请求。当我在服务器计算机上运行netstat时,有许多CLOSE_WAIT套接字连接。即使在关闭我的服务器一小时后,CLOSE_WAIT套接字连接仍然存在。
问题
到底是怎么回事?为什么我的Delphi Indy idHTTPServer在有(甚至只有一个)CLOSE_WAIT套接字连接时停止响应? CLOSE_WAIT不会消失,服务器不会再次开始响应。我必须重启。
我不做什么?
以下是显示CLOSE_WAITs的netstat命令的结果:
C:\Windows\system32>netstat -abn | findstr 62000
TCP 0.0.0.0:62000 0.0.0.0:0 LISTENING
TCP 10.1.1.13:62000 9.49.1.3:57036 TIME_WAIT
TCP 10.1.1.13:62000 9.49.1.3:57162 CLOSE_WAIT
TCP 10.1.1.13:62000 9.49.1.3:57215 CLOSE_WAIT
TCP 10.1.1.13:62000 9.49.1.3:57244 CLOSE_WAIT
TCP 10.1.1.13:62000 9.49.1.3:57263 CLOSE_WAIT
TCP 10.1.1.13:62000 9.49.1.3:57279 ESTABLISHED
TCP 10.1.1.13:62000 104.236.216.73:59051 ESTABLISHED
这是我的Web服务器的本质:
unit MyWebServer;
interface
Uses
...
Type
TfrmWebServer = class(TForm)
...
IdHTTPServer: TIdHTTPServer;
...
procedure IdHTTPServerCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
procedure IdHTTPServerDisconnect(AContext: TIdContext);
procedure btnStartClick(Sender: TObject);
...
dbFirebird : TIBDatabase;
txFireird : TIBTransaction;
...
private
function CreateSomeResponseStringData: string;
end;
implementation
procedure TfrmWebServer.btnStartClick(Sender: TObject);
begin
{set the IP's and proit to listen on}
IdHTTPServer.Bindings.Clear;
IdHTTPServer.Bindings.Add.IP := GetSetting(OPTION_TCPIP_ADDRESS);
IdHTTPServer.Bindings.Add.Port := Str2Int(GetSetting(OPTION_TCPIP_PORT));
{start the web server}
IdHTTPServer.Active := TRUE;
...
dbFirebird.Transactrion := txFirebird;
...
end;
procedure TfrmWebServer.IdHTTPServerCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
qryFirebird : TIBSql;
function CreateSomeResponseStringData: string;
begin
qryFirebird := NIL;
qryFirebird := TIBSql.Create(IdHTTPServer);
qryFirebird.Database := dbFirebird;
dbFirebird.Connected := FALSE;
dbFirebird.Connected := TRUE;
qryFirebird.Active := TRUE;
Result := {...whatever string will be returned}
end;
function CreateAnErrorResponse: string;
begin
Result := {...whatever string will be returned}
end;
begin
try
AResponseInfo.ContentText := CreateSomeResponseStringData;
{Clean up: What do I do here to make sure that the connection that was served is:
- properly closed so that I don't run out of resourses?
- anything that needs to be cleaned up is freed so no memory leaks
- TIME_WAIT, CLOSE_WAIT, any other kind of _WAITs are not accumulating?}
except;
AResponseInfo.ContentText := CreateAnErrorResponse;
end;
qryFirebird.Free;
end;
procedure TfrmWebServer.IdHTTPServerDisconnect(AContext: TIdContext);
begin
{Maybe I do the "Clean Up" here? I tried Disconnect as shown but still lots of
TIME_WAIT tcp/ip connections accumulate. even after the app is closed}
AContext.Connection.Disconnect;
end;
end.
此代码至少存在两个可能导致崩溃的主要问题:
IdHTTPServer
创建的所有线程都是全局的。断开数据库连接时,它将断开所有线程的连接。AResponseInfo.ContentText := CreateAnErrorResponse;
不在异常块中。我将如何解决这个问题:
...
procedure TfrmWebServer.btnStartClick(Sender: TObject);
begin
{set the IP's and port to listen on}
IdHTTPServer.Bindings.Clear;
IdHTTPServer.Default.Port := Str2Int(GetSetting(OPTION_TCPIP_PORT));
IdHTTPServer.Bindings.Add.IP := GetSetting(OPTION_TCPIP_ADDRESS);
{start the web server}
IdHTTPServer.Active := TRUE;
...
end;
procedure TfrmWebServer.IdHTTPServerCommandGet(AContext: TIdContext; ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var
{make these local to each thread}
qryFirebird : TIBSql;
dbFirebird : TIBDatabase;
txFirebird : TIBTransaction;
function CreateSomeResponseStringData: string;
begin
dbFirebird := TIBDatbase.Create(IdHTTPServer);
txFirebird := TIBTransaction.Create(IdHTTPServer);
qryFirebird := TIBSql.Create(IdHTTPServer);
dbFirebird.Transaction := txFirebird;
qryFirebird.Database := dbFirebird;
...Add params that do the log in to database
dbFirebird.Connected := TRUE;
qryFirebird.Active := TRUE;
Result := {...whatever string will be returned}
end;
function CreateAnErrorResponse: string;
begin
Result := {...whatever string will be returned}
end;
begin
try
try
...
AResponseInfo.ContentText := CreateSomeResponseStringData;
...
except;
try
AResponseInfo.ContentText := CreateAnErrorResponse;
except
{give up}
end;
end;
finaly
qryFirebird.Free;
dbFirebird.Free;
txFirebird.Free;
end;
end;
end.