Skip to content

Commit

Permalink
Configurable RequestTimeout
Browse files Browse the repository at this point in the history
  • Loading branch information
tgulacsi committed Aug 23, 2024
1 parent b29aedf commit c3b8b9c
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 10 deletions.
7 changes: 5 additions & 2 deletions converter/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,14 @@ var (
// ConfSortBeforeMerge should be true if generally we should sort files by filename before merge
ConfSortBeforeMerge = config.Bool("sortBeforeMerge", false)

// ConfRequestTimeout is the general HTTP request timeout
ConfRequestTimeout = config.Duration("requestTimeout", 5*time.Minute)

// ConfChildTimeout is the time before the child gets killed
ConfChildTimeout = config.Duration("childTimeout", 10*time.Minute)
ConfChildTimeout = config.Duration("childTimeout", 4*time.Minute)

// ConfLofficeTimeout is the time before LibreOffice gets killed.
ConfLofficeTimeout = config.Duration("lofficeTimeout", time.Minute)
ConfLofficeTimeout = config.Duration("lofficeTimeout", 3*time.Minute)

// ConcLimit limits the concurrently running child processes
ConcLimit = NewRateLimiter(Concurrency)
Expand Down
2 changes: 1 addition & 1 deletion converter/mimetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func (d HTTPMIMEDetector) Match(b []byte) string {
type FileMIMEDetector struct{}

func (d FileMIMEDetector) Match(b []byte) string {
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
cmd := Exec.CommandContext(ctx, "file", "-E", "-b", "--mime-type", "-")
cmd.Stdin = bytes.NewReader(b)
Expand Down
2 changes: 1 addition & 1 deletion server.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ var defaultBeforeFuncs = []kithttp.RequestFunc{

func prepareContext(ctx context.Context, r *http.Request) context.Context {
// nosemgrep: dgryski.semgrep-go.contextcancelable.cancelable-context-not-systematically-cancelled
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
ctx, cancel := context.WithTimeout(ctx, *converter.ConfChildTimeout)
ctx = SetRequestCancel(ctx, cancel)
ctx = SetRequestID(ctx, "")
lgr := getLogger(ctx)
Expand Down
12 changes: 6 additions & 6 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,19 @@ var (
func onStart() {
var err error
if self, err = osext.Executable(); err != nil {
logger.Error( "error getting the path for self", "error", err)
logger.Error("error getting the path for self", "error", err)
} else {
var self2 string
if self2, err = filepath.Abs(self); err != nil {
logger.Error( "error getting the absolute path", "for", self, "error", err)
logger.Error("error getting the absolute path", "for", self, "error", err)
} else {
self = self2
}
}

var uname string
if u, e := user.Current(); e != nil {
logger.Error( "cannot get current user", "error", e)
logger.Error("cannot get current user", "error", e)
uname = os.Getenv("USER")
} else {
uname = u.Username
Expand All @@ -70,15 +70,15 @@ func onStart() {
// getTopOut returns the output of the topCmd - shall be protected with a mutex
func getTopOutput() ([]byte, error) {
topOut.Reset()
ctx, cancel := context.WithTimeout(context.Background(), time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
// nosemgrep: go.lang.security.audit.dangerous-exec-command.dangerous-exec-command
cmd := exec.CommandContext(ctx, topCmd[0], topCmd[1:]...)
cmd.Stdout = topOut
cmd.Stderr = os.Stderr
e := cmd.Run()
if e != nil {
logger.Error( "error calling", "cmd", topCmd, "error", e)
logger.Error("error calling", "cmd", topCmd, "error", e)
fmt.Fprintf(topOut, "\n\nerror calling %s: %s\n", topCmd, e)
}
return topOut.Bytes(), e
Expand All @@ -100,7 +100,7 @@ func (st *statInfo) fill() {
runtime.ReadMemStats(st.mem)
top, err := getTopOutput()
if err != nil {
logger.Error( "error calling top", "error", err)
logger.Error("error calling top", "error", err)
} else {
st.top = bytes.Replace(top, []byte("\n"), []byte("\n "), -1)
}
Expand Down

0 comments on commit c3b8b9c

Please sign in to comment.