Skip to content

Commit

Permalink
update lazymap
Browse files Browse the repository at this point in the history
add logging
  • Loading branch information
vintikzzz committed Dec 12, 2024
1 parent af0eedc commit 19a1533
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 16 deletions.
7 changes: 3 additions & 4 deletions services/content_prober.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func RegisterContentProberFlags(f []cli.Flag) []cli.Flag {
}

type ContentProbe struct {
lazymap.LazyMap
lazymap.LazyMap[*cp.ProbeReply]
host string
port int
timeout int
Expand All @@ -56,18 +56,17 @@ func NewContentProbe(c *cli.Context) *ContentProbe {
host: c.String(contentProberHostFlag),
port: c.Int(contentProberPortFlag),
timeout: c.Int(contentProberTimeoutFlag),
LazyMap: lazymap.New(&lazymap.Config{
LazyMap: lazymap.New[*cp.ProbeReply](&lazymap.Config{
Expire: 30 * time.Minute,
ErrorExpire: 10 * time.Second,
}),
}
}

func (s *ContentProbe) Get(input string, out string) (*cp.ProbeReply, error) {
pr, err := s.LazyMap.Get(input+out, func() (interface{}, error) {
return s.LazyMap.Get(input+out, func() (*cp.ProbeReply, error) {
return s.get(input, out)
})
return pr.(*cp.ProbeReply), err
}

func (s *ContentProbe) get(input string, out string) (pr *cp.ProbeReply, err error) {
Expand Down
20 changes: 9 additions & 11 deletions services/touch_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,24 @@ import (
)

type TouchMap struct {
lazymap.LazyMap
lazymap.LazyMap[bool]
}

func NewTouchMap() *TouchMap {
return &TouchMap{
LazyMap: lazymap.New(&lazymap.Config{
LazyMap: lazymap.New[bool](&lazymap.Config{
Expire: 30 * time.Second,
}),
}
}

func (s *TouchMap) touch(path string) error {
func (s *TouchMap) touch(path string) (bool, error) {
f := path + ".touch"
_, err := os.Stat(f)
if os.IsNotExist(err) {
file, err := os.Create(f)
if err != nil {
return err
return false, err
}
defer func(file *os.File) {
_ = file.Close()
Expand All @@ -34,16 +34,14 @@ func (s *TouchMap) touch(path string) error {
currentTime := time.Now().Local()
err = os.Chtimes(f, currentTime, currentTime)
if err != nil {
return err
return false, err
}
}
return nil
return true, nil
}

func (s *TouchMap) Touch(path string) error {
_, err := s.LazyMap.Get(path, func() (interface{}, error) {
err := s.touch(path)
return nil, err
func (s *TouchMap) Touch(path string) (bool, error) {
return s.LazyMap.Get(path, func() (bool, error) {
return s.touch(path)
})
return err
}
2 changes: 1 addition & 1 deletion services/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ func (s *Web) waitHandler(next http.Handler) http.Handler {
func (s *Web) touchHandler(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
out := r.Context().Value(OutputDirContext).(string)
_ = s.touchMap.Touch(out)
_, _ = s.touchMap.Touch(out)
next.ServeHTTP(w, r)
})
}
Expand Down

0 comments on commit 19a1533

Please sign in to comment.