1 Star 0 Fork 0

蔡风华/ioutils

加入 Gitee
与超过 1400万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
recorder.go 3.40 KB
一键复制 编辑 原始数据 按行查看 历史
蔡风华 提交于 2023-05-30 19:58 +08:00 . 1.适配更新后的依赖库。
package ioutils
import (
"context"
"encoding/json"
"fmt"
"os"
"time"
"gitee.com/cfh008/baseutils/extendutil"
"gitee.com/cfh008/runutils"
)
type Writer interface {
Write(b []byte) (n int, err error)
Sync() error
Close() error
}
type FileWriter struct {
file *os.File
}
func NewFileWriter(file *os.File) *FileWriter {
return &FileWriter{file}
}
func (writer *FileWriter) Write(b []byte) (n int, err error) {
file := writer.file
if !extendutil.InterfaceIsNil(file) {
cnt, doErr := file.Write(b)
if doErr != nil {
err = fmt.Errorf("%v call file.Write with error %v", runutils.RunFuncName(), doErr)
return
}
_, doErr = file.WriteString("\n")
if doErr != nil {
err = fmt.Errorf("%v call file.WriteString with error %v", runutils.RunFuncName(), doErr)
return
}
n = cnt
} else {
err = fmt.Errorf("%v file is nil", runutils.RunFuncName())
}
return
}
func (writer *FileWriter) Sync() (err error) {
file := writer.file
if !extendutil.InterfaceIsNil(file) {
doErr := file.Sync()
if doErr != nil {
err = fmt.Errorf("%v call file.Sync with error %v", runutils.RunFuncName(), doErr)
return
}
}
return
}
func (writer *FileWriter) Close() (err error) {
file := writer.file
if !extendutil.InterfaceIsNil(file) {
doErr := file.Close()
if doErr != nil {
err = fmt.Errorf("%v call file.Close with error %v", runutils.RunFuncName(), doErr)
return
}
}
return
}
type Recorder struct {
ch chan []byte
writer Writer
}
func (recorder *Recorder) writeData(data []byte) {
recorder.ch <- data
}
func (recorder *Recorder) startRecord(ctx context.Context) {
// 每10秒同步一次数据到文件
ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for {
select {
case v := <-recorder.ch:
writer := recorder.writer
if !extendutil.InterfaceIsNil(writer) {
_, err := writer.Write(v)
if err != nil {
fmt.Printf("%v call writer.Write with error %v.\n", runutils.RunFuncName(), err)
}
} else {
fmt.Println(v)
}
case <-ticker.C:
writer := recorder.writer
if !extendutil.InterfaceIsNil(writer) {
err := writer.Sync()
if err != nil {
fmt.Printf("%v call writer.Sync with error %v.\n", runutils.RunFuncName(), err)
}
}
case <-ctx.Done():
writer := recorder.writer
if !extendutil.InterfaceIsNil(writer) {
err := writer.Sync()
if err != nil {
fmt.Printf("%v call writer.Sync with error %v.\n", runutils.RunFuncName(), err)
}
err = writer.Close()
if err != nil {
fmt.Printf("%v call writer.Close with error %v.\n", runutils.RunFuncName(), err)
}
}
fmt.Printf("%v to the end.\n", runutils.RunFuncName())
close(recorder.ch)
return
}
}
}
// 写入已经json序列化好的字符数组,字符串或者是可以进行json序列化的对象
func RecordData(recorder *Recorder, data interface{}) (err error) {
var target []byte
err = nil
switch ret := data.(type) {
case []byte:
target = ret
case string:
target = []byte(ret)
default:
target, err = json.Marshal(ret)
if err != nil {
return
}
}
recorder.writeData(target)
return
}
func SetRecordWriter(recorder *Recorder, writer Writer) {
recorder.writer = writer
}
// 开启存储数据的服务,并返回对象用于后续写数据
func StartRecorder(ctx context.Context, writer Writer) *Recorder {
recorder := &Recorder{ch: make(chan []byte, 1024), writer: writer}
go recorder.startRecord(ctx)
return recorder
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/cfh008/ioutils.git
git@gitee.com:cfh008/ioutils.git
cfh008
ioutils
ioutils
v0.2.8

搜索帮助