writerpool

package module
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 17, 2023 License: GPL-3.0 Imports: 6 Imported by: 0

README

writerpool

file writer pool for large discrete file write.

Introduction

The main contribute of this project is to develop the writerpool. It will great impove the ability of huge file storage, such as log file store and analysis.

In this project, the log data will be saved to differernt files by hours, and it supports the discrete time logs, that is the arrival time of log will not be sequential. The main designs are shown bellow

  • We use bufio to save data to disk by a buffer. The data will first be saved to buffer, and flushed to disk if the buffer capacity is less than the threshold.
  • We set timer to flush data and delete unused file pointer. The first timer to flush data is acted as a last resort. The second timer is used to save the memory.
  • We use synv.Map to get file pointer and writer pointer, which is thread safe.

Compare with using file.WriteString to save data, our method increased QPS by 5 times (up to 1652 QPS) and only use 38% memory. The details is shown in writerpool_test.go.

Quick Start

# get the package
go get github.com/BamLubi/writerpool
import "github.com/BamLubi/writerpool"

func main() {
    wp := writerpool.New()
    defer wp.Close()

    err := wp.WriteStringWithDate("directory name", "file name", "data")
    if err != nil {
        panic(err)
    }
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type WriterPool

type WriterPool struct {
	// contains filtered or unexported fields
}

func New

func New() *WriterPool

New function will return a new object pointer of WriterPool

func (*WriterPool) Close

func (wp *WriterPool) Close() error

Close is used to close all file interface in WriterPool

func (*WriterPool) GetStatus

func (wp *WriterPool) GetStatus() map[string]interface{}

GetStatus provides a interface to watch the status of writerpool

func (*WriterPool) SetTimer2Del

func (wp *WriterPool) SetTimer2Del(key string, file *os.File, writer *bufio.Writer, mu *sync.Mutex)

SetTimer2Del is used to delete unused interface{}

func (*WriterPool) SetTimer2Flush

func (wp *WriterPool) SetTimer2Flush(key string, writer *bufio.Writer, mu *sync.Mutex)

SetTimer2Flush is used to Flush Data to disk it will make sure that all data can be flushed into file

func (*WriterPool) WriteStringWithDate

func (wp *WriterPool) WriteStringWithDate(dirName string, fileName string, data string) error

WriteStringWithDate will use target date to stroe file The file will store at path/to/data/date/hour.txt

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL