Skip to content

Commit

Permalink
Speed up gpg initialization (#1528)
Browse files Browse the repository at this point in the history
Fixes #1527

RELEASE_NOTES=[ENHANCEMENT] Cache gpg binary location

Signed-off-by: Dominik Schulz <[email protected]>
  • Loading branch information
dominikschulz authored Aug 15, 2020
1 parent bce0bdc commit 3ba0d3f
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ dev.sh
coverage.out
coverage-all.*

# Profiling
*.out

# Compiled Object files, Static and Dynamic libs (Shared Objects)
*.o
*.a
Expand Down
40 changes: 37 additions & 3 deletions internal/backend/crypto/gpg/cli/binary.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@ package cli
import (
"context"
"errors"
"fmt"
"io/ioutil"
"os/exec"
"path/filepath"
"sort"
"strings"

"github.com/gopasspw/gopass/internal/debug"
"github.com/gopasspw/gopass/pkg/appdir"
)

var (
Expand All @@ -21,25 +26,51 @@ func (g *GPG) Binary() string {
return g.binary
}

func binaryLocCacheFn() string {
return filepath.Join(appdir.UserCache(), "gpg-binary.loc")
}

func readBinaryLocFromCache() (string, error) {
fn := binaryLocCacheFn()
buf, err := ioutil.ReadFile(fn)
if err != nil {
return "", err
}
loc := strings.TrimSpace(string(buf))
if loc == "" {
return "", fmt.Errorf("empty location in cache")
}
return loc, nil
}

func writeBinaryLocToCache(fn string) error {
return ioutil.WriteFile(binaryLocCacheFn(), []byte(fn), 0644)
}

// Binary returns the GPG binary location
func Binary(ctx context.Context, bin string) (string, error) {
if gpgBinC != "" {
return gpgBinC, nil
}
if binLoc, err := readBinaryLocFromCache(); err == nil {
gpgBinC = binLoc
debug.Log("read binary from cache: %s", binLoc)
return binLoc, nil
}

bins, err := detectBinaryCandidates(bin)
if err != nil {
return "", err
}
bv := make(byVersion, 0, len(bins))
for _, b := range bins {
debug.Log("gpg.detectBinary - Looking for '%s' ...", b)
debug.Log("Looking for '%s' ...", b)
if p, err := exec.LookPath(b); err == nil {
gb := gpgBin{
path: p,
ver: version(ctx, p),
}
debug.Log("gpg.detectBinary - Found '%s' at '%s' (%s)", b, p, gb.ver.String())
debug.Log("Found '%s' at '%s' (%s)", b, p, gb.ver.String())
bv = append(bv, gb)
}
}
Expand All @@ -48,7 +79,10 @@ func Binary(ctx context.Context, bin string) (string, error) {
}
sort.Sort(bv)
binary := bv[len(bv)-1].path
debug.Log("gpg.detectBinary - using '%s'", binary)
debug.Log("using '%s'", binary)
gpgBinC = binary
if err := writeBinaryLocToCache(binary); err != nil {
debug.Log("failed to write binary location to cache file: %s", err)
}
return binary, nil
}
23 changes: 23 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"os"
"os/signal"
"runtime"
"runtime/pprof"
"sort"
"strings"
"time"
Expand Down Expand Up @@ -49,6 +50,17 @@ var (
)

func main() {
if cp := os.Getenv("GOPASS_CPU_PROFILE"); cp != "" {
f, err := os.Create(cp)
if err != nil {
log.Fatalf("could not create CPU profile at %s: %s", cp, err)
}
defer f.Close()
if err := pprof.StartCPUProfile(f); err != nil {
log.Fatalf("could not start CPU profile: %s", err)
}
defer pprof.StopCPUProfile()
}
if err := protect.Pledge("stdio rpath wpath cpath tty proc exec"); err != nil {
panic(err)
}
Expand Down Expand Up @@ -83,6 +95,17 @@ func main() {
log.Fatal(err)
}
q.Wait(ctx)
if mp := os.Getenv("GOPASS_MEM_PROFILE"); mp != "" {
f, err := os.Create(mp)
if err != nil {
log.Fatalf("could not write mem profile to %s: %s", mp, err)
}
defer f.Close()
runtime.GC()
if err := pprof.WriteHeapProfile(f); err != nil {
log.Fatalf("could not write heap profile: %s", err)
}
}
}

func setupApp(ctx context.Context, sv semver.Version) (context.Context, *cli.App) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/fsutil/fsutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func IsFile(path string) bool {
// not found
return false
}
debug.Log("failed to check dir %s: %s\n", path, err)
debug.Log("failed to check file %s: %s\n", path, err)
return false
}

Expand Down

0 comments on commit 3ba0d3f

Please sign in to comment.