diff options
author | Daniel Baumann <daniel@debian.org> | 2024-10-18 20:33:49 +0200 |
---|---|---|
committer | Daniel Baumann <daniel@debian.org> | 2024-12-12 23:57:56 +0100 |
commit | e68b9d00a6e05b3a941f63ffb696f91e554ac5ec (patch) | |
tree | 97775d6c13b0f416af55314eb6a89ef792474615 /modules/process/context.go | |
parent | Initial commit. (diff) | |
download | forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.tar.xz forgejo-e68b9d00a6e05b3a941f63ffb696f91e554ac5ec.zip |
Adding upstream version 9.0.3.
Signed-off-by: Daniel Baumann <daniel@debian.org>
Diffstat (limited to '')
-rw-r--r-- | modules/process/context.go | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/modules/process/context.go b/modules/process/context.go new file mode 100644 index 0000000..26a80eb --- /dev/null +++ b/modules/process/context.go @@ -0,0 +1,68 @@ +// Copyright 2021 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package process + +import ( + "context" +) + +// Context is a wrapper around context.Context and contains the current pid for this context +type Context struct { + context.Context + pid IDType +} + +// GetPID returns the PID for this context +func (c *Context) GetPID() IDType { + return c.pid +} + +// GetParent returns the parent process context (if any) +func (c *Context) GetParent() *Context { + return GetContext(c.Context) +} + +// 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 +func (c *Context) Value(key any) any { + if key == ProcessContextKey { + return c + } + return c.Context.Value(key) +} + +// ProcessContextKey is the key under which process contexts are stored +var ProcessContextKey any = "process-context" + +// GetContext will return a process context if one exists +func GetContext(ctx context.Context) *Context { + if pCtx, ok := ctx.(*Context); ok { + return pCtx + } + pCtxInterface := ctx.Value(ProcessContextKey) + if pCtxInterface == nil { + return nil + } + if pCtx, ok := pCtxInterface.(*Context); ok { + return pCtx + } + return nil +} + +// GetPID returns the PID for this context +func GetPID(ctx context.Context) IDType { + pCtx := GetContext(ctx) + if pCtx == nil { + return "" + } + return pCtx.GetPID() +} + +// GetParentPID returns the ParentPID for this context +func GetParentPID(ctx context.Context) IDType { + var parentPID IDType + if parentProcess := GetContext(ctx); parentProcess != nil { + parentPID = parentProcess.GetPID() + } + return parentPID +} |