/
drop.go
49 lines (36 loc) · 1.05 KB
/
drop.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// Unless explicitly stated otherwise all files in this repository are licensed
// under the MIT License.
// This product includes software developed at Guance Cloud (https://www.guance.com/).
// Copyright 2021-present Guance, Inc.
package diskcache
import "os"
const (
reasonExceedCapacity = "exceed-max-capacity"
reasonBadDataFile = "bad-data-file"
)
func (c *DiskCache) dropBatch() error {
c.rwlock.Lock()
defer c.rwlock.Unlock()
if len(c.dataFiles) == 0 {
return nil
}
fname := c.dataFiles[0]
if c.rfd != nil && c.curReadfile == fname {
if err := c.rfd.Close(); err != nil {
return err
}
c.rfd = nil
}
if fi, err := os.Stat(fname); err == nil {
if err := os.Remove(fname); err != nil {
return err
}
c.size -= fi.Size()
c.dataFiles = c.dataFiles[1:]
droppedBatchVec.WithLabelValues(c.path, reasonExceedCapacity).Inc()
droppedBytesVec.WithLabelValues(c.path).Add(float64(fi.Size()))
datafilesVec.WithLabelValues(c.path).Set(float64(len(c.dataFiles)))
sizeVec.WithLabelValues(c.path).Set(float64(c.size))
}
return nil
}