github.com/GuanceCloud/cliutils@v1.1.21/diskcache/drop.go (about)

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