diff options
author | 6543 <m.huber@kithara.com> | 2023-10-21 04:05:29 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-21 10:05:29 +0800 |
commit | b2f828db5e5878d47de32c292d95c95c4217de68 (patch) | |
tree | fd00c29654fb9d4580dc590ba6250c49fc1941ac /models/db | |
parent | 510d07506e43d1ee1903a676cb0bb305bfbc306f (diff) | |
download | gitea-b2f828db5e5878d47de32c292d95c95c4217de68.tar.gz gitea-b2f828db5e5878d47de32c292d95c95c4217de68.zip |
refactor: make db iterate context aware (#27710)
the iteration will run until finished atm.
this changes it by checking if if the context got canceled before each
run of a loop sequence is executed
[View this pull with now
whitespace](https://github.com/go-gitea/gitea/pull/27710/files?diff=unified&w=1)
Diffstat (limited to 'models/db')
-rw-r--r-- | models/db/iterate.go | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/models/db/iterate.go b/models/db/iterate.go index fc8c78d83e..e1caefa72b 100644 --- a/models/db/iterate.go +++ b/models/db/iterate.go @@ -17,22 +17,27 @@ func Iterate[Bean any](ctx context.Context, cond builder.Cond, f func(ctx contex batchSize := setting.Database.IterateBufferSize sess := GetEngine(ctx) for { - beans := make([]*Bean, 0, batchSize) - if cond != nil { - sess = sess.Where(cond) - } - if err := sess.Limit(batchSize, start).Find(&beans); err != nil { - return err - } - if len(beans) == 0 { - return nil - } - start += len(beans) - - for _, bean := range beans { - if err := f(ctx, bean); err != nil { + select { + case <-ctx.Done(): + return ctx.Err() + default: + beans := make([]*Bean, 0, batchSize) + if cond != nil { + sess = sess.Where(cond) + } + if err := sess.Limit(batchSize, start).Find(&beans); err != nil { return err } + if len(beans) == 0 { + return nil + } + start += len(beans) + + for _, bean := range beans { + if err := f(ctx, bean); err != nil { + return err + } + } } } } |