Skip to content

Commit 7940c36

Browse files
Fix panic in some long-running process, in rare conditions
1 parent 9bee84b commit 7940c36

1 file changed

Lines changed: 18 additions & 8 deletions

File tree

internal/ui/app.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ const (
2525
viewHelp
2626
)
2727

28+
const (
29+
sparklineSize = 15
30+
graphHistorySize = 300
31+
memHistorySize = 60
32+
globalFetchTimeout = 10 * time.Second
33+
)
34+
2835
type Config struct {
2936
Interval time.Duration
3037
SlowThreshold time.Duration
@@ -62,10 +69,6 @@ type certFetcher interface {
6269
DialTLSCertificates(ctx context.Context, hosts []string) []fetcher.CertificateInfo
6370
}
6471

65-
const sparklineSize = 15
66-
const graphHistorySize = 300
67-
const memHistorySize = 60
68-
6972
type App struct {
7073
fetcher fetcher.Fetcher
7174
config Config
@@ -797,24 +800,30 @@ func (a *App) doTick() tea.Cmd {
797800

798801
func (a *App) doFetch() tea.Cmd {
799802
return func() tea.Msg {
800-
snap, err := a.fetcher.Fetch(context.Background())
803+
ctx, cancel := context.WithTimeout(context.Background(), globalFetchTimeout)
804+
defer cancel()
805+
snap, err := a.fetcher.Fetch(ctx)
801806
return fetchMsg{snap: snap, err: err}
802807
}
803808
}
804809

805810
func (a *App) doRestart() tea.Cmd {
806811
return func() tea.Msg {
812+
ctx, cancel := context.WithTimeout(context.Background(), globalFetchTimeout)
813+
defer cancel()
807814
if r, ok := a.fetcher.(restarter); ok {
808-
return restartResultMsg{err: r.RestartWorkers(context.Background())}
815+
return restartResultMsg{err: r.RestartWorkers(ctx)}
809816
}
810817
return restartResultMsg{}
811818
}
812819
}
813820

814821
func (a *App) doFetchConfig() tea.Cmd {
815822
return func() tea.Msg {
823+
ctx, cancel := context.WithTimeout(context.Background(), globalFetchTimeout)
824+
defer cancel()
816825
if cf, ok := a.fetcher.(configFetcher); ok {
817-
raw, err := cf.FetchConfig(context.Background())
826+
raw, err := cf.FetchConfig(ctx)
818827
return configFetchMsg{raw: raw, err: err}
819828
}
820829
return configFetchMsg{err: fmt.Errorf("config inspection not supported")}
@@ -834,7 +843,8 @@ func (a *App) doFetchCertificates() tea.Cmd {
834843
return certFetchMsg{err: fmt.Errorf("certificate inspection not supported")}
835844
}
836845

837-
ctx := context.Background()
846+
ctx, cancel := context.WithTimeout(context.Background(), globalFetchTimeout)
847+
defer cancel()
838848
all := make([]fetcher.CertificateInfo, 0)
839849

840850
all = append(all, cf.FetchPKICertificates(ctx)...)

0 commit comments

Comments
 (0)