Skip to content

Commit

Permalink
Merge branch 'main' into opengraph-title
Browse files Browse the repository at this point in the history
  • Loading branch information
lafriks authored Mar 25, 2022
2 parents ac2e99c + 5fe764b commit bb1fcd9
Show file tree
Hide file tree
Showing 27 changed files with 57 additions and 92 deletions.
50 changes: 16 additions & 34 deletions modules/graceful/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package graceful

import (
"context"
"runtime/pprof"
"sync"
"time"

Expand Down Expand Up @@ -62,15 +63,13 @@ type WithCallback func(callback func())
// Similarly the callback function provided to atTerminate must return once termination is complete.
// Please note that use of the atShutdown and atTerminate callbacks will create go-routines that will wait till their respective signals
// - users must therefore be careful to only call these as necessary.
// If run is not expected to run indefinitely RunWithShutdownChan is likely to be more appropriate.
type RunnableWithShutdownFns func(atShutdown, atTerminate func(func()))

// RunWithShutdownFns takes a function that has both atShutdown and atTerminate callbacks
// After the callback to atShutdown is called and is complete, the main function must return.
// Similarly the callback function provided to atTerminate must return once termination is complete.
// Please note that use of the atShutdown and atTerminate callbacks will create go-routines that will wait till their respective signals
// - users must therefore be careful to only call these as necessary.
// If run is not expected to run indefinitely RunWithShutdownChan is likely to be more appropriate.
func (g *Manager) RunWithShutdownFns(run RunnableWithShutdownFns) {
g.runningServerWaitGroup.Add(1)
defer g.runningServerWaitGroup.Done()
Expand Down Expand Up @@ -98,32 +97,6 @@ func (g *Manager) RunWithShutdownFns(run RunnableWithShutdownFns) {
})
}

// RunnableWithShutdownChan is a runnable with functions to run at shutdown and terminate.
// After the atShutdown channel is closed, the main function must return once shutdown is complete.
// (Optionally IsHammer may be waited for instead however, this should be avoided if possible.)
// The callback function provided to atTerminate must return once termination is complete.
// Please note that use of the atTerminate function will create a go-routine that will wait till terminate - users must therefore be careful to only call this as necessary.
type RunnableWithShutdownChan func(atShutdown <-chan struct{}, atTerminate WithCallback)

// RunWithShutdownChan takes a function that has channel to watch for shutdown and atTerminate callbacks
// After the atShutdown channel is closed, the main function must return once shutdown is complete.
// (Optionally IsHammer may be waited for instead however, this should be avoided if possible.)
// The callback function provided to atTerminate must return once termination is complete.
// Please note that use of the atTerminate function will create a go-routine that will wait till terminate - users must therefore be careful to only call this as necessary.
func (g *Manager) RunWithShutdownChan(run RunnableWithShutdownChan) {
g.runningServerWaitGroup.Add(1)
defer g.runningServerWaitGroup.Done()
defer func() {
if err := recover(); err != nil {
log.Critical("PANIC during RunWithShutdownChan: %v\nStacktrace: %s", err, log.Stack(2))
g.doShutdown()
}
}()
run(g.IsShutdown(), func(atTerminate func()) {
g.RunAtTerminate(atTerminate)
})
}

// RunWithShutdownContext takes a function that has a context to watch for shutdown.
// After the provided context is Done(), the main function must return once shutdown is complete.
// (Optionally the HammerContext may be obtained and waited for however, this should be avoided if possible.)
Expand All @@ -136,7 +109,9 @@ func (g *Manager) RunWithShutdownContext(run func(context.Context)) {
g.doShutdown()
}
}()
run(g.ShutdownContext())
ctx := g.ShutdownContext()
pprof.SetGoroutineLabels(ctx) // We don't have a label to restore back to but I think this is fine
run(ctx)
}

