Skip to content

Commit

Permalink
chore: improve some variable names
Browse files Browse the repository at this point in the history
Existing names were really similar to imported package names or other
variables.
  • Loading branch information
ryepup committed Dec 30, 2024
1 parent b56f47f commit fe5c421
Showing 1 changed file with 19 additions and 20 deletions.
39 changes: 19 additions & 20 deletions internal/watcher/instance/nginx_process_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,21 +66,20 @@ func (npp *NginxProcessParser) Parse(ctx context.Context, processes []*nginxproc
instanceMap := make(map[string]*mpi.Instance) // key is instanceID
workers := make(map[int32][]*mpi.InstanceChild) // key is ppid of process

nginxProcesses := convertToMap(processes)
processesByPID := convertToMap(processes)

for _, nginxProcess := range nginxProcesses {
// Here we are determining if the nginxProcess is a worker process
if nginxProcess.IsWorker() {
for _, proc := range processesByPID {
if proc.IsWorker() {
// Here we are determining if the worker process has a master
if masterProcess, ok := nginxProcesses[nginxProcess.PPID]; ok {
if masterProcess, ok := processesByPID[proc.PPID]; ok {
workers[masterProcess.PID] = append(workers[masterProcess.PID],
&mpi.InstanceChild{ProcessId: nginxProcess.PID})
&mpi.InstanceChild{ProcessId: proc.PID})

continue
}
nginxInfo, err := npp.getInfo(ctx, nginxProcess)
nginxInfo, err := npp.getInfo(ctx, proc)
if err != nil {
slog.DebugContext(ctx, "Unable to get NGINX info", "pid", nginxProcess.PID, "error", err)
slog.DebugContext(ctx, "Unable to get NGINX info", "pid", proc.PID, "error", err)

continue
}
Expand All @@ -91,23 +90,23 @@ func (npp *NginxProcessParser) Parse(ctx context.Context, processes []*nginxproc

if foundInstance, ok := instanceMap[instance.GetInstanceMeta().GetInstanceId()]; ok {
foundInstance.GetInstanceRuntime().InstanceChildren = append(foundInstance.GetInstanceRuntime().
GetInstanceChildren(), &mpi.InstanceChild{ProcessId: nginxProcess.PID})
GetInstanceChildren(), &mpi.InstanceChild{ProcessId: proc.PID})

continue
}

instance.GetInstanceRuntime().InstanceChildren = append(instance.GetInstanceRuntime().
GetInstanceChildren(), &mpi.InstanceChild{ProcessId: nginxProcess.PID})
GetInstanceChildren(), &mpi.InstanceChild{ProcessId: proc.PID})

instanceMap[instance.GetInstanceMeta().GetInstanceId()] = instance

continue
}

// Here we are determining if the nginxProcess is a master process
nginxInfo, err := npp.getInfo(ctx, nginxProcess)
// proc is a master process
nginxInfo, err := npp.getInfo(ctx, proc)
if err != nil {
slog.DebugContext(ctx, "Unable to get NGINX info", "pid", nginxProcess.PID, "error", err)
slog.DebugContext(ctx, "Unable to get NGINX info", "pid", proc.PID, "error", err)

continue
}
Expand All @@ -125,13 +124,13 @@ func (npp *NginxProcessParser) Parse(ctx context.Context, processes []*nginxproc
return instanceMap
}

func (npp *NginxProcessParser) getInfo(ctx context.Context, nginxProcess *nginxprocess.Process) (*Info, error) {
exePath := nginxProcess.Exe
func (npp *NginxProcessParser) getInfo(ctx context.Context, proc *nginxprocess.Process) (*Info, error) {
exePath := proc.Exe

if exePath == "" {
exePath = npp.getExe(ctx)
if exePath == "" {
return nil, fmt.Errorf("unable to find NGINX exe for process %d", nginxProcess.PID)
return nil, fmt.Errorf("unable to find NGINX exe for process %d", proc.PID)
}
}

Expand All @@ -145,7 +144,7 @@ func (npp *NginxProcessParser) getInfo(ctx context.Context, nginxProcess *nginxp
nginxInfo = parseNginxVersionCommandOutput(ctx, outputBuffer)

nginxInfo.ExePath = exePath
nginxInfo.ProcessID = nginxProcess.PID
nginxInfo.ProcessID = proc.PID

loadableModules := getLoadableModules(nginxInfo)
nginxInfo.LoadableModules = loadableModules
Expand Down Expand Up @@ -383,11 +382,11 @@ func readDirectory(dir, extension string) (files []string, err error) {
}

func convertToMap(processes []*nginxprocess.Process) map[int32]*nginxprocess.Process {
nginxProcesses := make(map[int32]*nginxprocess.Process)
processesByPID := make(map[int32]*nginxprocess.Process)

for _, p := range processes {
nginxProcesses[p.PID] = p
processesByPID[p.PID] = p
}

return nginxProcesses
return processesByPID
}

0 comments on commit fe5c421

Please sign in to comment.