Skip to content

Commit

Permalink
update cosmovisor README and set buffer size to ENV setting
Browse files Browse the repository at this point in the history
  • Loading branch information
allthatjazzleo committed Apr 10, 2021
1 parent ce4c944 commit c471405
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
14 changes: 14 additions & 0 deletions cosmovisor/args.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package cosmovisor

import (
"bufio"
"errors"
"fmt"
"net/url"
"os"
"path/filepath"
"strconv"
)

const (
Expand All @@ -21,6 +23,7 @@ type Config struct {
Name string
AllowDownloadBinaries bool
RestartAfterUpgrade bool
LogBufferSize int
}

// Root returns the root directory where all info lives
Expand Down Expand Up @@ -99,6 +102,17 @@ func GetConfigFromEnv() (*Config, error) {
cfg.RestartAfterUpgrade = true
}

logBufferSizeStr := os.Getenv("DAEMON_LOG_BUFFER_SIZE")
if logBufferSizeStr != "" {
logBufferSize, err := strconv.Atoi(logBufferSizeStr)
if err != nil {
return nil, err
}
cfg.LogBufferSize = logBufferSize * 1024
} else {
cfg.LogBufferSize = bufio.MaxScanTokenSize
}

if err := cfg.validate(); err != nil {
return nil, err
}
Expand Down
11 changes: 11 additions & 0 deletions cosmovisor/process.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ func LaunchProcess(cfg *Config, args []string, stdout, stderr io.Writer) (bool,

scanOut := bufio.NewScanner(io.TeeReader(outpipe, stdout))
scanErr := bufio.NewScanner(io.TeeReader(errpipe, stderr))
// set scanner's buffer size to cfg.LogBufferSize, and ensure larger than bufio.MaxScanTokenSize otherwise fallback to bufio.MaxScanTokenSize
var maxCapacity int
if cfg.LogBufferSize < bufio.MaxScanTokenSize {
maxCapacity = bufio.MaxScanTokenSize
} else {
maxCapacity = cfg.LogBufferSize
}
bufOut := make([]byte, maxCapacity)
bufErr := make([]byte, maxCapacity)
scanOut.Buffer(bufOut, maxCapacity)
scanErr.Buffer(bufErr, maxCapacity)

if err := cmd.Start(); err != nil {
return false, fmt.Errorf("launching process %s %s: %w", bin, strings.Join(args, " "), err)
Expand Down
4 changes: 0 additions & 4 deletions cosmovisor/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,6 @@ type UpgradeInfo struct {
// It returns (nil, err) if the input stream errored
// It returns (nil, nil) if the input closed without ever matching the regexp
func WaitForUpdate(scanner *bufio.Scanner) (*UpgradeInfo, error) {
// set larger buffer in case the line is too long, the default one is 64*1024
const maxCapacity = 512 * 1024
buf := make([]byte, maxCapacity)
scanner.Buffer(buf, maxCapacity)
for scanner.Scan() {
line := scanner.Text()
if upgradeRegex.MatchString(line) {
Expand Down
1 change: 1 addition & 0 deletions docs/run-node/cosmovisor.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ binary).
command line arguments and flags (but new binary) after a successful upgrade. By default, `cosmovisor` dies
afterwards and allows the supervisor to restart it if needed. Note that this will not auto-restart the child
if there was an error.
* `DAEMON_LOG_BUFFER_SIZE` (*optional*) is the buffer size for cosmovisor to scan log. If not set, it will use the default [64](https://github.com/golang/go/blob/2217e89ba326875470a856cd0da79f3ec9a896b8/src/bufio/scan.go#L80). (e.g. set to `256` or `512`) It is to avoid scanning stuck in case of long line of the log.

## Data Folder Layout

Expand Down

0 comments on commit c471405

Please sign in to comment.