Forum > Third party

HTTP/2+HTTP/1.1+WebSocket server with Free Pascal

<< < (3/5) > >>

nomorelogic:
Hi I'm here again to ask for support.

I need help in the build-response process and a memory lack I get.
I refer to previous posted code like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  { TWCMyItem }   TWCMyItem = class(TWCMainClientJob)  public    procedure Execute; override;  end; ... procedure TWCMyItem.Execute;var s1, s2: string;begin    s1:=Request.QueryFields.Values['id'];    s2:=Request.QueryFields.Values['name'];    Response.Content:=TWCTestWebClient(Client).AnalizeRequest(s1, s2);     inherited Execute;end; 
In TWCMyItem.Execute procedure I create a client class to solve the request.
The client create seems to be linked to a session so (I think) this client will be released when session ends.

In my case, cause I'm writing a json rest server, I don't want to use sessions, so I need to free clients after the response is ready.

So I tried following code but this doesn't avoid the memory lack.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TWCMyItem.Execute;var s1, s2: string;begin    s1:=Request.QueryFields.Values['id'];    s2:=Request.QueryFields.Values['name'];    Response.Content:=TWCTestWebClient(Client).AnalizeRequest(s1, s2);    WebContainer.RemoveClient(Client);      inherited Execute;end; 
What is the best way to destroy the client to avoid this memory lack?

Another question: is there a gracefully way to stop server using endpoint?

Thanks in advance
nomorelogic


Edit:
removed not necessary comment in posted code

iLya2IK:

--- Quote from: nomorelogic on June 28, 2021, 12:09:34 pm ---Hi I'm here again to ask for support.

--- End quote ---
It is wonderful. Each of your questions develops a project. I created it for my narrow needs, but I would like to make it more versatile


--- Quote from: nomorelogic on June 28, 2021, 12:09:34 pm ---The client create seems to be linked to a session so (I think) this client will be released when session ends.

--- End quote ---
Yes It is. The session created every time you connect unknown client with empty or unrecognized "cid" cookie


--- Quote from: nomorelogic on June 28, 2021, 12:09:34 pm ---In my case, cause I'm writing a json rest server, I don't want to use sessions, so I need to free clients after the response is ready.
...
What is the best way to destroy the client to avoid this memory lack?

--- End quote ---
This server works according to the principles of targeting a web client and has a built-in mechanism for creating and maintaining sessions. But you can work around it simply by defining an inheritor for the TWCPreAnalizeClientJob class.


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure TWCPACNoSessionJob.Execute;var ASynThread : TWCMainClientJob;    aClient : TWebClient;    aConsumeResult : TWCConsumeResult;begin  try     if not (Assigned(FConn) and TWCHttpServer(FConn.Server).ServerActive) then       Exit;     aConsumeResult := FConn.ConsumeSocketData;     if aConsumeResult = wccrOK then begin       aClient := WebContainer.AddClient(Request, 'empty');       if not assigned(aClient) then begin         Application.SendError(Response, 405);         Exit;       end else begin         aClient.Initialize;       end;             FConn.SetSessionParams(aClient, nil);       //       if assigned(aClient) then begin         ASynThread := GenerateClientJob;         if Assigned(ASynThread) then         begin           FConn := nil; //now fconn is part of ASynThread job           Application.ESServer.AddToMainPool(ASynThread);         end;       end;     end;  except    on E: Exception do ; // catch errors. jail them in thread  end;end;And then set the ServerAnalizeJobClass property while initializing the application.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---Application.ESServer.ServerAnalizeJobClass := TWCPACNoSessionJob;I have no way to test it yet, so please use this code with caution for now. In the next update, I will include this code in the release.

--- Quote from: nomorelogic on June 28, 2021, 12:09:34 pm ---Another question: is there a gracefully way to stop server using endpoint?

--- End quote ---
Yes, sure. You can use the server.cfg file and the "Maintaining"."Shutdown" property.
You can set the property to true value to stop the server while execution.

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---"Maintaining": {        "Shutdown":true}

nomorelogic:

--- Quote from: iLya2IK on June 29, 2021, 08:19:19 am ---I have no way to test it yet, so please use this code with caution for now. In the next update, I will include this code in the release.

--- End quote ---

really many thanks :)
These days I’ll do some tests like you suggested

iLya2IK:

--- Quote from: nomorelogic on June 29, 2021, 10:28:38 am ---really many thanks :)
These days I’ll do some tests like you suggested

--- End quote ---

I invite you to try the last commit. I have implemented all the suggestions written here earlier in the demo "examples/restjson".

The changes mainly affected two lines in file wcrestjsondemo.lpr.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---WebContainer.Verbose := false; // reduces disk load as it stops writing //new client information to the database Application.WebClientClass:= WCTestClient.TWCTestWebClient; //this line was deleted. //The parent class TWCWebClient is used. //There is no need to redefine it as clients are now faceless. //All business logic can be moved to file wcrestjsonjobs.pas.  
And now the TWCPreThread class is the inheritor of TWCPreAnalizeClientNoSessionJob (the new class in the wcApplication unit)

iLya2IK:
REST JSON simple server

Short description
The server operates in the REST architecture mode. The server can accept POST requests and respond with JSON objects. The server simulates a simple online store capable of creating customers, products, and adding products to a customer cart.

Server testing

--- Code: Text  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---> curl -k -H "Content-Type: application/json" -X POST "https://localhost:8080/addClient.json?id=1&name=vasya" {"result":"OK"} > curl -k -d '{"shrtName":"item1", "fullName": "item num 1", "descr":"description for item1", "cost":500}' -H "Content-Type: application/json" -X POST "https://localhost:8080/addItem.json" {"result":"OK"} > curl -k -d '{"shrtName":"item2", "fullName": "item num 2", "descr":"description for item2", "cost":1500}' -H "Content-Type: application/json" -X POST "https://localhost:8080/addItem.json" {"result":"OK"} > curl -k -H "Content-Type: application/json" -X POST "https://localhost:8080/getItem.json?iId=1" {"shrtName":"item1","fullName":"item num 1","descr":"description for item1","cost":500,"result":"OK"} > curl -k -H "Content-Type: application/json" -X POST "https://localhost:8080/getClientByName.json?name=vasya" {"cId":1,"result":"OK"} > curl -k -H "Content-Type: application/json" -X POST "https://localhost:8080/addToBasket.json?cId=1&iId=2&cost=500" {"result":"OK"} > curl -k -H "Content-Type: application/json" -X POST "https://localhost:8080/addToBasket.json?cId=1&iId=1&cost=500" {"result":"OK"} > curl -k -H "Content-Type: application/json" -X POST "https://localhost:8080/addToBasket.json?cId=1&iId=1&cost=500" {"result":"OK"} > curl -k -H "Content-Type: application/json" -X POST "https://localhost:8080/getBasket.json?cId=1" [{"iId":2,"cost":500},{"iId":1,"cost":500}]  

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version