github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/process/manager.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 // Copyright 2014 The Gogs Authors. All rights reserved. 7 8 package process 9 10 import ( 11 "context" 12 "runtime/pprof" 13 "strconv" 14 "sync" 15 "time" 16 ) 17 18 // TODO: This packages still uses a singleton for the Manager. 19 // Once there's a decent web framework and dependencies are passed around like they should, 20 // then we delete the singleton. 21 22 var ( 23 manager *Manager 24 managerInit sync.Once 25 26 // DefaultContext is the default context to run processing commands in 27 DefaultContext = context.Background() 28 ) 29 30 // DescriptionPProfLabel is a label set on goroutines that have a process attached 31 const DescriptionPProfLabel = "process-description" 32 33 // PIDPProfLabel is a label set on goroutines that have a process attached 34 const PIDPProfLabel = "pid" 35 36 // PPIDPProfLabel is a label set on goroutines that have a process attached 37 const PPIDPProfLabel = "ppid" 38 39 // ProcessTypePProfLabel is a label set on goroutines that have a process attached 40 const ProcessTypePProfLabel = "process-type" 41 42 // IDType is a pid type 43 type IDType string 44 45 // FinishedFunc is a function that marks that the process is finished and can be removed from the process table 46 // - it is simply an alias for context.CancelFunc and is only for documentary purposes 47 type FinishedFunc = context.CancelFunc 48 49 // Manager manages all processes and counts PIDs. 50 type Manager struct { 51 mutex sync.Mutex 52 53 next int64 54 lastTime int64 55 56 processMap map[IDType]*process 57 } 58 59 // GetManager returns a Manager and initializes one as singleton if there's none yet 60 func GetManager() *Manager { 61 managerInit.Do(func() { 62 manager = &Manager{ 63 processMap: make(map[IDType]*process), 64 next: 1, 65 } 66 }) 67 return manager 68 } 69 70 // AddContext creates a new context and adds it as a process. Once the process is finished, finished must be called 71 // to remove the process from the process table. It should not be called until the process is finished but must always be called. 72 // 73 // cancel should be used to cancel the returned context, however it will not remove the process from the process table. 74 // finished will cancel the returned context and remove it from the process table. 75 // 76 // Most processes will not need to use the cancel function but there will be cases whereby you want to cancel the process but not immediately remove it from the 77 // process table. 78 func (pm *Manager) AddContext(parent context.Context, description string) (ctx context.Context, cancel context.CancelFunc, finished FinishedFunc) { 79 ctx, cancel = context.WithCancel(parent) 80 81 ctx, _, finished = pm.Add(ctx, description, cancel, NormalProcessType, true) 82 83 return ctx, cancel, finished 84 } 85 86 // AddTypedContext creates a new context and adds it as a process. Once the process is finished, finished must be called 87 // to remove the process from the process table. It should not be called until the process is finished but must always be called. 88 // 89 // cancel should be used to cancel the returned context, however it will not remove the process from the process table. 90 // finished will cancel the returned context and remove it from the process table. 91 // 92 // Most processes will not need to use the cancel function but there will be cases whereby you want to cancel the process but not immediately remove it from the 93 // process table. 94 func (pm *Manager) AddTypedContext(parent context.Context, description, processType string, currentlyRunning bool) (ctx context.Context, cancel context.CancelFunc, finished FinishedFunc) { 95 ctx, cancel = context.WithCancel(parent) 96 97 ctx, _, finished = pm.Add(ctx, description, cancel, processType, currentlyRunning) 98 99 return ctx, cancel, finished 100 } 101 102 // AddContextTimeout creates a new context and add it as a process. Once the process is finished, finished must be called 103 // to remove the process from the process table. It should not be called until the process is finished but must always be called. 104 // 105 // cancel should be used to cancel the returned context, however it will not remove the process from the process table. 106 // finished will cancel the returned context and remove it from the process table. 107 // 108 // Most processes will not need to use the cancel function but there will be cases whereby you want to cancel the process but not immediately remove it from the 109 // process table. 110 func (pm *Manager) AddContextTimeout(parent context.Context, timeout time.Duration, description string) (ctx context.Context, cancel context.CancelFunc, finshed FinishedFunc) { 111 if timeout <= 0 { 112 // it's meaningless to use timeout <= 0, and it must be a bug! so we must panic here to tell developers to make the timeout correct 113 panic("the timeout must be greater than zero, otherwise the context will be cancelled immediately") 114 } 115 116 ctx, cancel = context.WithTimeout(parent, timeout) 117 118 ctx, _, finshed = pm.Add(ctx, description, cancel, NormalProcessType, true) 119 120 return ctx, cancel, finshed 121 } 122 123 // Add create a new process 124 func (pm *Manager) Add(ctx context.Context, description string, cancel context.CancelFunc, processType string, currentlyRunning bool) (context.Context, IDType, FinishedFunc) { 125 parentPID := GetParentPID(ctx) 126 127 pm.mutex.Lock() 128 start, pid := pm.nextPID() 129 130 parent := pm.processMap[parentPID] 131 if parent == nil { 132 parentPID = "" 133 } 134 135 process := &process{ 136 PID: pid, 137 ParentPID: parentPID, 138 Description: description, 139 Start: start, 140 Cancel: cancel, 141 Type: processType, 142 } 143 144 var finished FinishedFunc 145 if currentlyRunning { 146 finished = func() { 147 cancel() 148 pm.remove(process) 149 pprof.SetGoroutineLabels(ctx) 150 } 151 } else { 152 finished = func() { 153 cancel() 154 pm.remove(process) 155 } 156 } 157 158 pm.processMap[pid] = process 159 pm.mutex.Unlock() 160 161 pprofCtx := pprof.WithLabels(ctx, pprof.Labels(DescriptionPProfLabel, description, PPIDPProfLabel, string(parentPID), PIDPProfLabel, string(pid), ProcessTypePProfLabel, processType)) 162 if currentlyRunning { 163 pprof.SetGoroutineLabels(pprofCtx) 164 } 165 166 return &Context{ 167 Context: pprofCtx, 168 pid: pid, 169 }, pid, finished 170 } 171 172 // nextPID will return the next available PID. pm.mutex should already be locked. 173 func (pm *Manager) nextPID() (start time.Time, pid IDType) { 174 start = time.Now() 175 startUnix := start.Unix() 176 if pm.lastTime == startUnix { 177 pm.next++ 178 } else { 179 pm.next = 1 180 } 181 pm.lastTime = startUnix 182 pid = IDType(strconv.FormatInt(start.Unix(), 16)) 183 184 if pm.next == 1 { 185 return 186 } 187 pid = IDType(string(pid) + "-" + strconv.FormatInt(pm.next, 10)) 188 return 189 } 190 191 // Remove a process from the ProcessManager. 192 func (pm *Manager) Remove(pid IDType) { 193 pm.mutex.Lock() 194 delete(pm.processMap, pid) 195 pm.mutex.Unlock() 196 } 197 198 func (pm *Manager) remove(process *process) { 199 pm.mutex.Lock() 200 defer pm.mutex.Unlock() 201 if p := pm.processMap[process.PID]; p == process { 202 delete(pm.processMap, process.PID) 203 } 204 } 205 206 // Cancel a process in the ProcessManager. 207 func (pm *Manager) Cancel(pid IDType) { 208 pm.mutex.Lock() 209 process, ok := pm.processMap[pid] 210 pm.mutex.Unlock() 211 if ok && process.Type != SystemProcessType { 212 process.Cancel() 213 } 214 }