/
NGSessionPool.java
122 lines (103 loc) · 2.74 KB
/
NGSessionPool.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
Copyright 2004, Martian Software, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package vimclojure.nailgun;
/**
* Provides NGSession pooling functionality. One parameter, "maxIdle",
* governs its behavior by setting the maximum number of idle NGSession
* threads it will allow. It creates a pool of size maxIdle - 1, because
* one NGSession is kept "on deck" by the NGServer in order to eke out
* a little extra responsiveness.
*
* @author <a href="http://www.martiansoftware.com/contact.html">Marty Lamb</a>
*/
class NGSessionPool {
/**
* number of sessions to store in the pool
*/
int poolSize = 0;
/**
* the pool itself
*/
NGSession[] pool = null;
/**
* The number of sessions currently in the pool
*/
int poolEntries = 0;
/**
* reference to server we're working for
*/
NGServer server = null;
/**
* have we been shut down?
*/
boolean done = false;
/**
* synchronization object
*/
private Object lock = new Object();
/**
* Creates a new NGSessionRunner operating for the specified server, with
* the specified number of threads
* @param server the server to work for
* @param poolSize the maximum number of idle threads to allow
*/
NGSessionPool(NGServer server, int maxIdle) {
this.server = server;
this.poolSize = maxIdle - 1;
pool = new NGSession[poolSize];
poolEntries = 0;
}
/**
* Returns an NGSession from the pool, or creates one if necessary
* @return an NGSession ready to work
*/
NGSession take() {
NGSession result;
synchronized(lock) {
if (poolEntries == 0) {
result = new NGSession(this, server);
result.start();
} else {
--poolEntries;
result = pool[poolEntries];
}
}
return (result);
}
/**
* Returns an NGSession to the pool. The pool may choose to shutdown
* the thread if the pool is full
* @param session the NGSession to return to the pool
*/
void give(NGSession session) {
if (done || poolEntries == poolSize) {
session.shutdown();
} else {
synchronized(lock) {
pool[poolEntries] = session;
++poolEntries;
}
}
}
/**
* Shuts down the pool. Running nails are allowed to finish.
*/
void shutdown() {
done = true;
synchronized(lock) {
while (poolEntries > 0) {
take().shutdown();
}
}
}
}