mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-08 00:22:14 +01:00
reducing the number of concurrent dev tools connections to Chrome
This commit is contained in:
@@ -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 {
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user