1 Star 0 Fork 0

zhuchance / kubernetes

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
backend.go 1.64 KB
一键复制 编辑 原始数据 按行查看 历史
package backend
import (
"io"
"log"
"time"
"github.com/boltdb/bolt"
)
type Backend interface {
BatchTx() BatchTx
Snapshot(w io.Writer) (n int64, err error)
ForceCommit()
Close() error
}
type backend struct {
db *bolt.DB
batchInterval time.Duration
batchLimit int
batchTx *batchTx
stopc chan struct{}
donec chan struct{}
}
func New(path string, d time.Duration, limit int) Backend {
return newBackend(path, d, limit)
}
func newBackend(path string, d time.Duration, limit int) *backend {
db, err := bolt.Open(path, 0600, nil)
if err != nil {
log.Panicf("backend: cannot open database at %s (%v)", path, err)
}
b := &backend{
db: db,
batchInterval: d,
batchLimit: limit,
stopc: make(chan struct{}),
donec: make(chan struct{}),
}
b.batchTx = newBatchTx(b)
go b.run()
return b
}
// BatchTx returns the current batch tx in coalescer. The tx can be used for read and
// write operations. The write result can be retrieved within the same tx immediately.
// The write result is isolated with other txs until the current one get committed.
func (b *backend) BatchTx() BatchTx {
return b.batchTx
}
// force commit the current batching tx.
func (b *backend) ForceCommit() {
b.batchTx.Commit()
}
func (b *backend) Snapshot(w io.Writer) (n int64, err error) {
b.db.View(func(tx *bolt.Tx) error {
n, err = tx.WriteTo(w)
return nil
})
return n, err
}
func (b *backend) run() {
defer close(b.donec)
for {
select {
case <-time.After(b.batchInterval):
case <-b.stopc:
b.batchTx.CommitAndStop()
return
}
b.batchTx.Commit()
}
}
func (b *backend) Close() error {
close(b.stopc)
<-b.donec
return b.db.Close()
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/meoom/kubernetes.git
git@gitee.com:meoom/kubernetes.git
meoom
kubernetes
kubernetes
v1.2.0-alpha.3

搜索帮助

344bd9b3 5694891 D2dac590 5694891