From a7ad1297ac6bb6424b6dfbae1e768300cb4e459b Mon Sep 17 00:00:00 2001 From: Kirill Smelkov Date: Sun, 18 Feb 2018 23:31:45 +0300 Subject: [PATCH] driver: Don't waste resources setting/being-notified up in progress handler if context cannot be canceled Please see https://github.com/mattn/go-sqlite3/pull/530 for similar change. Here it is not additional goroutine but a callback setup which will be called ever 100 ops. We can avoid that if we know that ctx cannot be canceled. --- driver.go | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/driver.go b/driver.go index c7777ae..5c3f201 100644 --- a/driver.go +++ b/driver.go @@ -140,8 +140,10 @@ func (c *conn) ExecContext(ctx context.Context, query string, args []driver.Name if c.c.IsClosed() { return nil, driver.ErrBadConn } - c.c.ProgressHandler(progressHandler, 100, ctx) - defer c.c.ProgressHandler(nil, 0, nil) + if ctx.Done() != nil { + c.c.ProgressHandler(progressHandler, 100, ctx) + defer c.c.ProgressHandler(nil, 0, nil) + } if len(args) == 0 { if query == "unwrap" { return nil, ConnError{c: c.c} @@ -254,8 +256,10 @@ func (s *stmt) ExecContext(ctx context.Context, args []driver.NamedValue) (drive if err := s.s.bindNamedValue(args); err != nil { return nil, err } - s.s.c.ProgressHandler(progressHandler, 100, ctx) - defer s.s.c.ProgressHandler(nil, 0, nil) + if ctx.Done() != nil { + s.s.c.ProgressHandler(progressHandler, 100, ctx) + defer s.s.c.ProgressHandler(nil, 0, nil) + } if err := s.s.exec(); err != nil { return nil, ctxError(ctx, err) } @@ -270,7 +274,9 @@ func (s *stmt) QueryContext(ctx context.Context, args []driver.NamedValue) (driv return nil, err } s.rowsRef = true - s.s.c.ProgressHandler(progressHandler, 100, ctx) + if ctx.Done() != nil { + s.s.c.ProgressHandler(progressHandler, 100, ctx) + } return &rowsImpl{s, nil, ctx}, nil } @@ -308,7 +314,9 @@ func (r *rowsImpl) Next(dest []driver.Value) error { } func (r *rowsImpl) Close() error { - r.s.s.c.ProgressHandler(nil, 0, nil) + if r.ctx.Done() != nil { + r.s.s.c.ProgressHandler(nil, 0, nil) + } r.s.rowsRef = false if r.s.pendingClose { return r.s.Close()