diff options
| author | ruki <[email protected]> | 2022-05-21 10:08:05 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2022-05-21 10:08:05 +0800 |
| commit | 525f5145e9b0a1a28757bf8273b74d4caf675f57 (patch) | |
| tree | 5504a4de932edeb034a664c49dbbd053a9873e85 /xmake/modules | |
| parent | a391526b3c1ffdcad34c2095980ee046e161016e (diff) | |
use remote cache
Diffstat (limited to 'xmake/modules')
| -rw-r--r-- | xmake/modules/private/cache/build_cache.lua | 12 | ||||
| -rw-r--r-- | xmake/modules/private/service/message.lua | 17 | ||||
| -rw-r--r-- | xmake/modules/private/service/remote_cache/client.lua | 32 | ||||
| -rw-r--r-- | xmake/modules/private/service/remote_cache/server.lua | 2 | ||||
| -rw-r--r-- | xmake/modules/private/service/remote_cache/server_session.lua | 14 |
5 files changed, 76 insertions, 1 deletions
diff --git a/xmake/modules/private/cache/build_cache.lua b/xmake/modules/private/cache/build_cache.lua index d2180ab58..66d1abd38 100644 --- a/xmake/modules/private/cache/build_cache.lua +++ b/xmake/modules/private/cache/build_cache.lua @@ -22,6 +22,7 @@ import("core.base.bytes") import("core.base.hashset") import("core.project.config") +import("private.service.remote_cache.client", {alias = "remote_cache_client"}) -- is enabled? function is_enabled() @@ -110,6 +111,11 @@ function get(cachekey) if os.isfile(objectfile_cached) then _g.hit_count = (_g.hit_count or 0) + 1 return objectfile_cached + elseif remote_cache_client.is_connected() and + remote_cache_client.singleton():pull(cachekey, objectfile_cached) and + os.isfile(objectfile_cached) then + _g.hit_count = (_g.hit_count or 0) + 1 + return objectfile_cached end end @@ -118,6 +124,12 @@ function put(cachekey, objectfile) local objectfile_cached = path.join(rootdir(), cachekey:sub(1, 2):lower(), cachekey) os.cp(objectfile, objectfile_cached) _g.newfiles_count = (_g.newfiles_count or 0) + 1 + if remote_cache_client.is_connected() then + local cacheinfo = remote_cache_client.singleton():cacheinfo(cachekey) + if not cacheinfo or not cacheinfo.exists then + remote_cache_client.singleton():push(cachekey, objectfile) + end + end end -- build with cache diff --git a/xmake/modules/private/service/message.lua b/xmake/modules/private/service/message.lua index 71cd9ce28..88ee90747 100644 --- a/xmake/modules/private/service/message.lua +++ b/xmake/modules/private/service/message.lua @@ -35,6 +35,7 @@ message.CODE_SYNC = 7 -- sync files between server and client message.CODE_COMPILE = 8 -- compile the given file from client in server message.CODE_PULL = 9 -- pull the given file from server message.CODE_PUSH = 10 -- push the given file to server +message.CODE_FILEINFO = 11 -- get the given file info in server -- init message function message:init(body) @@ -101,6 +102,11 @@ function message:is_push() return self:code() == message.CODE_PUSH end +-- is fileinfo message? +function message:is_fileinfo() + return self:code() == message.CODE_FILEINFO +end + -- get user authorization function message:token() return self:body().token @@ -264,6 +270,17 @@ function new_push(session_id, filename, opt) }) end +-- new fileinfo message +function new_fileinfo(session_id, filename, opt) + opt = opt or {} + return _new({ + code = message.CODE_PUSH, + filename = filename, + session_id = session_id, + token = opt.token + }) +end + function main(body) return _new(body) end diff --git a/xmake/modules/private/service/remote_cache/client.lua b/xmake/modules/private/service/remote_cache/client.lua index 13a20d65c..b2ff17873 100644 --- a/xmake/modules/private/service/remote_cache/client.lua +++ b/xmake/modules/private/service/remote_cache/client.lua @@ -232,6 +232,38 @@ function remote_cache_client:push(cachekey, cachefile) end end +-- get cache file info +function remote_cache_client:cacheinfo(cachekey) + assert(self:is_connected(), "%s: has been not connected!", self) + local addr = self:addr() + local port = self:port() + local sock = assert(socket.connect(addr, port), "%s: server unreachable!", self) + local session_id = self:session_id() + local errors + local ok = false + local cacheinfo + dprint("%s: get cacheinfo(%s) in %s:%d ..", self, cachekey, addr, port) + local stream = socket_stream(sock) + if stream:send_msg(message.new_clean(session_id, {token = self:token()})) and stream:flush() then + local msg = stream:recv_msg() + if msg then + dprint(msg:body()) + if msg:success() then + cacheinfo = msg:body().fileinfo + ok = true + else + errors = msg:errors() + end + end + end + if ok then + dprint("%s: get cacheinfo(%s) ok!", self, cachekey) + else + dprint("%s: get cacheinfo(%s) failed in %s:%d, %s", self, cachekey, addr, port, errors or "unknown") + end + return cacheinfo +end + -- clean server files function remote_cache_client:clean() assert(self:is_connected(), "%s: has been not connected!", self) diff --git a/xmake/modules/private/service/remote_cache/server.lua b/xmake/modules/private/service/remote_cache/server.lua index c62a15e84..42d0046ad 100644 --- a/xmake/modules/private/service/remote_cache/server.lua +++ b/xmake/modules/private/service/remote_cache/server.lua @@ -90,6 +90,8 @@ function remote_cache_server:_on_handle(stream, msg) session:push(respmsg) elseif msg:is_pull() then session:pull(respmsg) + elseif msg:is_fileinfo() then + session:fileinfo(respmsg) elseif msg:is_clean() then session:clean() end diff --git a/xmake/modules/private/service/remote_cache/server_session.lua b/xmake/modules/private/service/remote_cache/server_session.lua index 53845a189..a73734749 100644 --- a/xmake/modules/private/service/remote_cache/server_session.lua +++ b/xmake/modules/private/service/remote_cache/server_session.lua @@ -101,9 +101,21 @@ function server_session:push(respmsg) local stream = self:stream() local cachekey = body.filename local cachefile = path.join(self:cachedir(), cachekey:sub(1, 2), cachekey) - if not stream:recv_file(cachefile) then + local cachefile_tmp = cachefile .. ".tmp" + if not stream:recv_file(cachefile_tmp) then + os.tryrm(cachefile_tmp) raise("recv %s failed!", cachefile) end + os.mv(cachefile_tmp, cachefile) +end + +-- get file info +function server_session:fileinfo(respmsg) + local body = respmsg:body() + local stream = self:stream() + local cachekey = body.filename + local cachefile = path.join(self:cachedir(), cachekey:sub(1, 2), cachekey) + body.fileinfo = {filesize = os.filesize(cachefile), exists = os.isfile(cachefile)} end -- clean files |
