Add pprof; use Readdirnames instead of Walk for finding files.
This commit is contained in:
parent
00278ba2dd
commit
0ebcc33f55
@ -5,6 +5,7 @@ import (
|
|||||||
"flag"
|
"flag"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
_ "net/http/pprof"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"sort"
|
"sort"
|
||||||
@ -17,6 +18,8 @@ var (
|
|||||||
"root directory to watch")
|
"root directory to watch")
|
||||||
poll = flag.Duration("poll", time.Second*time.Duration(10),
|
poll = flag.Duration("poll", time.Second*time.Duration(10),
|
||||||
"poll interval between new file check")
|
"poll interval between new file check")
|
||||||
|
addr = flag.String("addr", ":8080",
|
||||||
|
"address to listen for HTTP connections")
|
||||||
)
|
)
|
||||||
|
|
||||||
func folderFromPath(path string) string {
|
func folderFromPath(path string) string {
|
||||||
@ -103,6 +106,36 @@ func (mc *mailCounter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mc *mailCounter) updateCount(path string) error {
|
||||||
|
dn := filepath.Dir(path)
|
||||||
|
if !isMailDir(dn) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
d, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fi, err := d.Stat()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if !fi.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
folder := folderFromPath(filepath.Dir(path))
|
||||||
|
paths, err := d.Readdirnames(-1)
|
||||||
|
for _, path := range paths {
|
||||||
|
if isNewEmail(path) {
|
||||||
|
mc.counts[folder]++
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func (mc *mailCounter) walk(path string, info os.FileInfo, err error) error {
|
func (mc *mailCounter) walk(path string, info os.FileInfo, err error) error {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@ -160,7 +193,9 @@ func (mc *mailCounter) watch(root string, poll time.Duration) error {
|
|||||||
if err := mc.fillNeedUpdate(); err != nil {
|
if err := mc.fillNeedUpdate(); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if len(mc.needUpdate) > 0 {
|
modCnt := len(mc.needUpdate)
|
||||||
|
if modCnt > 0 {
|
||||||
|
log.Println(modCnt, "folders need update")
|
||||||
update := map[string]struct{}{}
|
update := map[string]struct{}{}
|
||||||
for _, dir := range mc.needUpdate {
|
for _, dir := range mc.needUpdate {
|
||||||
// Store parent directory, which is the IMAP folder
|
// Store parent directory, which is the IMAP folder
|
||||||
@ -171,12 +206,18 @@ func (mc *mailCounter) watch(root string, poll time.Duration) error {
|
|||||||
mc.counts[folder] = 0
|
mc.counts[folder] = 0
|
||||||
}
|
}
|
||||||
for u := range update {
|
for u := range update {
|
||||||
if err := filepath.Walk(filepath.Join(u, "cur"), mc.walk); err != nil {
|
if err := mc.updateCount(filepath.Join(u, "cur")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if err := filepath.Walk(filepath.Join(u, "new"), mc.walk); err != nil {
|
if err := mc.updateCount(filepath.Join(u, "new")); err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
//if err := filepath.Walk(filepath.Join(u, "cur"), mc.walk); err != nil {
|
||||||
|
// return err
|
||||||
|
//}
|
||||||
|
//if err := filepath.Walk(filepath.Join(u, "new"), mc.walk); err != nil {
|
||||||
|
// return err
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mc.lastrun = thisrun
|
mc.lastrun = thisrun
|
||||||
@ -229,7 +270,8 @@ func main() {
|
|||||||
|
|
||||||
mc := newMailCounter()
|
mc := newMailCounter()
|
||||||
go func() {
|
go func() {
|
||||||
err := http.ListenAndServe(":8080", mc)
|
http.Handle("/", mc)
|
||||||
|
err := http.ListenAndServe(*addr, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user