// RunAtTerminate adds to the terminate wait group and creates a go-routine to run the provided function at termination
Expand Down Expand Up @@ -198,6 +173,8 @@ func (g *Manager) doShutdown() {
}
g.lock.Lock()
g.shutdownCtxCancel()
atShutdownCtx := pprof.WithLabels(g.hammerCtx, pprof.Labels("graceful-lifecycle", "post-shutdown"))
pprof.SetGoroutineLabels(atShutdownCtx)
for _, fn := range g.toRunAtShutdown {
go fn()
}
Expand All @@ -214,7 +191,7 @@ func (g *Manager) doShutdown() {
g.doTerminate()
g.WaitForTerminate()
g.lock.Lock()
g.doneCtxCancel()
g.managerCtxCancel()
g.lock.Unlock()
}()
}
Expand All @@ -227,6 +204,8 @@ func (g *Manager) doHammerTime(d time.Duration) {
default:
log.Warn("Setting Hammer condition")
g.hammerCtxCancel()
atHammerCtx := pprof.WithLabels(g.terminateCtx, pprof.Labels("graceful-lifecycle", "post-hammer"))
pprof.SetGoroutineLabels(atHammerCtx)
for _, fn := range g.toRunAtHammer {
go fn()
}
Expand All @@ -244,6 +223,9 @@ func (g *Manager) doTerminate() {
default:
log.Warn("Terminating")
g.terminateCtxCancel()
atTerminateCtx := pprof.WithLabels(g.managerCtx, pprof.Labels("graceful-lifecycle", "post-terminate"))
pprof.SetGoroutineLabels(atTerminateCtx)

for _, fn := range g.toRunAtTerminate {
go fn()
}
Expand Down Expand Up @@ -331,20 +313,20 @@ func (g *Manager) InformCleanup() {

// Done allows the manager to be viewed as a context.Context, it returns a channel that is closed when the server is finished terminating
func (g *Manager) Done() <-chan struct{} {
return g.doneCtx.Done()
return g.managerCtx.Done()
}

// Err allows the manager to be viewed as a context.Context done at Terminate
func (g *Manager) Err() error {
return g.doneCtx.Err()
return g.managerCtx.Err()
}

// Value allows the manager to be viewed as a context.Context done at Terminate
func (g *Manager) Value(key interface{}) interface{} {
return g.doneCtx.Value(key)
return g.managerCtx.Value(key)
}

// Deadline returns nil as there is no fixed Deadline for the manager, it allows the manager to be viewed as a context.Context
func (g *Manager) Deadline() (deadline time.Time, ok bool) {
return g.doneCtx.Deadline()
return g.managerCtx.Deadline()
}
17 changes: 14 additions & 3 deletions modules/graceful/manager_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"errors"
"os"
"os/signal"
"runtime/pprof"
"sync"
"syscall"
"time"
Expand All @@ -29,11 +30,11 @@ type Manager struct {
shutdownCtx context.Context
hammerCtx context.Context
terminateCtx context.Context
doneCtx context.Context
managerCtx context.Context
shutdownCtxCancel context.CancelFunc
hammerCtxCancel context.CancelFunc
terminateCtxCancel context.CancelFunc
doneCtxCancel context.CancelFunc
managerCtxCancel context.CancelFunc
runningServerWaitGroup sync.WaitGroup
createServerWaitGroup sync.WaitGroup
terminateWaitGroup sync.WaitGroup
Expand All @@ -58,7 +59,17 @@ func (g *Manager) start(ctx context.Context) {
g.terminateCtx, g.terminateCtxCancel = context.WithCancel(ctx)
g.shutdownCtx, g.shutdownCtxCancel = context.WithCancel(ctx)
g.hammerCtx, g.hammerCtxCancel = context.WithCancel(ctx)
g.doneCtx, g.doneCtxCancel = context.WithCancel(ctx)
g.managerCtx, g.managerCtxCancel = context.WithCancel(ctx)

// Next add pprof labels to these contexts
g.terminateCtx = pprof.WithLabels(g.terminateCtx, pprof.Labels("graceful-lifecycle", "with-terminate"))
g.shutdownCtx = pprof.WithLabels(g.shutdownCtx, pprof.Labels("graceful-lifecycle", "with-shutdown"))
g.hammerCtx = pprof.WithLabels(g.hammerCtx, pprof.Labels("graceful-lifecycle", "with-hammer"))
g.managerCtx = pprof.WithLabels(g.managerCtx, pprof.Labels("graceful-lifecycle", "with-manager"))

// Now label this and all goroutines created by this goroutine with the graceful-lifecycle manager
pprof.SetGoroutineLabels(g.managerCtx)
defer pprof.SetGoroutineLabels(ctx)

// Set the running state & handle signals
g.setState(stateRunning)
Expand Down
17 changes: 14 additions & 3 deletions modules/graceful/manager_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ package graceful
import (
"context"
"os"
"runtime/pprof"
"strconv"
"sync"
"time"
Expand Down Expand Up @@ -40,11 +41,11 @@ type Manager struct {
shutdownCtx context.Context
hammerCtx context.Context
terminateCtx context.Context
doneCtx context.Context
managerCtx context.Context
shutdownCtxCancel context.CancelFunc
hammerCtxCancel context.CancelFunc
terminateCtxCancel context.CancelFunc
doneCtxCancel context.CancelFunc
managerCtxCancel context.CancelFunc
runningServerWaitGroup sync.WaitGroup
createServerWaitGroup sync.WaitGroup
terminateWaitGroup sync.WaitGroup
Expand All @@ -71,7 +72,17 @@ func (g *Manager) start() {
g.terminateCtx, g.terminateCtxCancel = context.WithCancel(g.ctx)
g.shutdownCtx, g.shutdownCtxCancel = context.WithCancel(g.ctx)
g.hammerCtx, g.hammerCtxCancel = context.WithCancel(g.ctx)
g.doneCtx, g.doneCtxCancel = context.WithCancel(g.ctx)
g.managerCtx, g.managerCtxCancel = context.WithCancel(g.ctx)

// Next add pprof labels to these contexts
g.terminateCtx = pprof.WithLabels(g.terminateCtx, pprof.Labels("graceful-lifecycle", "with-terminate"))
g.shutdownCtx = pprof.WithLabels(g.shutdownCtx, pprof.Labels("graceful-lifecycle", "with-shutdown"))
g.hammerCtx = pprof.WithLabels(g.hammerCtx, pprof.Labels("graceful-lifecycle", "with-hammer"))
g.managerCtx = pprof.WithLabels(g.managerCtx, pprof.Labels("graceful-lifecycle", "with-manager"))

// Now label this and all goroutines created by this goroutine with the graceful-lifecycle manager
pprof.SetGoroutineLabels(g.managerCtx)
defer pprof.SetGoroutineLabels(g.ctx)

// Make channels
g.shutdownRequested = make(chan struct{})
Expand Down
19 changes: 11 additions & 8 deletions modules/process/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"io"
"os/exec"
"runtime/pprof"
"sort"
"strconv"
"sync"
Expand Down Expand Up @@ -66,11 +67,9 @@ func GetManager() *Manager {
// 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
// process table.
func (pm *Manager) AddContext(parent context.Context, description string) (ctx context.Context, cancel context.CancelFunc, finished FinishedFunc) {
parentPID := GetParentPID(parent)

ctx, cancel = context.WithCancel(parent)

pid, finished := pm.Add(parentPID, description, cancel)
ctx, pid, finished := pm.Add(ctx, description, cancel)

return &Context{
Context: ctx,
Expand All @@ -87,11 +86,9 @@ func (pm *Manager) AddContext(parent context.Context, description string) (ctx c
// 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
// process table.
func (pm *Manager) AddContextTimeout(parent context.Context, timeout time.Duration, description string) (ctx context.Context, cancel context.CancelFunc, finshed FinishedFunc) {
parentPID := GetParentPID(parent)

ctx, cancel = context.WithTimeout(parent, timeout)

pid, finshed := pm.Add(parentPID, description, cancel)
ctx, pid, finshed := pm.Add(ctx, description, cancel)

return &Context{
Context: ctx,
Expand All @@ -100,7 +97,9 @@ func (pm *Manager) AddContextTimeout(parent context.Context, timeout time.Durati
}

// Add create a new process
func (pm *Manager) Add(parentPID IDType, description string, cancel context.CancelFunc) (IDType, FinishedFunc) {
func (pm *Manager) Add(ctx context.Context, description string, cancel context.CancelFunc) (context.Context, IDType, FinishedFunc) {
parentPID := GetParentPID(ctx)

pm.mutex.Lock()
start, pid := pm.nextPID()

Expand All @@ -120,6 +119,7 @@ func (pm *Manager) Add(parentPID IDType, description string, cancel context.Canc
finished := func() {
cancel()
pm.remove(process)
pprof.SetGoroutineLabels(ctx)
}

if parent != nil {
Expand All @@ -128,7 +128,10 @@ func (pm *Manager) Add(parentPID IDType, description string, cancel context.Canc
pm.processes[pid] = process
pm.mutex.Unlock()

return pid, finished
pprofCtx := pprof.WithLabels(ctx, pprof.Labels("process-description", description, "ppid", string(parentPID), "pid", string(pid)))
pprof.SetGoroutineLabels(pprofCtx)

return pprofCtx, pid, finished
}

// nextPID will return the next available PID. pm.mutex should already be locked.
Expand Down
2 changes: 0 additions & 2 deletions options/locale/locale_cs-CZ.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1246,8 +1246,6 @@ issues.due_date_remove=odstranil/a termín dokončení %s %s
issues.due_date_overdue=Zpožděné
issues.due_date_invalid=Termín dokončení není platný nebo je mimo rozsah. Použijte prosím formát „rrrr-mm-dd“.
issues.dependency.title=Závislosti
issues.dependency.issue_no_dependencies=Tento úkol momentálně nemá žádné závislosti.
issues.dependency.pr_no_dependencies=Tento požadavek na natažení momentálně nemá žádné závislosti.
issues.dependency.add=Přidat závislost…
issues.dependency.cancel=Zrušit
issues.dependency.remove=Odstranit
Expand Down
2 changes: 0 additions & 2 deletions options/locale/locale_de-DE.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1397,8 +1397,6 @@ issues.due_date_remove=hat %[2]s das Fälligkeitsdatum %[1]s entfernt
issues.due_date_overdue=Überfällig
issues.due_date_invalid=Das Fälligkeitsdatum ist ungültig oder außerhalb des zulässigen Bereichs. Bitte verwende das Format „jjjj-mm-tt“.
issues.dependency.title=Abhängigkeiten
issues.dependency.issue_no_dependencies=Dieses Issue hat momentan keine Abhängigkeiten.
issues.dependency.pr_no_dependencies=Dieser Pull-Request hat momentan keine Abhängigkeiten.
issues.dependency.add=Abhängigkeit hinzufügen…
issues.dependency.cancel=Abbrechen
issues.dependency.remove=Entfernen
Expand Down
2 changes: 0 additions & 2 deletions options/locale/locale_el-GR.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1397,8 +1397,6 @@ issues.due_date_remove=αφαίρεσε την ημερομηνία παράδο
issues.due_date_overdue=Εκπρόθεσμο
issues.due_date_invalid=Η ημερομηνία παράδοσης δεν είναι έγκυρη ή εκτός εύρους. Παρακαλούμε χρησιμοποιήστε τη μορφή 'εεεε-μμ-ηη'.
issues.dependency.title=Εξαρτήσεις
issues.dependency.issue_no_dependencies=Αυτό το ζήτημα προς το παρόν δεν έχει καμία εξάρτηση.
issues.dependency.pr_no_dependencies=Αυτό το pull request προς το παρόν δεν έχει εξαρτήσεις.
issues.dependency.add=Προσθήκη εξάρτησης…
issues.dependency.cancel=Ακύρωση
issues.dependency.remove=Διαγραφή
Expand Down
2 changes: 0 additions & 2 deletions options/locale/locale_es-ES.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1394,8 +1394,6 @@ issues.due_date_remove=eliminó la fecha de vencimiento %s %s
issues.due_date_overdue=Vencido
issues.due_date_invalid=La fecha de vencimiento es inválida o está fuera de rango. Por favor utilice el formato 'aaaa-mm-dd'.
issues.dependency.title=Dependencias
issues.dependency.issue_no_dependencies=Esta incidencia actualmente no tiene ninguna dependencia.
issues.dependency.pr_no_dependencies=Este pull request actualmente no tiene ninguna dependencia.
issues.dependency.add=Añadir dependencia…
issues.dependency.cancel=Cancelar
issues.dependency.remove=Eliminar
Expand Down
2 changes: 0 additions & 2 deletions options/locale/locale_fa-IR.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1320,8 +1320,6 @@ issues.due_date_remove=موعد مقرر %s %s حذف شد
issues.due_date_overdue=تاریخ گذشته
issues.due_date_invalid=موعد مقرر نامعتبر است یا خارج از محدوده. لطفاً از قالب 'yyy-mm-dd' استفاده کنید.
issues.dependency.title=وابستگی ها
issues.dependency.issue_no_dependencies=این مسئله در حال حاضر هیچ وابستگی ندارد.
issues.dependency.pr_no_dependencies=این تقاضای واکشی در حال حاضر هیچ وابستگی ندارد.
issues.dependency.add=اضافه کردن وابستگی…
issues.dependency.cancel=انصراف
issues.dependency.remove=حذف/ساقط کردن
Expand Down
1 change: 0 additions & 1 deletion options/locale/locale_fi-FI.ini
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,6 @@ issues.due_date_form_edit=Muokkaa
issues.due_date_form_remove=Poista
issues.due_date_not_set=Määräpäivää ei asetettu.
issues.dependency.title=Riippuvuudet
issues.dependency.pr_no_dependencies=Tällä vetopyynnöllä ei tällä hetkellä ole riippuvuuksia.
issues.dependency.add=Lisää riippuvuus…
issues.dependency.cancel=Peru
issues.dependency.remove=Poista
Expand Down
2 changes: 0 additions & 2 deletions options/locale/locale_fr-FR.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1253,8 +1253,6 @@ issues.due_date_remove=a supprimé l'échéance %s %s
issues.due_date_overdue=En retard
issues.due_date_invalid=La date d’échéance est invalide ou hors plage. Veuillez utiliser le format 'aaaa-mm-dd'.
issues.dependency.title=Dépendances
issues.dependency.issue_no_dependencies=Ce ticket n'a actuellement pas de dépendance.
issues.dependency.pr_no_dependencies=La demande de fusion n'a actuellement pas de dépendance.
issues.dependency.add=Ajouter une dépendance…
issues.dependency.cancel=Annuler
issues.dependency.remove=Supprimer
Expand Down
1 change: 0 additions & 1 deletion options/locale/locale_hu-HU.ini
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,6 @@ issues.due_date_modified=határidő módosítva %s-ről %s %s-re
issues.due_date_remove=%s %s-es határidő eltávolítva
issues.due_date_overdue=Lejárt
issues.dependency.title=Függőségek
issues.dependency.issue_no_dependencies=Ennek a hibajegynek jelenleg nincsenek függőségei.
issues.dependency.add=Függőség hozzáadása…
issues.dependency.cancel=Mégse
issues.dependency.remove=Eltávolítás
Expand Down
2 changes: 0 additions & 2 deletions options/locale/locale_it-IT.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1135,8 +1135,6 @@ issues.due_date_remove=rimossa la data di scadenza %s %s
issues.due_date_overdue=Scaduto
issues.due_date_invalid=La data di scadenza non è valida o fuori intervallo. Si prega di utilizzare il formato 'aaaa-mm-dd'.
issues.dependency.title=Dipendenze
issues.dependency.issue_no_dependencies=Questo problema attualmente non ha alcuna dipendenza.
issues.dependency.pr_no_dependencies=Questo problema attualmente non ha alcuna dipendenza.
issues.dependency.add=Aggiungi dipendenza…
issues.dependency.cancel=Annulla
issues.dependency.remove=Rimuovi
Expand Down
2 changes: 0 additions & 2 deletions options/locale/locale_ja-JP.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1418,8 +1418,6 @@ issues.due_date_remove=が期日 %s を削除 %s
issues.due_date_overdue=期日は過ぎています
issues.due_date_invalid=期日が正しくないか範囲を超えています。 'yyyy-mm-dd' の形式で入力してください。
issues.dependency.title=依存関係
issues.dependency.issue_no_dependencies=このイシューに依存関係はありません。
issues.dependency.pr_no_dependencies=このプルリクエストに依存関係はありません。
issues.dependency.add=依存関係を追加...
issues.dependency.cancel=キャンセル
issues.dependency.remove=削除
Expand Down
2 changes: 0 additions & 2 deletions options/locale/locale_ko-KR.ini
Original file line number Diff line number Diff line change
Expand Up @@ -816,8 +816,6 @@ issues.due_date_remove=%s %s 마감일이 삭제되었습니다.
issues.due_date_overdue=기한 초과
issues.due_date_invalid=기한이 올바르지 않거나 범위를 벗어났습니다. 'yyyy-mm-dd'형식을 사용해주십시오.
issues.dependency.title=의존성
issues.dependency.issue_no_dependencies=이 이슈는 어떠한 의존성도 가지지 않습니다.
issues.dependency.pr_no_dependencies=이 풀 리퀘스트는 어떠한 의존성도 가지지 않습니다.
issues.dependency.add=의존성 추가...
issues.dependency.cancel=취소
issues.dependency.remove=제거
Expand Down
2 changes: 0 additions & 2 deletions options/locale/locale_lv-LV.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1382,8 +1382,6 @@ issues.due_date_remove=noņēma izpildes termiņu %s %s
issues.due_date_overdue=Nokavēts
issues.due_date_invalid=Datums līdz nav korekts. Izmantojiet formātu 'gggg-mm-dd'.
issues.dependency.title=Atkarības
issues.dependency.issue_no_dependencies=Šai problēmai pagaidām nav nevienas atkarības.
issues.dependency.pr_no_dependencies=Šim izmaiņu pieprasījumam pagaidām nav nevienas atkarības.
issues.dependency.add=Pievienot atkarību…
issues.dependency.cancel=Atcelt
issues.dependency.remove=Noņemt
Expand Down
2 changes: 0 additions & 2 deletions options/locale/locale_nl-NL.ini
Original file line number Diff line number Diff line change
Expand Up @@ -1152,8 +1152,6 @@ issues.due_date_remove=heeft %[2]s de deadline %[1]s verwijderd
issues.due_date_overdue=Over tijd
issues.due_date_invalid=De deadline is ongeldig of buiten bereik. Gebruik het formaat 'jjjj-mm-dd'.
issues.dependency.title=Afhankelijkheden
issues.dependency.issue_no_dependencies=Deze kwestie heeft momenteel geen afhankelijkheden.
issues.dependency.pr_no_dependencies=Deze pull-aanvraag heeft momenteel geen afhankelijkheden.
issues.dependency.add=Voeg afhankelijkheid toe…
issues.dependency.cancel=Annuleer
issues.dependency.remove=Verwijder
Expand Down
Loading

0 comments on commit bb1fcd9

Please sign in to comment.