code.gitea.io/gitea@v1.19.3/modules/process/context.go (about) 1 // Copyright 2021 The Gitea Authors. All rights reserved. 2 // SPDX-License-Identifier: MIT 3 4 package process 5 6 import ( 7 "context" 8 ) 9 10 // Context is a wrapper around context.Context and contains the current pid for this context 11 type Context struct { 12 context.Context 13 pid IDType 14 } 15 16 // GetPID returns the PID for this context 17 func (c *Context) GetPID() IDType { 18 return c.pid 19 } 20 21 // GetParent returns the parent process context (if any) 22 func (c *Context) GetParent() *Context { 23 return GetContext(c.Context) 24 } 25 26 // Value is part of the interface for context.Context. We mostly defer to the internal context - but we return this in response to the ProcessContextKey 27 func (c *Context) Value(key interface{}) interface{} { 28 if key == ProcessContextKey { 29 return c 30 } 31 return c.Context.Value(key) 32 } 33 34 // ProcessContextKey is the key under which process contexts are stored 35 var ProcessContextKey interface{} = "process-context" 36 37 // GetContext will return a process context if one exists 38 func GetContext(ctx context.Context) *Context { 39 if pCtx, ok := ctx.(*Context); ok { 40 return pCtx 41 } 42 pCtxInterface := ctx.Value(ProcessContextKey) 43 if pCtxInterface == nil { 44 return nil 45 } 46 if pCtx, ok := pCtxInterface.(*Context); ok { 47 return pCtx 48 } 49 return nil 50 } 51 52 // GetPID returns the PID for this context 53 func GetPID(ctx context.Context) IDType { 54 pCtx := GetContext(ctx) 55 if pCtx == nil { 56 return "" 57 } 58 return pCtx.GetPID() 59 } 60 61 // GetParentPID returns the ParentPID for this context 62 func GetParentPID(ctx context.Context) IDType { 63 var parentPID IDType 64 if parentProcess := GetContext(ctx); parentProcess != nil { 65 parentPID = parentProcess.GetPID() 66 } 67 return parentPID 68 }