Skip to content

Commit

Permalink
Guardrail around panic and bad data (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 authored Sep 20, 2023
1 parent 506a7a6 commit e815a3d
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/ttlmap/ttlmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
type ItemWrapper struct {
Value interface{} `yaml:"value"`
Expiration int64 `yaml:"expiration"`
DoNotFlushToDisk bool
DoNotFlushToDisk bool `yaml:"-"`
}

type TTLMap struct {
Expand Down Expand Up @@ -115,6 +115,9 @@ func (t *TTLMap) cleanup() {
}

func (t *TTLMap) flush() error {
t.mu.Lock()
defer t.mu.Unlock()

if !t.shouldSave {
return nil
}
Expand Down Expand Up @@ -165,6 +168,12 @@ func (t *TTLMap) loadFromFile() error {
return fmt.Errorf("failed to unmarshal data, err: %v", err)
}

if data == nil {
data = make(map[string]*ItemWrapper)
}

t.mu.Lock()
t.data = data
t.mu.Unlock()
return nil
}

0 comments on commit e815a3d

Please sign in to comment.