Skip to content

Commit

Permalink
make commitTTL equal to EditDuration
Browse files Browse the repository at this point in the history
So that image is committed to permanent
storage after comment can no longer be edited.

Also, move cleanupTTL to Cleanup function,
as it's not used elsewhere in the code.
  • Loading branch information
paskal authored and umputun committed May 17, 2021
1 parent df547cc commit f9eb39d
Showing 1 changed file with 9 additions and 19 deletions.
28 changes: 9 additions & 19 deletions backend/app/store/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"encoding/base64"
"fmt"
"image"

// support gif and jpeg images decoding
_ "image/gif"
_ "image/jpeg"
Expand All @@ -36,8 +35,8 @@ import (
)

// Service wraps Store with common functions needed for any store implementation
// It also provides async Submit with func param retrieving all submitting ids.
// Submitted ids committed (i.e. moved from staging to final) on commitTTL expiration.
// It also provides async Submit with func param retrieving all submitting IDs.
// Submitted IDs committed (i.e. moved from staging to final) on ServiceParams.EditDuration expiration.
type Service struct {
ServiceParams

Expand All @@ -57,12 +56,6 @@ type ServiceParams struct {
MaxSize int
MaxHeight int
MaxWidth int

// duration of time after which images are checked and committed if still
// present in the submitted comment after it's EditDuration is expired
commitTTL time.Duration
// duration of time after which images are deleted from staging
cleanupTTL time.Duration
}

// StoreInfo contains image store meta information
Expand Down Expand Up @@ -95,10 +88,6 @@ type submitReq struct {

// NewService returns new Service instance
func NewService(s Store, p ServiceParams) *Service {
p.commitTTL = p.EditDuration * 15 / 10 // Commit call on every 1.5 * EditDuration
p.cleanupTTL = p.EditDuration * 25 / 10 // Cleanup call on every 2.5 * EditDuration
// In case Cleanup and Submit start at the same time (case of stale staging images check
// on the program start) these TTL values guarantee that Commit will happen before Cleanup.
return &Service{ServiceParams: p, store: s}
}

Expand Down Expand Up @@ -127,8 +116,8 @@ func (s *Service) Submit(idsFn func() []string) {
go func() {
defer s.wg.Done()
for req := range s.submitCh {
// wait for commitTTL expiration with emergency pass on term
for atomic.LoadInt32(&s.term) == 0 && time.Since(req.TS) <= s.commitTTL {
// wait for EditDuration expiration with emergency pass on term
for atomic.LoadInt32(&s.term) == 0 && time.Since(req.TS) <= s.EditDuration {
time.Sleep(time.Millisecond * 10) // small sleep to relive busy wait but keep reactive for term (close)
}
err := s.SubmitAndCommit(req.idsFn)
Expand Down Expand Up @@ -186,17 +175,18 @@ func (s *Service) ExtractPictures(commentHTML string) (ids []string, err error)
return ids, nil
}

// Cleanup runs periodic cleanup with cleanupTTL. Blocking loop, should be called inside of goroutine by consumer
// Cleanup runs periodic cleanup with 2.5*ServiceParams.EditDuration. Blocking loop, should be called inside of goroutine by consumer
func (s *Service) Cleanup(ctx context.Context) {
log.Printf("[INFO] start pictures cleanup, staging ttl=%v", s.cleanupTTL)
cleanupTTL := s.EditDuration * 25 / 10 // cleanup images older than 2.5 * EditDuration
log.Printf("[INFO] start pictures cleanup, staging ttl=%v", cleanupTTL)

for {
select {
case <-ctx.Done():
log.Printf("[INFO] cleanup terminated, %v", ctx.Err())
return
case <-time.After(s.cleanupTTL):
if err := s.store.Cleanup(ctx, s.cleanupTTL); err != nil {
case <-time.After(cleanupTTL):
if err := s.store.Cleanup(ctx, cleanupTTL); err != nil {
log.Printf("[WARN] failed to cleanup, %v", err)
}
}
Expand Down

0 comments on commit f9eb39d

Please sign in to comment.