Overview
Examples
Screenshots
Comparisons
Applications
Download
Documentation
Tutorials
Bazaar
Status & Roadmap
FAQ
Authors & License
Forums
Funding Ultimate++
Search on this site
Search in forums












SourceForge.net Logo
Home » Developing U++ » U++ Developers corner » SCGI Class
SCGI Class [message #28120] Tue, 17 August 2010 16:35 Go to next message
jeremy_c is currently offline  jeremy_c
Messages: 175
Registered: August 2007
Location: Ohio, USA
Experienced Member
I have created a SCGI class. SCGI is a more simple FastCGI interface. It allows the application to persist between connections, thus respond very quick unlike a CGI program that has to be loaded, initialized, executed and closed for each request.

This really comes in handy when there are expensive start up costs such as a database connection.

An example application:


#include <SCGI/SCGI.h>

class HelloWebApp : public ScgiServer {
public:
  HelloWebApp() : ScgiServer(8787) 
  {
    WhenRequest = HandleRequest;
  }

  void HandleRequest()
  {
    clientSocket.Write("Content-Type: text/plain\r\n\r\n");
    clientSocket.Write(Format("Hello, %s!", query["NAME"]));
  }
};

CONSOLE_APP_MAIN
{
  HelloWebApp app;
  app.Run();
}


There are other callbacks such as WhenAccepted, WhenClosed. "query" is public and is an instance of HttpQuery that is automatically populated. "map" is a VectorMap<String,String> that is also automatically populated with the server variables that are passed such as REQUEST_URI, SERVER_NAME, etc...

On my NetBook (1.6ghz Atom) the above SCGI app runs ~1200 requests a second. A static hello.txt file is ~1900 requests a second.

I still have a few loose ends to wrap up (post form data, some more general testing) but wondering if anyone else would find this useful and how to share it?

Oh... most web servers (Apache included) have a "mod_scgi" to interface with this type of application. The applications need not reside on the same computer as the web server, thus they can be distributed and offer load balancing. Advanced servers like Apache can do load balancing themselves internally knowing which SCGI app is in use and which one is not.

For anyone who wants to know more about the SCGI protocol: http://python.ca/scgi/protocol.txt

Jeremy

[Updated on: Tue, 17 August 2010 16:37]

Report message to a moderator

Re: SCGI Class [message #28144 is a reply to message #28120] Wed, 18 August 2010 17:50 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 693
Registered: December 2005
Location: Budapest, Hungary
Contributor
This would be a useful stuff Smile
I think, you can upload it here.
Re: SCGI Class [message #28159 is a reply to message #28120] Thu, 19 August 2010 15:32 Go to previous messageGo to next message
jeremy_c is currently offline  jeremy_c
Messages: 175
Registered: August 2007
Location: Ohio, USA
Experienced Member
Ok, I am attaching the Scgi package as well as a ScgiHello example. It now handles parsing query string, post data and server variables internally. There are no docs yet but it's use is pretty simple to figure out. The ScgiHello example helps w/this as well.

Once it gets a review or two, I'll make any corrections and start documenting. The ScgiHello example includes a sample LigHTTPD configuration file that should run out of the box.

It does use some signal handling which I have not tested on Windows.

I would like to upload this and begin to maintain this in Bazaar. How does one go about doing that? (Oh, I have the MultipartForm class that I added to Web/util.h/cpp that I think would be helpful for others).

Jeremy

[Updated on: Thu, 19 August 2010 15:34]

Report message to a moderator

Re: SCGI Class [message #29053 is a reply to message #28159] Sun, 03 October 2010 20:19 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
jeremy_c wrote on Thu, 19 August 2010 09:32

Ok, I am attaching the Scgi package as well as a ScgiHello example. It now handles parsing query string, post data and server variables internally. There are no docs yet but it's use is pretty simple to figure out. The ScgiHello example helps w/this as well.

Once it gets a review or two, I'll make any corrections and start documenting. The ScgiHello example includes a sample LigHTTPD configuration file that should run out of the box.

It does use some signal handling which I have not tested on Windows.

I would like to upload this and begin to maintain this in Bazaar. How does one go about doing that? (Oh, I have the MultipartForm class that I added to Web/util.h/cpp that I think would be helpful for others).

Jeremy



Perfect! (sorry for begin late here...)

In fact, I think this can go directly to uppsrc, bypassing bazaar this time. Just please test if this works in Windows too...

ScgiServer now in uppsrc.

I am sending you access info to svn server now...

Mirek
Re: SCGI Class [message #29078 is a reply to message #29053] Mon, 04 October 2010 18:52 Go to previous messageGo to next message
jeremy_c is currently offline  jeremy_c
Messages: 175
Registered: August 2007
Location: Ohio, USA
Experienced Member
luzr wrote on Sun, 03 October 2010 14:19


Just please test if this works in Windows too...



Tested on Windows (all works as designed) using nginx (single executable web server that will work on Windows as well as *nix systems). I added a config file for nginx to the ScgiHello reference application so others can play with it on Windows as well.

Jeremy
Re: SCGI Class [message #29079 is a reply to message #29078] Mon, 04 October 2010 19:12 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Looking at it, I have strong impression that in this case it would be more logical to use virtual methods instead of Callbacks. I guess even HelloScgi would be quite simpler that way..

Also, as this seems to be quite short piece of code, I consider moving it to Web package (you would retain access rights there too).

What do you think about this?
Re: SCGI Class [message #29081 is a reply to message #29079] Mon, 04 October 2010 19:19 Go to previous messageGo to next message
jeremy_c is currently offline  jeremy_c
Messages: 175
Registered: August 2007
Location: Ohio, USA
Experienced Member
luzr wrote on Mon, 04 October 2010 13:12

Looking at it, I have strong impression that in this case it would be more logical to use virtual methods instead of Callbacks. I guess even HelloScgi would be quite simpler that way..

Also, as this seems to be quite short piece of code, I consider moving it to Web package (you would retain access rights there too).

What do you think about this?


I was thinking the same about virtual methods vs. callbacks.

In regards to moving to the web package, that depends on how you would like the overall structure. Say someone else comes along and makes FastCGI and then WSCGI... How would you envision those being added? I'm not saying someone is, but just something to think about.

It does seem odd out there in uppsrc/ all by it's lonesome.

Jeremy
Re: SCGI Class [message #29088 is a reply to message #29081] Mon, 04 October 2010 20:28 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
jeremy_c wrote on Mon, 04 October 2010 13:19

luzr wrote on Mon, 04 October 2010 13:12

Looking at it, I have strong impression that in this case it would be more logical to use virtual methods instead of Callbacks. I guess even HelloScgi would be quite simpler that way..

Also, as this seems to be quite short piece of code, I consider moving it to Web package (you would retain access rights there too).

What do you think about this?


I was thinking the same about virtual methods vs. callbacks.



Well, perhaps you could test the svn access rights and provide this change (and to reference example too)? Smile

Quote:


In regards to moving to the web package, that depends on how you would like the overall structure. Say someone else comes along and makes FastCGI and then WSCGI... How would you envision those being added? I'm not saying someone is, but just something to think about.



I do not think this is really a problem - we can live with complicated stuff in separate package and simple stuff as part of something...
Re: SCGI Class [message #29095 is a reply to message #29088] Mon, 04 October 2010 23:54 Go to previous messageGo to next message
jeremy_c is currently offline  jeremy_c
Messages: 175
Registered: August 2007
Location: Ohio, USA
Experienced Member
luzr wrote on Mon, 04 October 2010 14:28


Well, perhaps you could test the svn access rights and provide this change (and to reference example too)? Smile



No problem, it's working now (didn't commit yet). Is there a naming convention I should follow in regards to the virtual events? For example, WhenAccepted... OnAccept ?

Jeremy

[Updated on: Mon, 04 October 2010 23:54]

Report message to a moderator

Re: SCGI Class [message #29096 is a reply to message #29095] Tue, 05 October 2010 00:14 Go to previous messageGo to next message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
jeremy_c wrote on Mon, 04 October 2010 17:54

luzr wrote on Mon, 04 October 2010 14:28


Well, perhaps you could test the svn access rights and provide this change (and to reference example too)? Smile



No problem, it's working now (didn't commit yet). Is there a naming convention I should follow in regards to the virtual events? For example, WhenAccepted... OnAccept ?

Jeremy


Just "Accept" would do fine IMO, but if you feel like "On" makes it any better, no problem with me Smile
Re: SCGI Class [message #29097 is a reply to message #29088] Tue, 05 October 2010 00:30 Go to previous messageGo to next message
jeremy_c is currently offline  jeremy_c
Messages: 175
Registered: August 2007
Location: Ohio, USA
Experienced Member
luzr wrote on Mon, 04 October 2010 14:28


Well, perhaps you could test the svn access rights and provide this change (and to reference example too)? Smile



Made the change. moved the code and when I attempted to commit I got in the SVN output:

Deleting     Projects\UppSrc\uppsrc\ScgiServer
Adding       Projects\UppSrc\uppsrc\Web\ScgiServer.cpp
Adding       Projects\UppSrc\uppsrc\Web\ScgiServer.h
Sending      Projects\UppSrc\uppsrc\Web\Web.upp
Sending      Projects\UppSrc\uppsrc\Web\init
Transmitting file data ..svn: Commit failed (details follow):
svn: Access denied


TheIDE then promptly quit on me w/a Windows "stopped working" dialog.

Jeremy
Re: SCGI Class [message #29149 is a reply to message #29097] Thu, 07 October 2010 17:40 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
jeremy_c wrote on Tue, 05 October 2010 00:30

luzr wrote on Mon, 04 October 2010 14:28


Well, perhaps you could test the svn access rights and provide this change (and to reference example too)? Smile



Made the change. moved the code and when I attempted to commit I got in the SVN output:

Deleting     Projects\UppSrc\uppsrc\ScgiServer
Adding       Projects\UppSrc\uppsrc\Web\ScgiServer.cpp
Adding       Projects\UppSrc\uppsrc\Web\ScgiServer.h
Sending      Projects\UppSrc\uppsrc\Web\Web.upp
Sending      Projects\UppSrc\uppsrc\Web\init
Transmitting file data ..svn: Commit failed (details follow):
svn: Access denied


TheIDE then promptly quit on me w/a Windows "stopped working" dialog.

Jeremy



Hi, Jeremy... did you solve the issue ? I wish to try your reference example, but it can't find ScgiServer in web package.

Ciao

Max
Re: SCGI Class [message #29171 is a reply to message #29149] Fri, 08 October 2010 14:14 Go to previous messageGo to next message
jeremy_c is currently offline  jeremy_c
Messages: 175
Registered: August 2007
Location: Ohio, USA
Experienced Member
mdelfede wrote on Thu, 07 October 2010 11:40


Hi, Jeremy... did you solve the issue ? I wish to try your reference example, but it can't find ScgiServer in web package.



No, I did not. I sent Luzr a PM w/no response yet. Sorry about this!

Jeremy
Re: SCGI Class [message #29172 is a reply to message #29171] Fri, 08 October 2010 15:57 Go to previous messageGo to next message
mdelfede is currently offline  mdelfede
Messages: 1307
Registered: September 2007
Ultimate Contributor
NP, Jeremy, I adapted reference app to include SgiServer package instead of web and it works perfectly Smile

Ciao

Max
Re: SCGI Class [message #31599 is a reply to message #29172] Wed, 16 March 2011 21:07 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

Recently I've tested ScgiServer class inside FastCGI application under FreeBSD 8 with nginx 0.7.67.
Nginx refused to accept data with following error:
upstream sent unsupported FastCGI protocol version: 72 while reading response header from upstream

I googled and found that FastCGI uses some transport protocol with binary header:
http://www.fastcgi.com/devkit/doc/fcgi-spec.html#S3.1

Then I looked inside ScgiServer source and found no support for this binary header, which is strange.

Also I had exceptions when no QUERY_STRING or REQUEST_METHOD was defined in user's request. This is a potential problem, so I propose change serverVars.Get("...") calls with serverVars.GetAdd("...", "")
Re: SCGI Class [message #31600 is a reply to message #31599] Wed, 16 March 2011 22:06 Go to previous messageGo to next message
zsolt is currently offline  zsolt
Messages: 693
Registered: December 2005
Location: Budapest, Hungary
Contributor
SCGI and FastCGI are different things. They are not compatible.
Re: SCGI Class [message #31603 is a reply to message #31600] Wed, 16 March 2011 23:41 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

shame on me Embarassed
Re: SCGI Class [message #31759 is a reply to message #31603] Thu, 24 March 2011 22:44 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

I plan using SCGI server class for potentially high-load service and so I make some tests first.
Simple example is created:
#include <Core/Core.h>
#include <Web/Web.h>
using namespace Upp;

class MyScgiServer : public ScgiServer
{
public:
	MyScgiServer() :ScgiServer(7000) {}

	virtual void OnRequest()
	{
		Write("HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\nHello, world!");
	}

private:
};

CONSOLE_APP_MAIN
{
	MyScgiServer server;
	server.Run();
	Cout() << "cleanup exit";
}

From the first glance, it works well, at least in junction with nginx/mod_scgi. You may even try no-so-high-load tests which will give you good results:
Quote:

$ ab -t 10 -c 8 localhost:7000/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
Completed 5000 requests
Completed 10000 requests
Completed 15000 requests
Completed 20000 requests
Completed 25000 requests
Completed 30000 requests
Completed 35000 requests
Completed 40000 requests
Completed 45000 requests
Completed 50000 requests
Finished 50000 requests


Server Software:
Server Hostname: localhost
Server Port: 7000

Document Path: /
Document Length: 13 bytes

Concurrency Level: 8
Time taken for tests: 6.806 seconds
Complete requests: 50000
Failed requests: 0
Write errors: 0
Total transferred: 2900000 bytes
HTML transferred: 650000 bytes
Requests per second: 7346.41 [#/sec] (mean)
Time per request: 1.089 [ms] (mean)
Time per request: 0.136 [ms] (mean, across all concurrent requests)
Transfer rate: 416.11 [Kbytes/sec] received

Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.3 0 18
Processing: 0 1 1.1 1 48
Waiting: 0 1 0.8 1 46
Total: 0 1 1.2 1 56

Percentage of the requests served within a certain time (ms)
50% 1
66% 1
75% 1
80% 1
90% 1
95% 1
98% 2
99% 2
100% 56 (longest request)



The bad thing is that if you try to test with concurrency > 10, your test will fail:
Quote:

$ ab -t 10 -c 9 localhost:7000/
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

Benchmarking localhost (be patient)
apr_socket_recv: Connection reset by peer (54)
Total of 13 requests completed



I started investigation on the source of the problem. If you have any clues, you are welcome.
Re: SCGI Class [message #31766 is a reply to message #31759] Fri, 25 March 2011 16:32 Go to previous messageGo to next message
Mindtraveller is currently offline  Mindtraveller
Messages: 917
Registered: August 2007
Location: Russia, Moscow rgn.
Experienced Contributor

OK, the problem is solved for now.
To support multiple connections on high-load projects we just need to increase listen_count parameter for server socket. So I propose a little update to ScgiServer class:
void ScgiServer::Run(int listenCount /*= 10*/)
{
	ServerSocket(serverSock, port, false, listenCount);
	...


That's it. With this little patch scgi server withListenCount = 10000 handles more than 1500 connection requests per second and processing more than 8000 sessions simultaniously.

[Updated on: Fri, 25 March 2011 19:07]

Report message to a moderator

Re: SCGI Class [message #31767 is a reply to message #31766] Fri, 25 March 2011 18:25 Go to previous messageGo to previous message
mirek is currently offline  mirek
Messages: 13975
Registered: November 2005
Ultimate Member
Makes sense, patch applied, thank you!
Previous Topic: How should we deal with SQL column names in conflict with SQL standard keywords
Next Topic: What's up with Google subversion mirror?
Goto Forum:
  


Current Time: Thu Mar 28 13:04:56 CET 2024

Total time taken to generate the page: 0.01843 seconds