From 8415bee5da8c4238293fd22d17d286100e3177be Mon Sep 17 00:00:00 2001 From: Julien Neuhart Date: Tue, 27 Aug 2019 09:08:00 +0200 Subject: [PATCH] reducing the number of concurrent dev tools connections to Chrome --- internal/pkg/printer/chrome.go | 45 +++++++++++++++++++++++++++++++--- internal/pkg/printer/office.go | 8 +++--- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/internal/pkg/printer/chrome.go b/internal/pkg/printer/chrome.go index 75b62db7..aff21e75 100644 --- a/internal/pkg/printer/chrome.go +++ b/internal/pkg/printer/chrome.go @@ -61,6 +61,14 @@ func DefaultChromePrinterOptions(config conf.Config) ChromePrinterOptions { } } +// nolint: gochecknoglobals +var lockChrome = make(chan struct{}, 1) + +const maxDevtConnections int = 5 + +// nolint: gochecknoglobals +var devtConnections int + func (p chromePrinter) Print(destination string) error { const op string = "printer.chromePrinter.Print" logOptions(p.logger, p.opts) @@ -158,13 +166,44 @@ func (p chromePrinter) Print(destination string) error { } return nil } - if err := resolver(); err != nil { + if devtConnections < maxDevtConnections { + p.logger.DebugOp(op, "skipping lock acquisition...") + devtConnections++ + err := resolver() + devtConnections-- + if err != nil { + return xcontext.MustHandleError( + ctx, + xerror.New(op, err), + ) + } + return nil + } + p.logger.DebugOp(op, "waiting lock to be acquired...") + select { + case lockChrome <- struct{}{}: + // lock acquired. + p.logger.DebugOp(op, "lock acquired") + devtConnections++ + err := resolver() + devtConnections-- + <-lockChrome // we release the lock. + if err != nil { + return xcontext.MustHandleError( + ctx, + xerror.New(op, err), + ) + } + return nil + case <-ctx.Done(): + // failed to acquire lock before + // deadline. + p.logger.DebugOp(op, "failed to acquire lock before context.Context deadline") return xcontext.MustHandleError( ctx, - xerror.New(op, err), + ctx.Err(), ) } - return nil } func (p chromePrinter) enableEvents(ctx context.Context, client *cdp.Client) error { diff --git a/internal/pkg/printer/office.go b/internal/pkg/printer/office.go index b73a6c13..34ebdcb4 100644 --- a/internal/pkg/printer/office.go +++ b/internal/pkg/printer/office.go @@ -88,7 +88,7 @@ func (p officePrinter) Print(destination string) error { } // nolint: gochecknoglobals -var lock = make(chan struct{}, 1) +var lockUnoconv = make(chan struct{}, 1) func unoconv(ctx context.Context, logger xlog.Logger, fpath, destination string, opts OfficePrinterOptions) error { const op string = "printer.unoconv" @@ -115,14 +115,14 @@ func unoconv(ctx context.Context, logger xlog.Logger, fpath, destination string, } logger.DebugOp(op, "waiting lock to be acquired...") select { - case lock <- struct{}{}: + case lockUnoconv <- struct{}{}: // lock acquired. logger.DebugOp(op, "lock acquired") if err := resolver(); err != nil { - <-lock // we release the lock. + <-lockUnoconv // we release the lock. return xerror.New(op, err) } - <-lock // we release the lock. + <-lockUnoconv // we release the lock. return nil case <-ctx.Done(): // failed to acquire lock before