Skip to content

Commit

Permalink
attempt to fix #584 by making submitted image commits on a half of TTL
Browse files Browse the repository at this point in the history
  • Loading branch information
umputun committed Jan 27, 2020
1 parent afe8ed6 commit e4e80b9
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion backend/app/store/image/fs_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (f *FileSystem) cleanup(_ context.Context, ttl time.Duration) error {
return nil
}
age := time.Since(info.ModTime())
if age > ttl {
if age > (ttl + 100*time.Millisecond) { // delay cleanup triggering to allow commit
log.Printf("[INFO] remove staging image %s, age %v", fpath, age)
rmErr := os.Remove(fpath)
_ = os.Remove(path.Dir(fpath)) // try to remove directory
Expand Down
2 changes: 1 addition & 1 deletion backend/app/store/image/fs_store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func TestFsStore_Cleanup(t *testing.T) {
time.Sleep(100 * time.Millisecond)
img3 := save("blah_ff3.png", "user2")

time.Sleep(100 * time.Millisecond) // make first image expired
time.Sleep(200 * time.Millisecond) // make first image expired
err := svc.cleanup(context.Background(), time.Millisecond*300)
assert.NoError(t, err)

Expand Down
4 changes: 2 additions & 2 deletions backend/app/store/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ func (s *Service) Submit(idsFn func() []string) {
defer s.wg.Done()
for req := range s.submitCh {
// wait for TTL expiration with emergency pass on term
for atomic.LoadInt32(&s.term) == 0 && time.Since(req.TS) <= s.TTL {
for atomic.LoadInt32(&s.term) == 0 && time.Since(req.TS) <= s.TTL/2 { // commit on a half of TTL
time.Sleep(time.Millisecond * 10) // small sleep to relive busy wait but keep reactive for term (close)
}
for _, id := range req.idsFn() {
Expand Down Expand Up @@ -123,7 +123,7 @@ func (s *Service) Cleanup(ctx context.Context) {
case <-ctx.Done():
log.Printf("[INFO] cleanup terminated, %v", ctx.Err())
return
case <-time.After(s.TTL / 2):
case <-time.After(s.TTL / 2): // cleanup call on every 1/2 TTL
if err := s.Store.cleanup(ctx, s.TTL); err != nil {
log.Printf("[WARN] failed to cleanup, %v", err)
}
Expand Down

0 comments on commit e4e80b9

Please sign in to comment.