Add pprof; use Readdirnames instead of Walk for finding files.

This commit is contained in:
Bill Thiede 2013-09-02 19:42:16 -07:00
parent 00278ba2dd
commit 0ebcc33f55

View File

@ -5,6 +5,7 @@ import (
"flag"
"log"
"net/http"
_ "net/http/pprof"
"os"
"path/filepath"
"sort"
@ -17,6 +18,8 @@ var (
"root directory to watch")
poll = flag.Duration("poll", time.Second*time.Duration(10),
"poll interval between new file check")
addr = flag.String("addr", ":8080",
"address to listen for HTTP connections")
)
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 {
if err != nil {
return err
@ -160,7 +193,9 @@ func (mc *mailCounter) watch(root string, poll time.Duration) error {
if err := mc.fillNeedUpdate(); err != nil {
return err
}
if len(mc.needUpdate) > 0 {
modCnt := len(mc.needUpdate)
if modCnt > 0 {
log.Println(modCnt, "folders need update")
update := map[string]struct{}{}
for _, dir := range mc.needUpdate {
// 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
}
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
}
if err := filepath.Walk(filepath.Join(u, "new"), mc.walk); err != nil {
if err := mc.updateCount(filepath.Join(u, "new")); err != nil {
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
@ -229,7 +270,8 @@ func main() {
mc := newMailCounter()
go func() {
err := http.ListenAndServe(":8080", mc)
http.Handle("/", mc)
err := http.ListenAndServe(*addr, nil)
if err != nil {
log.Fatal(err)
}