Fix memory leak on closure compilation
This commit is contained in:
parent
2b57ba0806
commit
4fb0c9cfce
5 changed files with 17 additions and 9 deletions
|
@ -22,6 +22,8 @@ import pl.touk.mockserver.api.response.Parameter
|
|||
import javax.xml.bind.JAXBContext
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import java.util.concurrent.CopyOnWriteArraySet
|
||||
import java.util.concurrent.Executor
|
||||
import java.util.concurrent.Executors
|
||||
|
||||
import static pl.touk.mockserver.server.Util.createResponse
|
||||
|
||||
|
@ -32,12 +34,14 @@ class HttpMockServer {
|
|||
private final Map<Integer, HttpServerWraper> childServers = new ConcurrentHashMap<>()
|
||||
private final Set<String> mockNames = new CopyOnWriteArraySet<>()
|
||||
private final ConfigObject configuration = new ConfigObject()
|
||||
private final Executor executor
|
||||
|
||||
private static
|
||||
final JAXBContext requestJaxbContext = JAXBContext.newInstance(AddMock.package.name, AddMock.classLoader)
|
||||
|
||||
HttpMockServer(int port = 9999, ConfigObject initialConfiguration = new ConfigObject()) {
|
||||
httpServerWraper = new HttpServerWraper(port)
|
||||
HttpMockServer(int port = 9999, ConfigObject initialConfiguration = new ConfigObject(), int threads = 10) {
|
||||
executor = Executors.newFixedThreadPool(threads)
|
||||
httpServerWraper = new HttpServerWraper(port, executor)
|
||||
|
||||
initialConfiguration.values()?.each { ConfigObject co ->
|
||||
addMock(co)
|
||||
|
@ -172,7 +176,7 @@ class HttpMockServer {
|
|||
private HttpServerWraper getOrCreateChildServer(int mockPort) {
|
||||
HttpServerWraper child = childServers[mockPort]
|
||||
if (!child) {
|
||||
child = new HttpServerWraper(mockPort)
|
||||
child = new HttpServerWraper(mockPort, executor)
|
||||
childServers.put(mockPort, child)
|
||||
}
|
||||
return child
|
||||
|
|
|
@ -5,7 +5,7 @@ import com.sun.net.httpserver.HttpServer
|
|||
import groovy.transform.PackageScope
|
||||
import groovy.util.logging.Slf4j
|
||||
|
||||
import java.util.concurrent.Executors
|
||||
import java.util.concurrent.Executor
|
||||
|
||||
@Slf4j
|
||||
@PackageScope
|
||||
|
@ -15,11 +15,11 @@ class HttpServerWraper {
|
|||
|
||||
private List<ContextExecutor> executors = []
|
||||
|
||||
HttpServerWraper(int port) {
|
||||
HttpServerWraper(int port, Executor executor) {
|
||||
this.port = port
|
||||
InetSocketAddress addr = new InetSocketAddress(Inet4Address.getByName("0.0.0.0"), port)
|
||||
httpServer = HttpServer.create(addr, 0)
|
||||
httpServer.executor = Executors.newWorkStealingPool()
|
||||
httpServer.executor = executor
|
||||
log.info("Http server starting on port $port...")
|
||||
httpServer.start()
|
||||
log.info('Http server is started')
|
||||
|
|
|
@ -21,9 +21,11 @@ class Main {
|
|||
private static HttpMockServer startMockServer(String... args) {
|
||||
switch (args.length) {
|
||||
case 1:
|
||||
return new HttpMockServer(args[0] as int)
|
||||
return new HttpMockServer(args[0] as int, new ConfigObject())
|
||||
case 2:
|
||||
return new HttpMockServer(args[0] as int, new ConfigSlurper().parse(new File(args[1]).toURI().toURL()))
|
||||
case 3:
|
||||
return new HttpMockServer(args[0] as int, new ConfigSlurper().parse(new File(args[1]).toURI().toURL()), args[2] as int)
|
||||
default:
|
||||
return new HttpMockServer()
|
||||
}
|
||||
|
|
|
@ -103,7 +103,9 @@ class Mock implements Comparable<Mock> {
|
|||
}
|
||||
compilerConfiguration.addCompilationCustomizers(customizer)
|
||||
GroovyShell sh = new GroovyShell(this.class.classLoader, compilerConfiguration);
|
||||
return sh.evaluate(predicate) as Closure
|
||||
Closure closure = sh.evaluate(predicate) as Closure
|
||||
sh.resetLoadedClasses()
|
||||
return closure
|
||||
}
|
||||
|
||||
void setResponse(String response) {
|
||||
|
|
2
pom.xml
2
pom.xml
|
@ -24,7 +24,7 @@
|
|||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>
|
||||
<groovy.version>2.4.1</groovy.version>
|
||||
<groovy.version>2.4.11</groovy.version>
|
||||
<httpclient.version>4.3.5</httpclient.version>
|
||||
<spock-core.version>1.0-groovy-2.4</spock-core.version>
|
||||
<commons-lang3.version>3.3.2</commons-lang3.version>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue