gproc多进程通信功能增加CommEnabled属性控制

This commit is contained in:
john
2018-06-29 13:03:29 +08:00
parent e8693643fa
commit 4011eb4868
8 changed files with 64 additions and 50 deletions

4
TODO
View File

@ -9,6 +9,10 @@ orm增加更多数据库支持
ghttp.Response增加输出内容后自动退出当前请求机制不需要用户手动return参考beego如何实现
Cookie&Session数据池化处理
ghttp.Client增加proxy特性
gtime增加对时区转换的封装并简化失去转换时对类似+80500时区的支持
改进gf-orm的where查询功能参考thinkphp 里的where查询语法
DONE:
1. gconv完善针对不同类型的判断例如尽量减少sprintf("%v", xxx)来执行string类型的转换

View File

@ -110,7 +110,7 @@ func (c *gCmdOption) GetBool(key string) bool {
return false
}
// 绑定命令行参数及对应的命令函数,注意参数是函数的内存地址
// 绑定命令行参数及对应的命令函数,注意命令函数参数是函数的内存地址
// 如果操作失败返回错误信息
func BindHandle (cmd string, f func()) error {
if _, ok := cmdFuncMap[cmd]; ok {

View File

@ -17,8 +17,9 @@ import (
)
const (
gPROC_ENV_KEY_PPID_KEY = "gproc.ppid"
gPROC_TEMP_DIR_ENV_KEY = "gproc.tempdir"
gPROC_ENV_KEY_PPID_KEY = "GPROC_PPID"
gPROC_ENV_KEY_COMM_KEY = "GPROC_COMM_ENABLED"
gPROC_TEMP_DIR_ENV_KEY = "GPROC_TEMP_DIR"
)
// 进程开始执行时间
@ -36,7 +37,7 @@ func PPid() int {
}
// gPROC_ENV_KEY_PPID_KEY为gproc包自定义的父进程
ppidValue := os.Getenv(gPROC_ENV_KEY_PPID_KEY)
if ppidValue != "" {
if ppidValue != "" && ppidValue != "0" {
return gconv.Int(ppidValue)
}
return PPidOS()
@ -49,7 +50,8 @@ func PPidOS() int {
// 判断当前进程是否为gproc创建的子进程
func IsChild() bool {
return os.Getenv(gPROC_ENV_KEY_PPID_KEY) != ""
ppidValue := os.Getenv(gPROC_ENV_KEY_PPID_KEY)
return ppidValue != "" && ppidValue != "0"
}
// 设置gproc父进程ID当ppid为0时表示该进程为gproc主进程否则为gproc子进程

View File

@ -37,7 +37,9 @@ type sendQueueItem struct {
// 进程管理/通信初始化操作
func init() {
go startTcpListening()
if os.Getenv(gPROC_ENV_KEY_COMM_KEY) == "1" {
go startTcpListening()
}
}
// 获取指定进程的通信文件地址

View File

@ -9,10 +9,7 @@ package gproc
import (
"os"
"strings"
"os/exec"
"gitee.com/johng/gf/g/container/gmap"
"fmt"
)
// 进程管理器
@ -27,40 +24,6 @@ func NewManager() *Manager {
}
}
// 创建一个进程(不执行)
func NewProcess(path string, args []string, environment []string) *Process {
env := make([]string, len(environment) + 1)
for k, v := range environment {
env[k] = v
}
env[len(env) - 1] = fmt.Sprintf("%s=%s", gPROC_TEMP_DIR_ENV_KEY, os.TempDir())
p := &Process {
Manager : nil,
PPid : os.Getpid(),
Cmd : exec.Cmd {
Args : []string{path},
Path : path,
Stdin : os.Stdin,
Stdout : os.Stdout,
Stderr : os.Stderr,
Env : env,
ExtraFiles : make([]*os.File, 0),
},
}
// 当前工作目录
if d, err := os.Getwd(); err == nil {
p.Dir = d
}
if len(args) > 0 {
start := 0
if strings.EqualFold(path, args[0]) {
start = 1
}
p.Args = append(p.Args, args[start : ]...)
}
return p
}
// 创建一个进程(不执行)
func (m *Manager) NewProcess(path string, args []string, environment []string) *Process {
p := NewProcess(path, args, environment)

View File

@ -11,13 +11,50 @@ import (
"fmt"
"os/exec"
"errors"
"strings"
)
// 子进程
type Process struct {
exec.Cmd
Manager *Manager // 所属进程管理器
PPid int // 自定义关联的父进程ID
Manager *Manager // 所属进程管理器
PPid int // 自定义关联的父进程ID
CommEnabled bool // 是否开启TCP通信监听服务
}
// 创建一个进程(不执行)
func NewProcess(path string, args []string, environment []string) *Process {
env := make([]string, len(environment) + 1)
for k, v := range environment {
env[k] = v
}
env[len(env) - 1] = fmt.Sprintf("%s=%s", gPROC_TEMP_DIR_ENV_KEY, os.TempDir())
p := &Process {
Manager : nil,
PPid : os.Getpid(),
CommEnabled : true,
Cmd : exec.Cmd {
Args : []string{path},
Path : path,
Stdin : os.Stdin,
Stdout : os.Stdout,
Stderr : os.Stderr,
Env : env,
ExtraFiles : make([]*os.File, 0),
},
}
// 当前工作目录
if d, err := os.Getwd(); err == nil {
p.Dir = d
}
if len(args) > 0 {
start := 0
if strings.EqualFold(path, args[0]) {
start = 1
}
p.Args = append(p.Args, args[start : ]...)
}
return p
}
// 开始执行(非阻塞)
@ -25,9 +62,12 @@ func (p *Process) Start() (int, error) {
if p.Process != nil {
return p.Pid(), nil
}
if p.PPid > 0 {
p.Env = append(p.Env, fmt.Sprintf("%s=%d", gPROC_ENV_KEY_PPID_KEY, p.PPid))
commEnabled := 0
if p.CommEnabled {
commEnabled = 1
}
p.Env = append(p.Env, fmt.Sprintf("%s=%d", gPROC_ENV_KEY_PPID_KEY, p.PPid))
p.Env = append(p.Env, fmt.Sprintf("%s=%d", gPROC_ENV_KEY_COMM_KEY, commEnabled))
if err := p.Cmd.Start(); err == nil {
if p.Manager != nil {
p.Manager.processes.Set(p.Process.Pid, p)

View File

@ -15,6 +15,10 @@ import (
"errors"
)
const (
TIME_REAGEX_PATTERN = `(\d{4}-\d{2}-\d{2})[\sT]{0,1}(\d{2}:\d{2}:\d{2}){0,1}\.{0,1}(\d{0,9})([\sZ]{0,1})([\+-]{0,1})([:\d]*)`
)
var (
// 用于time.Time转换使用防止多次Compile
timeRegex *regexp.Regexp
@ -22,7 +26,7 @@ var (
func init() {
// 使用正则判断会比直接使用ParseInLocation挨个轮训判断要快很多
timeRegex, _ = regexp.Compile(`(\d{4}-\d{2}-\d{2})[\sT]{0,1}(\d{2}:\d{2}:\d{2}){0,1}\.{0,1}(\d{0,9})([\sZ]{0,1})([\+-]{0,1})([:\d]*)`)
timeRegex, _ = regexp.Compile(TIME_REAGEX_PATTERN)
}

View File

@ -8,5 +8,4 @@ import (
func main() {
fmt.Println(gconv.Float64(float32(19.66)))
fmt.Println(float64(float32(19.66)))
}
}