Skip to content

Commit a550978

Browse files
committed
Merge pull request #4 from obscurerichard/pg-fixes
Fix for issue #3: refactoring of connection pool handing
2 parents 652c964 + a4d5dbd commit a550978

File tree

2 files changed

+271
-25
lines changed

2 files changed

+271
-25
lines changed

server/routes/index.js

Lines changed: 28 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,12 @@ router.post('/api/v1/todos', function(req, res) {
1919

2020
// Get a Postgres client from the connection pool
2121
pg.connect(connectionString, function(err, client, done) {
22+
// Handle connection errors
23+
if(err) {
24+
done();
25+
console.log(err);
26+
return res.status(500).json({ success: false, data: err});
27+
}
2228

2329
// SQL Query > Insert Data
2430
client.query("INSERT INTO items(text, complete) values($1, $2)", [data.text, data.complete]);
@@ -33,14 +39,10 @@ router.post('/api/v1/todos', function(req, res) {
3339

3440
// After all data is returned, close connection and return results
3541
query.on('end', function() {
36-
client.end();
42+
done();
3743
return res.json(results);
3844
});
3945

40-
// Handle Errors
41-
if(err) {
42-
console.log(err);
43-
}
4446

4547
});
4648
});
@@ -51,6 +53,12 @@ router.get('/api/v1/todos', function(req, res) {
5153

5254
// Get a Postgres client from the connection pool
5355
pg.connect(connectionString, function(err, client, done) {
56+
// Handle connection errors
57+
if(err) {
58+
done();
59+
console.log(err);
60+
return res.status(500).json({ success: false, data: err});
61+
}
5462

5563
// SQL Query > Select Data
5664
var query = client.query("SELECT * FROM items ORDER BY id ASC;");
@@ -62,15 +70,10 @@ router.get('/api/v1/todos', function(req, res) {
6270

6371
// After all data is returned, close connection and return results
6472
query.on('end', function() {
65-
client.end();
73+
done();
6674
return res.json(results);
6775
});
6876

69-
// Handle Errors
70-
if(err) {
71-
console.log(err);
72-
}
73-
7477
});
7578

7679
});
@@ -87,6 +90,12 @@ router.put('/api/v1/todos/:todo_id', function(req, res) {
8790

8891
// Get a Postgres client from the connection pool
8992
pg.connect(connectionString, function(err, client, done) {
93+
// Handle connection errors
94+
if(err) {
95+
done();
96+
console.log(err);
97+
return res.status(500).send(json({ success: false, data: err}));
98+
}
9099

91100
// SQL Query > Update Data
92101
client.query("UPDATE items SET text=($1), complete=($2) WHERE id=($3)", [data.text, data.complete, id]);
@@ -101,15 +110,9 @@ router.put('/api/v1/todos/:todo_id', function(req, res) {
101110

102111
// After all data is returned, close connection and return results
103112
query.on('end', function() {
104-
client.end();
113+
done();
105114
return res.json(results);
106115
});
107-
108-
// Handle Errors
109-
if(err) {
110-
console.log(err);
111-
}
112-
113116
});
114117

115118
});
@@ -124,6 +127,12 @@ router.delete('/api/v1/todos/:todo_id', function(req, res) {
124127

125128
// Get a Postgres client from the connection pool
126129
pg.connect(connectionString, function(err, client, done) {
130+
// Handle connection errors
131+
if(err) {
132+
done();
133+
console.log(err);
134+
return res.status(500).json({ success: false, data: err});
135+
}
127136

128137
// SQL Query > Delete Data
129138
client.query("DELETE FROM items WHERE id=($1)", [id]);
@@ -138,15 +147,9 @@ router.delete('/api/v1/todos/:todo_id', function(req, res) {
138147

139148
// After all data is returned, close connection and return results
140149
query.on('end', function() {
141-
client.end();
150+
done();
142151
return res.json(results);
143152
});
144-
145-
// Handle Errors
146-
if(err) {
147-
console.log(err);
148-
}
149-
150153
});
151154

152155
});

test/conn-handling-test-results.txt

Lines changed: 243 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,243 @@
1+
$ test/load-test.sh; git stash apply; git diff < /dev/null; test/load-test.sh
2+
This is ApacheBench, Version 2.3 <$Revision: 1663405 $>
3+
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
4+
Licensed to The Apache Software Foundation, http://www.apache.org/
5+
6+
Benchmarking localhost (be patient)
7+
1s: 1 connections
8+
2s: 4 connections
9+
3s: 4 connections
10+
4s: 4 connections
11+
5s: 3 connections
12+
6s: 4 connections
13+
7s: 5 connections
14+
8s: 5 connections
15+
9s: 6 connections
16+
10s: 5 connections
17+
Finished 2112 requests
18+
19+
20+
Server Software:
21+
Server Hostname: localhost
22+
Server Port: 3000
23+
24+
Document Path: /api/v1/todos
25+
Document Length: 119 bytes
26+
27+
Concurrency Level: 20
28+
Time taken for tests: 10.008 seconds
29+
Complete requests: 2112
30+
Failed requests: 0
31+
Total transferred: 650496 bytes
32+
HTML transferred: 251328 bytes
33+
Requests per second: 211.04 [#/sec] (mean)
34+
Time per request: 94.771 [ms] (mean)
35+
Time per request: 4.739 [ms] (mean, across all concurrent requests)
36+
Transfer rate: 63.48 [Kbytes/sec] received
37+
38+
Connection Times (ms)
39+
min mean[+/-sd] median max
40+
Connect: 0 0 0.2 0 3
41+
Processing: 59 94 16.6 90 183
42+
Waiting: 59 94 16.5 89 183
43+
Total: 61 94 16.6 90 184
44+
45+
Percentage of the requests served within a certain time (ms)
46+
50% 90
47+
66% 96
48+
75% 101
49+
80% 104
50+
90% 118
51+
95% 130
52+
98% 139
53+
99% 145
54+
100% 184 (longest request)
55+
On branch master
56+
Your branch is ahead of 'origin/master' by 5 commits.
57+
(use "git push" to publish your local commits)
58+
Changes not staged for commit:
59+
(use "git add <file>..." to update what will be committed)
60+
(use "git checkout -- <file>..." to discard changes in working directory)
61+
62+
modified: server/routes/index.js
63+
64+
no changes added to commit (use "git add" and/or "git commit -a")
65+
diff --git a/server/routes/index.js b/server/routes/index.js
66+
index 45d9a05..e6ca250 100644
67+
--- a/server/routes/index.js
68+
+++ b/server/routes/index.js
69+
@@ -19,6 +19,12 @@ router.post('/api/v1/todos', function(req, res) {
70+
71+
// Get a Postgres client from the connection pool
72+
pg.connect(connectionString, function(err, client, done) {
73+
+ // Handle connection errors
74+
+ if(err) {
75+
+ done();
76+
+ console.log(err);
77+
+ return res.status(500).json({ success: false, data: err});
78+
+ }
79+
80+
// SQL Query > Insert Data
81+
client.query("INSERT INTO items(text, complete) values($1, $2)", [data.text, data.complete]);
82+
@@ -33,14 +39,10 @@ router.post('/api/v1/todos', function(req, res) {
83+
84+
// After all data is returned, close connection and return results
85+
query.on('end', function() {
86+
- client.end();
87+
+ done();
88+
return res.json(results);
89+
});
90+
91+
- // Handle Errors
92+
- if(err) {
93+
- console.log(err);
94+
- }
95+
96+
});
97+
});
98+
@@ -51,6 +53,12 @@ router.get('/api/v1/todos', function(req, res) {
99+
100+
// Get a Postgres client from the connection pool
101+
pg.connect(connectionString, function(err, client, done) {
102+
+ // Handle connection errors
103+
+ if(err) {
104+
+ done();
105+
+ console.log(err);
106+
+ return res.status(500).json({ success: false, data: err});
107+
+ }
108+
109+
// SQL Query > Select Data
110+
var query = client.query("SELECT * FROM items ORDER BY id ASC;");
111+
@@ -62,15 +70,10 @@ router.get('/api/v1/todos', function(req, res) {
112+
113+
// After all data is returned, close connection and return results
114+
query.on('end', function() {
115+
- client.end();
116+
+ done();
117+
return res.json(results);
118+
});
119+
120+
- // Handle Errors
121+
- if(err) {
122+
- console.log(err);
123+
- }
124+
-
125+
});
126+
127+
});
128+
@@ -87,6 +90,12 @@ router.put('/api/v1/todos/:todo_id', function(req, res) {
129+
130+
// Get a Postgres client from the connection pool
131+
pg.connect(connectionString, function(err, client, done) {
132+
+ // Handle connection errors
133+
+ if(err) {
134+
+ done();
135+
+ console.log(err);
136+
+ return res.status(500).send(json({ success: false, data: err}));
137+
+ }
138+
139+
// SQL Query > Update Data
140+
client.query("UPDATE items SET text=($1), complete=($2) WHERE id=($3)", [data.text, data.complete, id]);
141+
@@ -101,15 +110,9 @@ router.put('/api/v1/todos/:todo_id', function(req, res) {
142+
143+
// After all data is returned, close connection and return results
144+
query.on('end', function() {
145+
- client.end();
146+
+ done();
147+
return res.json(results);
148+
});
149+
-
150+
- // Handle Errors
151+
- if(err) {
152+
- console.log(err);
153+
- }
154+
-
155+
});
156+
157+
});
158+
@@ -124,6 +127,12 @@ router.delete('/api/v1/todos/:todo_id', function(req, res) {
159+
160+
// Get a Postgres client from the connection pool
161+
pg.connect(connectionString, function(err, client, done) {
162+
+ // Handle connection errors
163+
+ if(err) {
164+
+ done();
165+
+ console.log(err);
166+
+ return res.status(500).json({ success: false, data: err});
167+
+ }
168+
169+
// SQL Query > Delete Data
170+
client.query("DELETE FROM items WHERE id=($1)", [id]);
171+
@@ -138,15 +147,9 @@ router.delete('/api/v1/todos/:todo_id', function(req, res) {
172+
173+
// After all data is returned, close connection and return results
174+
query.on('end', function() {
175+
- client.end();
176+
+ done();
177+
return res.json(results);
178+
});
179+
-
180+
- // Handle Errors
181+
- if(err) {
182+
- console.log(err);
183+
- }
184+
-
185+
});
186+
187+
});
188+
This is ApacheBench, Version 2.3 <$Revision: 1663405 $>
189+
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
190+
Licensed to The Apache Software Foundation, http://www.apache.org/
191+
192+
Benchmarking localhost (be patient)
193+
1s: 1 connections
194+
2s: 11 connections
195+
3s: 11 connections
196+
4s: 11 connections
197+
5s: 11 connections
198+
6s: 11 connections
199+
7s: 11 connections
200+
Completed 5000 requests
201+
8s: 11 connections
202+
9s: 11 connections
203+
10s: 11 connections
204+
Finished 8032 requests
205+
206+
207+
Server Software:
208+
Server Hostname: localhost
209+
Server Port: 3000
210+
211+
Document Path: /api/v1/todos
212+
Document Length: 119 bytes
213+
214+
Concurrency Level: 20
215+
Time taken for tests: 10.008 seconds
216+
Complete requests: 8032
217+
Failed requests: 0
218+
Total transferred: 2473856 bytes
219+
HTML transferred: 955808 bytes
220+
Requests per second: 802.55 [#/sec] (mean)
221+
Time per request: 24.921 [ms] (mean)
222+
Time per request: 1.246 [ms] (mean, across all concurrent requests)
223+
Transfer rate: 241.39 [Kbytes/sec] received
224+
225+
Connection Times (ms)
226+
min mean[+/-sd] median max
227+
Connect: 0 1 0.4 0 11
228+
Processing: 13 24 6.6 23 119
229+
Waiting: 13 24 6.6 22 118
230+
Total: 15 25 6.7 23 120
231+
ERROR: The median and mean for the initial connection time are more than twice the standard
232+
deviation apart. These results are NOT reliable.
233+
234+
Percentage of the requests served within a certain time (ms)
235+
50% 23
236+
66% 25
237+
75% 27
238+
80% 28
239+
90% 31
240+
95% 34
241+
98% 41
242+
99% 48
243+
100% 120 (longest request)

0 commit comments

Comments
 (0)