changeset 1381:8d7a39ca2c0c

add Rpc in_backup_read_lock
author Franklin Schmidt <fschmidt@gmail.com>
date Sun, 28 Jul 2019 20:15:38 -0600
parents 04482e2a6ca3
children 9604579c1c9b
files src/luan/modules/Rpc.luan src/luan/modules/ThreadLuan.java src/luan/modules/host/Hosting.luan
diffstat 3 files changed, 34 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
diff -r 04482e2a6ca3 -r 8d7a39ca2c0c src/luan/modules/Rpc.luan
--- a/src/luan/modules/Rpc.luan	Sun Jul 28 11:06:37 2019 -0600
+++ b/src/luan/modules/Rpc.luan	Sun Jul 28 20:15:38 2019 -0600
@@ -25,6 +25,7 @@
 local Table = require "luan:Table.luan"
 local java_to_table_deep = Table.java_to_table_deep or error()
 local unpack = Table.unpack or error()
+local ThreadLuan = require "java:luan.modules.ThreadLuan"
 local Logging = require "luan:logging/Logging.luan"
 local logger = Logging.logger "Rpc"
 
@@ -135,12 +136,22 @@
 	end_function
 	function responder.respond()
 		local call = server.read()
-		if call==nil then return end
+		if call==nil then
+			return
+		end
 		local cmd = call.cmd
 		if cmd == "close" then
 			server.close()
 			return
 		end_if
+		if cmd == "in_backup_read_lock" then
+			ThreadLuan.lock(Thread.backup_read_lock)
+			function responder.after_close()
+				Thread.backup_read_lock.unlock()
+			end
+			server.write(RpcResult.new())
+			return
+		end_if
 		local fn = fns[cmd]
 		if fn == nil then
 			server.write(JavaRpc.COMMAND_NOT_FOUND)
@@ -185,6 +196,10 @@
 			return call(key,...)
 		end
 	end
+	local err = Luan.new_error("not closed")
+	function mt.__gc(_)
+		socket.isClosed() or logger.error(err)
+	end
 	local t = {}
 	set_metatable(t,mt)
 	return t
@@ -205,9 +220,10 @@
 			function()
 				local socket = socket_server.accept()
 				local function server()
+					local responder = nil
 					try {
 						function()
-							local responder = rpc_responder(socket,fns)
+							responder = rpc_responder(socket,fns)
 							while not responder.is_closed() do
 								responder.respond()
 							end
@@ -215,6 +231,9 @@
 						catch = function(e)
 							logger.warn(e)
 						end
+						finally = function()
+							responder and responder.after_close and responder.after_close()
+						end
 					}
 				end
 				Thread.fork(server)
diff -r 04482e2a6ca3 -r 8d7a39ca2c0c src/luan/modules/ThreadLuan.java
--- a/src/luan/modules/ThreadLuan.java	Sun Jul 28 11:06:37 2019 -0600
+++ b/src/luan/modules/ThreadLuan.java	Sun Jul 28 20:15:38 2019 -0600
@@ -238,11 +238,17 @@
 
 	public static final ReadWriteLock backupLock = new ReentrantReadWriteLock();
 
-	public static Object runInLock(Lock lock,LuanFunction fn,Object... args)
+	public static void lock(Lock lock)
 		throws LuanException, InterruptedException
 	{
 		if( !lock.tryLock(10,TimeUnit.MINUTES) )
 			throw new LuanException("failed to acquire lock");
+	}
+
+	public static Object runInLock(Lock lock,LuanFunction fn,Object... args)
+		throws LuanException, InterruptedException
+	{
+		lock(lock);
 		try {
 			return fn.call(args);
 		} finally {
diff -r 04482e2a6ca3 -r 8d7a39ca2c0c src/luan/modules/host/Hosting.luan
--- a/src/luan/modules/host/Hosting.luan	Sun Jul 28 11:06:37 2019 -0600
+++ b/src/luan/modules/host/Hosting.luan	Sun Jul 28 20:15:38 2019 -0600
@@ -10,6 +10,8 @@
 local Rpc = require "luan:Rpc.luan"
 local String = require "luan:String.luan"
 local matches = String.matches or error()
+local Logging = require "luan:logging/Logging.luan"
+local logger = Logging.logger "Hosting"
 
 
 local Hosting = {}
@@ -21,6 +23,7 @@
 	my_dir.is_directory() or error("'"..dir.."' is not a directory")
 	local host = Rpc.remote(domain)
 	local tree = host.get(domain,password)
+	host.in_backup_read_lock()
 	if tree == nil then
 		print("creating "..domain)
 		tree = host.create(domain,password)
@@ -64,6 +67,7 @@
 
 function Hosting.delete(domain,password)
 	local host = Rpc.remote(domain)
+	host.in_backup_read_lock()
 	host.delete(domain,password)
 	host.close()
 end
@@ -77,6 +81,7 @@
 
 function Hosting.change_domain(old_domain,new_domain,password)
 	local host = Rpc.remote(new_domain)
+	host.in_backup_read_lock()
 	local rtn = host.change_domain(old_domain,new_domain,password)
 	host.close()
 	return rtn
@@ -84,6 +89,7 @@
 
 function Hosting.change_password(domain,old_password,new_password)
 	local host = Rpc.remote(domain)
+	host.in_backup_read_lock()
 	local rtn = host.change_password(domain,old_password,new_password)
 	host.close()
 	return rtn