From ab0f188cafc682f7b116a9309ea176d6061808a0 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 19 May 2018 01:04:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9Bgproc=E5=85=B1=E4=BA=AB?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E6=9C=BA=E5=88=B6=EF=BC=8C=E5=8E=BB=E6=8E=89?= =?UTF-8?q?=E9=80=9A=E4=BF=A1=E6=96=87=E4=BB=B6=E8=BF=87=E6=BB=A4=E6=9C=BA?= =?UTF-8?q?=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/gproc/gproc_comm.go | 52 +++++++++++++++------------------------- 1 file changed, 19 insertions(+), 33 deletions(-) diff --git a/g/os/gproc/gproc_comm.go b/g/os/gproc/gproc_comm.go index 693e92942..3f3e47b9d 100644 --- a/g/os/gproc/gproc_comm.go +++ b/g/os/gproc/gproc_comm.go @@ -3,6 +3,8 @@ // This Source Code Form is subject to the terms of the MIT License. // If a copy of the MIT was not distributed with this file, // You can obtain one at https://gitee.com/johng/gf. +// @todo 后期改为tcp进程通信形式 + package gproc @@ -25,8 +27,6 @@ import ( const ( // 由于子进程的temp dir有可能会和父进程不一致(特别是windows下),影响进程间通信,这里统一使用环境变量设置 gPROC_TEMP_DIR_ENV_KEY = "gproc.tempdir" - // 自动通信文件清理时间间隔 - gPROC_COMM_AUTO_CLEAR_INTERVAL = 10*time.Second // 写入通信数据失败时候的重试次数 gPROC_COMM_FAILURE_RETRY_COUNT = 3 // (毫秒)主动通信内容检查时间间隔 @@ -49,23 +49,10 @@ type Msg struct { // 进程管理/通信初始化操作 func init() { path := getCommFilePath(os.Getpid()) - if !gfile.Exists(path) { - // 判断是否需要创建通信文件 - commLocker.Lock() - err := gfile.Create(path) - commLocker.UnLock() - if err != nil { - glog.Error(err) - os.Exit(1) - } - } - // 检测写入权限 - if !gfile.IsWritable(path) { - glog.Errorfln("%s is not writable for gproc", path) - os.Exit(1) - } + checkAndInitCommFile(path) + commLocker.Lock() fileMtime := gfile.MTime(path) - //commUpdateTime.Set(fileMtime) + commLocker.UnLock() if gtime.Second() - fileMtime < 10 { // 初始化时读取已有数据(文件修改时间在10秒以内) checkCommBuffer(path) @@ -83,26 +70,25 @@ func init() { glog.Error(err) } - go autoClearCommDir() go autoActiveCheckComm() } -// 自动清理通信目录文件 -// @todo 目前是以时间过期规则进行清理,后期可以考虑加入进程存在性判断 -func autoClearCommDir() { - dirPath := getCommDirPath() - for { - time.Sleep(gPROC_COMM_AUTO_CLEAR_INTERVAL) - if commClearLocker.TryLock() { - for _, name := range gfile.ScanDir(dirPath) { - path := dirPath + gfile.Separator + name - if gtime.Second() - gfile.MTime(path) >= 10 { - gfile.Remove(path) - } - } - commClearLocker.UnLock() +// 检测并初始化通信文件 +func checkAndInitCommFile(path string) { + commLocker.Lock() + defer commLocker.UnLock() + if !gfile.Exists(path) { + err := gfile.Create(path) + if err != nil { + glog.Error(err) + os.Exit(1) } } + // 检测写入权限 + if !gfile.IsWritable(path) { + glog.Errorfln("%s is not writable for gproc", path) + os.Exit(1) + } } // 主动通信内容检测