simplify title setter with anon func
This commit is contained in:
@@ -76,14 +76,17 @@ func (s *DataStore) Create(comment store.Comment) (commentID string, err error)
|
||||
return "", ErrRestrictedWordsFound
|
||||
}
|
||||
|
||||
// keep input title and set to extracted if missing
|
||||
if s.TitleExtractor != nil && comment.PostTitle == "" {
|
||||
if title, err := s.TitleExtractor.Get(comment.Locator.URL); err == nil {
|
||||
comment.PostTitle = title
|
||||
} else {
|
||||
log.Printf("[WARN] failed to set title, %v", err)
|
||||
func() { // keep input title and set to extracted if missing
|
||||
if s.TitleExtractor == nil || comment.PostTitle != "" {
|
||||
return
|
||||
}
|
||||
}
|
||||
title, e := s.TitleExtractor.Get(comment.Locator.URL)
|
||||
if e != nil {
|
||||
log.Printf("[WARN] failed to set title, %v", e)
|
||||
return
|
||||
}
|
||||
comment.PostTitle = title
|
||||
}()
|
||||
|
||||
return s.Interface.Create(comment)
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ func (t *TitleExtractor) Get(url string) (string, error) {
|
||||
return title, nil
|
||||
})
|
||||
|
||||
// on error save result (empty strung) to cache too but
|
||||
// on error save result (empty string) to cache too and return "" title
|
||||
if err != nil {
|
||||
_, _ = t.cache.Get(url, func() (lcw.Value, error) { return "", nil })
|
||||
return "", err
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
@@ -84,7 +85,7 @@ func TestTitle_GetConcurrent(t *testing.T) {
|
||||
|
||||
for i := 0; i < 100; i++ {
|
||||
i := i
|
||||
g.Go(func() {
|
||||
g.Go(func(_ context.Context) {
|
||||
title, err := ex.Get(ts.URL + "/good/" + strconv.Itoa(i))
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, "blah 123 "+"/good/"+strconv.Itoa(i), title)
|
||||
|
||||
Reference in New Issue
Block a user