Skip to content

Commit

Permalink
Add pprof server on 6060 and /debug route
Browse files Browse the repository at this point in the history
  • Loading branch information
bcl committed Sep 14, 2022
1 parent 540f5b3 commit dc93264
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 6 deletions.
31 changes: 25 additions & 6 deletions internal/dnfjson/cache.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package dnfjson

import (
"bytes"
"encoding/gob"
"fmt"
"io/fs"
"os"
Expand Down Expand Up @@ -213,8 +215,8 @@ func dirSize(path string) (uint64, error) {
// dnfResults holds the results of a dnfjson request
// expire is the time the request was made, used to expire the entry
type dnfResults struct {
expire time.Time
pkgs rpmmd.PackageList
Expire time.Time
Pkgs rpmmd.PackageList
}

// dnfCache is a cache of results from dnf-json requests
Expand All @@ -241,7 +243,7 @@ func (d *dnfCache) CleanCache() {

// Delete expired resultCache entries
for k := range d.results {
if time.Since(d.results[k].expire) > d.timeout {
if time.Since(d.results[k].Expire) > d.timeout {
delete(d.results, k)
}
}
Expand All @@ -254,15 +256,32 @@ func (d *dnfCache) Get(hash string) (rpmmd.PackageList, bool) {
defer d.RUnlock()

result, ok := d.results[hash]
if !ok || time.Since(result.expire) >= d.timeout {
if !ok || time.Since(result.Expire) >= d.timeout {
return rpmmd.PackageList{}, false
}
return result.pkgs, true
return result.Pkgs, true
}

// Store saves the package list in the cache
func (d *dnfCache) Store(hash string, pkgs rpmmd.PackageList) {
d.Lock()
defer d.Unlock()
d.results[hash] = dnfResults{expire: time.Now(), pkgs: pkgs}
d.results[hash] = dnfResults{Expire: time.Now(), Pkgs: pkgs}
}

// Info returns about about the cache
func (d *dnfCache) Info() map[string]string {

m := make(map[string]string)
m["timeout"] = fmt.Sprintf("%d", d.timeout)
m["entries"] = fmt.Sprintf("%d", len(d.results))

// Try to guesstimate the amount of memory used by marshaling the cache into a byte stream
b := new(bytes.Buffer)
if err := gob.NewEncoder(b).Encode(d.results); err != nil {
m["error"] = err.Error()
} else {
m["size"] = fmt.Sprintf("%d", b.Len())
}
return m
}
4 changes: 4 additions & 0 deletions internal/dnfjson/dnfjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ func NewSolver(modulePlatformID string, releaseVer string, arch string, cacheDir
return s.NewWithConfig(modulePlatformID, releaseVer, arch)
}

func (s *Solver) CacheDebugInfo() map[string]string {
return s.BaseSolver.resultCache.Info()
}

// Depsolve the list of required package sets with explicit excludes using
// their associated repositories. Each package set is depsolved as a separate
// transactions in a chain. It returns a list of all packages (with solved
Expand Down
37 changes: 37 additions & 0 deletions internal/weldr/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import (
"strings"
"time"

_ "net/http/pprof" //nolint:gosec

"github.com/BurntSushi/toml"
"github.com/gobwas/glob"
"github.com/google/uuid"
Expand Down Expand Up @@ -201,6 +203,12 @@ func New(repoPaths []string, stateDir string, solver *dnfjson.BaseSolver, dr *di
distros: validDistros(rr, dr, archName, logger),
distrosImageTypeDenylist: distrosImageTypeDenylist,
}

// XXX BCL
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()

return setupRouter(api), nil
}

Expand All @@ -212,6 +220,7 @@ func setupRouter(api *API) *API {
api.router.NotFound = http.HandlerFunc(notFoundHandler)

api.router.GET("/api/status", api.statusHandler)
api.router.GET("/api/debug", api.debugHandler)
api.router.GET("/api/v:version/projects/source/list", api.sourceListHandler)
api.router.GET("/api/v:version/projects/source/info/", api.sourceEmptyInfoHandler)
api.router.GET("/api/v:version/projects/source/info/:sources", api.sourceInfoHandler)
Expand Down Expand Up @@ -611,6 +620,34 @@ func (api *API) statusHandler(writer http.ResponseWriter, request *http.Request,
common.PanicOnError(err)
}

func (api *API) debugHandler(writer http.ResponseWriter, request *http.Request, _ httprouter.Params) {

distroName, err := api.parseDistro(request.URL.Query())
if err != nil {
errors := responseError{
ID: "DistroError",
Msg: err.Error(),
}
statusResponseError(writer, http.StatusBadRequest, errors)
return
}

d := api.getDistro(distroName)
if d == nil {
errors := responseError{
ID: "DistroError",
Msg: fmt.Sprintf("GetDistro - unknown distribution: %s", distroName),
}
statusResponseError(writer, http.StatusBadRequest, errors)
return
}

solver := api.solver.NewWithConfig(d.ModulePlatformID(), d.Releasever(), api.archName)
info := solver.CacheDebugInfo()
err = json.NewEncoder(writer).Encode(info)
common.PanicOnError(err)
}

func (api *API) sourceListHandler(writer http.ResponseWriter, request *http.Request, params httprouter.Params) {
if !verifyRequestVersion(writer, params, 0) {
return
Expand Down

0 comments on commit dc93264

Please sign in to comment.