Skip to content

Commit

Permalink
chore: HTTP caching
Browse files Browse the repository at this point in the history
  • Loading branch information
moul committed Nov 20, 2019
1 parent 99329a4 commit 47b009f
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
2 changes: 2 additions & 0 deletions cmd/depviz/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ var (
serverGodmode = serverFlags.Bool("godmode", false, "enable dangerous API calls")
serverWithPprof = serverFlags.Bool("with-pprof", false, "enable pprof endpoints")
serverWithoutRecovery = serverFlags.Bool("without-recovery", false, "disable panic recovery (dev)")
serverWithoutCache = serverFlags.Bool("without-cache", false, "disable HTTP caching")

runFlags = flag.NewFlagSet("run", flag.ExitOnError)
runNoPull = runFlags.Bool("no-pull", false, "don't pull providers (graph only)")
Expand Down Expand Up @@ -290,6 +291,7 @@ func execServer(args []string) error {
ShutdownTimeout: *serverShutdownTimeout,
WithPprof: *serverWithPprof,
WithoutRecovery: *serverWithoutRecovery,
WithoutCache: *serverWithoutCache,
Godmode: *serverGodmode,
}
svc, err = dvserver.New(ctx, store, schemaConfig, opts)
Expand Down
1 change: 1 addition & 0 deletions go.mod

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions go.sum

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

29 changes: 27 additions & 2 deletions internal/dvserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (
"github.com/oklog/run"
"github.com/rs/cors"
chilogger "github.com/treastech/logger"
cache "github.com/victorspringer/http-cache"
"github.com/victorspringer/http-cache/adapter/memory"
"go.uber.org/zap"
"google.golang.org/grpc"
"moul.io/depviz/internal/chiutil"
Expand All @@ -37,6 +39,7 @@ type Opts struct {
WithoutRecovery bool
WithPprof bool
Godmode bool
WithoutCache bool
}

type Service interface {
Expand Down Expand Up @@ -144,7 +147,6 @@ func New(ctx context.Context, h *cayley.Handle, schema *schema.Config, opts Opts
r.Use(middleware.Timeout(opts.RequestTimeout))
r.Use(middleware.RealIP)
r.Use(middleware.RequestID)
// FIXME: add caching
gwmux := runtime.NewServeMux(
runtime.WithMarshalerOption(runtime.MIMEWildcard, &gateway.JSONPb{
EmitDefaults: false,
Expand All @@ -158,8 +160,31 @@ func New(ctx context.Context, h *cayley.Handle, schema *schema.Config, opts Opts
return nil, fmt.Errorf("register service on gateway: %w", err)
}

var handler http.Handler = gwmux

// api endpoints
r.Mount("/api", http.StripPrefix("/api", gwmux))
if !opts.WithoutCache {
// FIXME: invalidate cache
memcached, err := memory.NewAdapter(
memory.AdapterWithAlgorithm(memory.LRU),
memory.AdapterWithCapacity(10000000),
)
if err != nil {
return nil, fmt.Errorf("memory cache: %w", err)
}

cacheClient, err := cache.NewClient(
cache.ClientWithAdapter(memcached),
cache.ClientWithTTL(10*time.Minute),
cache.ClientWithRefreshKey("opn"),
)
if err != nil {
return nil, fmt.Errorf("cache client: %w", err)
}

handler = cacheClient.Middleware(handler)
}
r.Mount("/api", http.StripPrefix("/api", handler))

// pprof endpoints
if opts.WithPprof {
Expand Down
2 changes: 1 addition & 1 deletion internal/githubprovider/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func fromIssues(issues []*github.Issue, logger *zap.Logger) (dvmodel.Batch, erro
for _, issue := range issues {
err := fromIssue(&batch, issue)
if err != nil {
logger.Warn("failed to parse issue", zap.Error(err))
logger.Warn("parse issue", zap.String("url", issue.GetHTMLURL()), zap.Error(err))
continue
}
}
Expand Down

0 comments on commit 47b009f

Please sign in to comment.