mirror of
https://github.com/gotenberg/gotenberg.git
synced 2026-08-18 13:12:17 +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 {
|
func (p chromePrinter) Print(destination string) error {
|
||||||
const op string = "printer.chromePrinter.Print"
|
const op string = "printer.chromePrinter.Print"
|
||||||
logOptions(p.logger, p.opts)
|
logOptions(p.logger, p.opts)
|
||||||
@@ -158,13 +166,44 @@ func (p chromePrinter) Print(destination string) error {
|
|||||||
}
|
}
|
||||||
return nil
|
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(
|
return xcontext.MustHandleError(
|
||||||
ctx,
|
ctx,
|
||||||
xerror.New(op, err),
|
xerror.New(op, err),
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
return nil
|
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,
|
||||||
|
ctx.Err(),
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p chromePrinter) enableEvents(ctx context.Context, client *cdp.Client) error {
|
func (p chromePrinter) enableEvents(ctx context.Context, client *cdp.Client) error {
|
||||||
|
|||||||
@@ -88,7 +88,7 @@ func (p officePrinter) Print(destination string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// nolint: gochecknoglobals
|
// 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 {
|
func unoconv(ctx context.Context, logger xlog.Logger, fpath, destination string, opts OfficePrinterOptions) error {
|
||||||
const op string = "printer.unoconv"
|
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...")
|
logger.DebugOp(op, "waiting lock to be acquired...")
|
||||||
select {
|
select {
|
||||||
case lock <- struct{}{}:
|
case lockUnoconv <- struct{}{}:
|
||||||
// lock acquired.
|
// lock acquired.
|
||||||
logger.DebugOp(op, "lock acquired")
|
logger.DebugOp(op, "lock acquired")
|
||||||
if err := resolver(); err != nil {
|
if err := resolver(); err != nil {
|
||||||
<-lock // we release the lock.
|
<-lockUnoconv // we release the lock.
|
||||||
return xerror.New(op, err)
|
return xerror.New(op, err)
|
||||||
}
|
}
|
||||||
<-lock // we release the lock.
|
<-lockUnoconv // we release the lock.
|
||||||
return nil
|
return nil
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
// failed to acquire lock before
|
// failed to acquire lock before
|
||||||
|
|||||||
Reference in New Issue
Block a user