1 Star 0 Fork 0

h79/gothird

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
app.go 3.92 KB
一键复制 编辑 原始数据 按行查看 历史
huqiuyun 提交于 2023-09-19 21:13 . log string
package token
import (
"encoding/gob"
"fmt"
"gitee.com/h79/goutils/common/logger"
"gitee.com/h79/goutils/common/system"
"go.uber.org/zap"
"time"
)
const (
fAll = 1
fReadId = 2
fVMap = 3
fApp = 4
ownerGob = "/owner.gob"
)
type AppItem struct {
item
conf Config
itemPath string
fUpdate chan int
}
type ConfigOptions func(conf *Config)
func NewAppItem(app App, tk Token, options ...ConfigOptions) *AppItem {
return newAppItem(&app, tk, VMap{}, options...)
}
func newAppItem(app *App, tk Token, vMap VMap, options ...ConfigOptions) *AppItem {
//for create file directory
ap := &AppItem{
item: item{
App: app,
tk: tk,
vMap: vMap,
appAt: updateTime{updateAt: time.Now().Unix()},
},
fUpdate: make(chan int, 10),
}
if len(vMap) > 0 {
//为了触发保存
ap.vMapAt.Update()
}
for i := range options {
options[i](&ap.conf)
}
ap.itemPath = fmt.Sprintf("%s/%s/%s", rootPath, ap.App.Type, fileId(ap.App.Id))
if err := checkPath(ap.itemPath); err != nil {
logger.Error("create directory path failure, err= %v", err)
}
return ap
}
func (app *AppItem) changeSecret(secret string) {
app.App.Secret = secret
if app.tk != nil {
app.tk.SetSecret(secret)
}
app.appAt.Update()
app.fUpdate <- fApp
}
func (app *AppItem) start() {
var count = 0
sys := system.Exit()
minute := time.NewTicker(time.Minute)
for {
select {
case <-minute.C:
app.checkExpire()
if count%5 == 0 {
//5分钟保存一次
count = 0
app.saveOwner()
}
count++
case f := <-app.fUpdate:
if f == fAll {
app.save()
}
if f == fVMap {
app.saveOwner()
}
if f == fApp {
app.saveApp()
}
case <-sys.Done():
return
}
}
}
func (app *AppItem) checkExpire() {
//自已的值
if app.vMapExpire() {
app.itemUpdated(fVMap)
}
}
func (app *AppItem) itemUpdated(f int) {
app.fUpdate <- f
}
func (app *AppItem) load() {
if !app.conf.IsLocal() {
return
}
if len(app.itemPath) == 0 {
return
}
app.loadOwner()
}
func (app *AppItem) loadOwner() {
filename := app.itemPath + ownerGob
app.vmu.Lock()
defer app.vmu.Unlock()
err := loadFile(filename, func(de *gob.Decoder) error {
return de.Decode(&app.vMap)
})
if err != nil {
zap.L().Error("Token:loadOwner",
zap.String("path", app.itemPath),
zap.String("id", app.App.Id),
zap.String("appId", app.App.AppId),
zap.Error(err))
return
}
app.vMapAt.Complete()
zap.L().Debug("Token:loadOwner",
zap.String("path", app.itemPath),
zap.String("id", app.App.Id),
zap.String("appId", app.App.AppId))
for _, v := range app.vMap {
zap.L().Debug("--Token:loadOwner",
zap.String("data", v.String()))
}
}
func (app *AppItem) saveOwner() {
app.vmu.Lock()
defer app.vmu.Unlock()
if !app.vMapAt.HasUpdate() {
return
}
if err := app.checkPath(); err != nil {
return
}
filename := app.itemPath + ownerGob
err := saveFile(filename, func(en *gob.Encoder) error {
return en.Encode(app.vMap)
})
if err != nil {
zap.L().Error("Token:saveOwner",
zap.String("path", filename),
zap.String("file", fileId(app.App.Id)),
zap.String("id", app.App.Id),
zap.String("appId", app.App.AppId), zap.Error(err))
return
}
app.vMapAt.Complete()
}
func (app *AppItem) saveApp() {
if !app.conf.IsLocal() {
return
}
if !app.appAt.HasUpdate() {
return
}
filename := fmt.Sprintf("%s/%s.gob", appPath, fileId(app.App.Id))
err := saveFile(filename, func(en *gob.Encoder) error {
return en.Encode(app.App)
})
if err != nil {
zap.L().Error("Token:saveApp",
zap.String("path", filename),
zap.String("file", fileId(app.App.Id)),
zap.String("id", app.App.Id),
zap.String("appId", app.App.AppId), zap.Error(err))
return
}
app.appAt.Complete()
}
func (app *AppItem) save() {
app.saveApp()
app.saveOwner()
}
func (app *AppItem) checkPath() error {
if !app.conf.IsLocal() {
return ErrNotSupported
}
return checkPath(app.itemPath)
}
func (app *AppItem) Path() string {
return fileId(app.App.Id)
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/h79/gothird.git
git@gitee.com:h79/gothird.git
h79
gothird
gothird
v1.8.64

搜索帮助

23e8dbc6 1850385 7e0993f3 1850385