gproc_comm.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. package gproc
  7. import (
  8. "context"
  9. "fmt"
  10. "github.com/gogf/gf/container/gmap"
  11. "github.com/gogf/gf/errors/gcode"
  12. "github.com/gogf/gf/errors/gerror"
  13. "github.com/gogf/gf/internal/intlog"
  14. "github.com/gogf/gf/net/gtcp"
  15. "github.com/gogf/gf/os/gfile"
  16. "github.com/gogf/gf/util/gconv"
  17. )
  18. // MsgRequest is the request structure for process communication.
  19. type MsgRequest struct {
  20. SendPid int // Sender PID.
  21. RecvPid int // Receiver PID.
  22. Group string // Message group name.
  23. Data []byte // Request data.
  24. }
  25. // MsgResponse is the response structure for process communication.
  26. type MsgResponse struct {
  27. Code int // 1: OK; Other: Error.
  28. Message string // Response message.
  29. Data []byte // Response data.
  30. }
  31. const (
  32. defaultFolderNameForProcComm = "gf_pid_port_mapping" // Default folder name for storing pid to port mapping files.
  33. defaultGroupNameForProcComm = "" // Default group name.
  34. defaultTcpPortForProcComm = 10000 // Starting port number for receiver listening.
  35. maxLengthForProcMsgQueue = 10000 // Max size for each message queue of the group.
  36. )
  37. var (
  38. // commReceiveQueues is the group name to queue map for storing received data.
  39. // The value of the map is type of *gqueue.Queue.
  40. commReceiveQueues = gmap.NewStrAnyMap(true)
  41. // commPidFolderPath specifies the folder path storing pid to port mapping files.
  42. commPidFolderPath string
  43. )
  44. func init() {
  45. availablePaths := []string{
  46. "/var/tmp",
  47. "/var/run",
  48. }
  49. if homePath, _ := gfile.Home(); homePath != "" {
  50. availablePaths = append(availablePaths, gfile.Join(homePath, ".config"))
  51. }
  52. availablePaths = append(availablePaths, gfile.TempDir())
  53. for _, availablePath := range availablePaths {
  54. checkPath := gfile.Join(availablePath, defaultFolderNameForProcComm)
  55. if !gfile.Exists(checkPath) && gfile.Mkdir(checkPath) != nil {
  56. continue
  57. }
  58. if gfile.IsWritable(checkPath) {
  59. commPidFolderPath = checkPath
  60. break
  61. }
  62. }
  63. if commPidFolderPath == "" {
  64. intlog.Errorf(
  65. context.TODO(),
  66. `cannot find available folder for storing pid to port mapping files in paths: %+v, process communication feature will fail`,
  67. availablePaths,
  68. )
  69. }
  70. }
  71. // getConnByPid creates and returns a TCP connection for specified pid.
  72. func getConnByPid(pid int) (*gtcp.PoolConn, error) {
  73. port := getPortByPid(pid)
  74. if port > 0 {
  75. if conn, err := gtcp.NewPoolConn(fmt.Sprintf("127.0.0.1:%d", port)); err == nil {
  76. return conn, nil
  77. } else {
  78. return nil, err
  79. }
  80. }
  81. return nil, gerror.NewCodef(gcode.CodeOperationFailed, "could not find port for pid: %d", pid)
  82. }
  83. // getPortByPid returns the listening port for specified pid.
  84. // It returns 0 if no port found for the specified pid.
  85. func getPortByPid(pid int) int {
  86. return gconv.Int(gfile.GetContentsWithCache(getCommFilePath(pid)))
  87. }
  88. // getCommFilePath returns the pid to port mapping file path for given pid.
  89. func getCommFilePath(pid int) string {
  90. return gfile.Join(commPidFolderPath, gconv.String(pid))
  91. }