style(go-pkg): rename pkg

This commit is contained in:
pyihe
2022-03-04 17:24:23 +08:00
parent 520fd7269b
commit cd80673d8b
61 changed files with 751 additions and 1006 deletions

64
monitorpkg/monitor.go Normal file
View File

@@ -0,0 +1,64 @@
package monitorpkg
import (
"errors"
"sync"
"time"
)
type (
Monitor interface {
AddFile(path string, spec string, handler func()) error
DelFile(path string) error
}
myMonitor struct {
sync.RWMutex
fs Files
}
Files map[string]*myFile
)
func (m *myMonitor) init() {
m.fs = make(map[string]*myFile)
}
func NewMonitor() Monitor {
mon := new(myMonitor)
mon.init()
return mon
}
func (m *myMonitor) AddFile(path string, spec string, handler func()) error {
m.Lock()
defer m.Unlock()
if handler == nil {
return errors.New("handler cannot be nil")
}
if _, ok := m.fs[path]; ok {
return errors.New("already exist")
}
f := &myFile{
spec: spec,
filePath: path,
updateOn: time.Now().Unix(),
handle: handler,
}
m.fs[path] = f
f.init()
return nil
}
func (m *myMonitor) DelFile(path string) error {
m.Lock()
defer m.Unlock()
f, ok := m.fs[path]
if !ok {
return nil
}
f.c.Stop()
f.c.Remove(f.id)
delete(m.fs, path)
return nil
}