Skip to content

Commit

Permalink
driver: Don't waste resources setting/being-notified up in progress h…
Browse files Browse the repository at this point in the history
…andler if context cannot be canceled

Please see mattn/go-sqlite3#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.
  • Loading branch information
navytux committed Feb 18, 2018
1 parent 79ff27e commit a7ad129
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand Down Expand Up @@ -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)
}
Expand All @@ -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
}

Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit a7ad129

Please sign in to comment.