diff --git a/os/gfile/gfile_cache.go b/os/gfile/gfile_cache.go index e1354f19f..c4c28d490 100644 --- a/os/gfile/gfile_cache.go +++ b/os/gfile/gfile_cache.go @@ -33,7 +33,7 @@ func GetContentsWithCache(path string, duration ...time.Duration) string { return string(GetBytesWithCache(path, duration...)) } -// GetBinContents returns []byte content of given file by from cache. +// GetBytesWithCache returns []byte content of given file by from cache. // If there's no content in the cache, it will read it from disk file specified by . // The parameter specifies the caching time for this file content in seconds. func GetBytesWithCache(path string, duration ...time.Duration) []byte { diff --git a/os/gfsnotify/gfsnotify_watcher_loop.go b/os/gfsnotify/gfsnotify_watcher_loop.go index 6eb1e1a7d..3c47a7f0a 100644 --- a/os/gfsnotify/gfsnotify_watcher_loop.go +++ b/os/gfsnotify/gfsnotify_watcher_loop.go @@ -12,7 +12,7 @@ import ( "github.com/gogf/gf/internal/intlog" ) -// watchLoop starts the loop for event listening fro underlying inotify monitor. +// watchLoop starts the loop for event listening from underlying inotify monitor. func (w *Watcher) watchLoop() { go func() { for { diff --git a/os/gproc/gproc_comm.go b/os/gproc/gproc_comm.go index 51a40f2bd..4ab499ca6 100644 --- a/os/gproc/gproc_comm.go +++ b/os/gproc/gproc_comm.go @@ -7,10 +7,12 @@ package gproc import ( + "context" "fmt" "github.com/gogf/gf/container/gmap" "github.com/gogf/gf/errors/gcode" "github.com/gogf/gf/errors/gerror" + "github.com/gogf/gf/internal/intlog" "github.com/gogf/gf/net/gtcp" "github.com/gogf/gf/os/gfile" "github.com/gogf/gf/util/gconv" @@ -32,9 +34,10 @@ type MsgResponse struct { } const ( - defaultGroupNameFoProcComm = "" // Default group name. - defaultTcpPortForProcComm = 10000 // Starting port number for receiver listening. - maxLengthForProcMsgQueue = 10000 // Max size for each message queue of the group. + defaultFolderNameForProcComm = "gf_pid_port_mapping" // Default folder name for storing pid to port mapping files. + defaultGroupNameForProcComm = "" // Default group name. + defaultTcpPortForProcComm = 10000 // Starting port number for receiver listening. + maxLengthForProcMsgQueue = 10000 // Max size for each message queue of the group. ) var ( @@ -43,17 +46,36 @@ var ( commReceiveQueues = gmap.NewStrAnyMap(true) // commPidFolderPath specifies the folder path storing pid to port mapping files. - commPidFolderPath = gfile.TempDir("gproc") + commPidFolderPath string ) func init() { - // Automatically create the storage folder. - if !gfile.Exists(commPidFolderPath) { - err := gfile.Mkdir(commPidFolderPath) - if err != nil { - panic(fmt.Errorf(`create gproc folder failed: %v`, err)) + availablePaths := []string{ + "/var/tmp", + "/var/run", + } + if homePath, _ := gfile.Home(); homePath != "" { + availablePaths = append(availablePaths, gfile.Join(homePath, ".config")) + } + availablePaths = append(availablePaths, gfile.TempDir()) + for _, availablePath := range availablePaths { + checkPath := gfile.Join(availablePath, defaultFolderNameForProcComm) + if !gfile.Exists(checkPath) && gfile.Mkdir(checkPath) != nil { + continue + } + if gfile.IsWritable(checkPath) { + commPidFolderPath = checkPath + break } } + if commPidFolderPath == "" { + intlog.Errorf( + context.TODO(), + `cannot find available folder for storing pid to port mapping files in paths: %+v, process communication feature will fail`, + availablePaths, + ) + } + } // getConnByPid creates and returns a TCP connection for specified pid. @@ -72,9 +94,7 @@ func getConnByPid(pid int) (*gtcp.PoolConn, error) { // getPortByPid returns the listening port for specified pid. // It returns 0 if no port found for the specified pid. func getPortByPid(pid int) int { - path := getCommFilePath(pid) - content := gfile.GetContentsWithCache(path) - return gconv.Int(content) + return gconv.Int(gfile.GetContentsWithCache(getCommFilePath(pid))) } // getCommFilePath returns the pid to port mapping file path for given pid. diff --git a/os/gproc/gproc_comm_receive.go b/os/gproc/gproc_comm_receive.go index df4f52412..f358db822 100644 --- a/os/gproc/gproc_comm_receive.go +++ b/os/gproc/gproc_comm_receive.go @@ -35,7 +35,7 @@ func Receive(group ...string) *MsgRequest { if len(group) > 0 { groupName = group[0] } else { - groupName = defaultGroupNameFoProcComm + groupName = defaultGroupNameForProcComm } queue := commReceiveQueues.GetOrSetFuncLock(groupName, func() interface{} { return gqueue.New(maxLengthForProcMsgQueue) @@ -79,8 +79,10 @@ func receiveTcpListening() { // receiveTcpHandler is the connection handler for receiving data. func receiveTcpHandler(conn *gtcp.Conn) { - var result []byte - var response MsgResponse + var ( + result []byte + response MsgResponse + ) for { response.Code = 0 response.Message = "" diff --git a/os/gproc/gproc_comm_send.go b/os/gproc/gproc_comm_send.go index 5a06cef62..9e05d5437 100644 --- a/os/gproc/gproc_comm_send.go +++ b/os/gproc/gproc_comm_send.go @@ -18,7 +18,7 @@ func Send(pid int, data []byte, group ...string) error { msg := MsgRequest{ SendPid: Pid(), RecvPid: pid, - Group: defaultGroupNameFoProcComm, + Group: defaultGroupNameForProcComm, Data: data, } if len(group) > 0 { diff --git a/version.go b/version.go index f5adfbbf9..e2d7525e5 100644 --- a/version.go +++ b/version.go @@ -1,4 +1,4 @@ package gf -const VERSION = "v1.17.0" +const VERSION = "v1.16.6" const AUTHORS = "john"