changeset 762:3f461f85243d

better rpc thread handling
author Franklin Schmidt <fschmidt@gmail.com>
date Wed, 20 Jul 2016 23:55:59 -0600
parents 99356cfde2f0
children 98df1c19ed7d
files core/src/luan/modules/Rpc.luan http/src/luan/modules/http/LuanHandler.java http/src/luan/modules/http/Server.luan
diffstat 3 files changed, 35 insertions(+), 35 deletions(-) [+]
line wrap: on
line diff
diff -r 99356cfde2f0 -r 3f461f85243d core/src/luan/modules/Rpc.luan
--- a/core/src/luan/modules/Rpc.luan	Wed Jul 20 01:52:20 2016 -0600
+++ b/core/src/luan/modules/Rpc.luan	Wed Jul 20 23:55:59 2016 -0600
@@ -3,8 +3,11 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
 local set_metatable = Luan.set_metatable or error()
+local try = Luan.try or error()
 local Io = require "luan:Io.luan"
 local Thread = require "luan:Thread.luan"
+local Logging = require "luan:logging/Logging.luan"  -- external dependency
+local logger = Logging.logger "Rpc"
 
 
 local M = {}
@@ -37,22 +40,29 @@
 	return M.remote_socket(socket)
 end
 
-function M.responder(fns)
-	local server = Io.socket_server(M.port)
-	return function()
-		local socket = server()
-		M.respond(socket,fns)
+function M.serve(port,fns)
+	local server = Io.socket_server(port or M.port)
+	while true do
+		try {
+			function()
+				local socket = server()
+				local function respond()
+					try {
+						function()
+							M.respond(socket,fns)
+						end
+						catch = function(e)
+							logger.error(e)
+						end
+					}
+				end
+				Thread.fork(respond)
+			end
+			catch = function(e)
+				logger.error(e)
+			end
+		}
 	end
 end
 
---[[
-function M.serve(port,fns)
-	local server = Io.socket_server(port)
-	while true do
-		local socket = server()
-		Thread.fork(function() M.respond(socket,fns) end)
-	end
-end
-]]
-
 return M
diff -r 99356cfde2f0 -r 3f461f85243d http/src/luan/modules/http/LuanHandler.java
--- a/http/src/luan/modules/http/LuanHandler.java	Wed Jul 20 01:52:20 2016 -0600
+++ b/http/src/luan/modules/http/LuanHandler.java	Wed Jul 20 23:55:59 2016 -0600
@@ -12,6 +12,7 @@
 import luan.LuanState;
 import luan.LuanTable;
 import luan.LuanFunction;
+import luan.DeepCloner;
 import luan.LuanException;
 import luan.modules.PackageLuan;
 
@@ -68,13 +69,15 @@
 
 	public static Object callRpc(LuanState luan,String fnName,Object... args) throws LuanException {
 		synchronized(luan) {
-			LuanTable rpc = (LuanTable)PackageLuan.require(luan,"luan:Rpc.luan");
-			LuanTable fns = (LuanTable)rpc.get(luan,"functions");
-			LuanFunction fn = (LuanFunction)fns.get(luan,fnName);
-			if( fn == null )
-				throw new LuanException( "function not found: " + fnName );
-			return fn.call(luan,args);
+			DeepCloner cloner = new DeepCloner();
+			luan = (LuanState)cloner.deepClone(luan);
 		}
+		LuanTable rpc = (LuanTable)PackageLuan.require(luan,"luan:Rpc.luan");
+		LuanTable fns = (LuanTable)rpc.get(luan,"functions");
+		LuanFunction fn = (LuanFunction)fns.get(luan,fnName);
+		if( fn == null )
+			throw new LuanException( "function not found: " + fnName );
+		return fn.call(luan,args);
 	}
 
 }
diff -r 99356cfde2f0 -r 3f461f85243d http/src/luan/modules/http/Server.luan
--- a/http/src/luan/modules/http/Server.luan	Wed Jul 20 01:52:20 2016 -0600
+++ b/http/src/luan/modules/http/Server.luan	Wed Jul 20 23:55:59 2016 -0600
@@ -1,6 +1,5 @@
 local Luan = require "luan:Luan.luan"
 local error = Luan.error
-local try = Luan.try or error()
 local String = require "luan:String.luan"
 local gsub = String.gsub or error()
 local matches = String.matches or error()
@@ -107,19 +106,7 @@
 		return M.luan_handler.call_rpc(fn_name,...)
 	end
 
-	local function run_server()
-		local responder = Rpc.responder()
-		while true do
-			try {
-				responder
-				catch = function(e)
-					logger.warn(e)
-				end
-			}
-		end
-	end
-	
-	Thread.fork(run_server)
+	Thread.fork(Rpc.serve)
 end
 
 function M.serve(dir)