小优化

This commit is contained in:
xing 2022-07-28 16:26:04 +08:00
parent 59d9acc4a4
commit eb973a1942

32
main.go
View File

@ -99,16 +99,16 @@ func isContain[T comparable](i T, arr []T) bool {
return false return false
} }
func setMap[T mapT](obj *mapXS[T], key string, v T) { func (r *mapXS[T]) set(k string, v T) {
obj.Lock() r.Lock()
(*obj.mapX)[key] = v (*r.mapX)[k] = v
obj.Unlock() r.Unlock()
} }
func delMap[T mapT](obj *mapXS[T], key string) { func (r *mapXS[T]) del(k string) {
obj.Lock() r.Lock()
delete(*obj.mapX, key) delete(*r.mapX, k)
obj.Unlock() r.Unlock()
} }
type mapT interface { type mapT interface {
@ -185,8 +185,8 @@ func (f *fetchHandler) receiveMsg() {
switch r.msg.Action { switch r.msg.Action {
case "search": case "search":
if t, ok := r.msg.Data.(*setting); ok { if t, ok := r.msg.Data.(*setting); ok {
setMap[string](&f.keyword, r.conn, t.Keyword) f.keyword.set(r.conn, t.Keyword)
setMap[[]string](&f.searchSource, r.conn, t.SearchSource) f.searchSource.set(r.conn, t.SearchSource)
f.handle(r.conn) f.handle(r.conn)
(*f.reloadCron.mapX)[r.conn] <- t.TimeStep (*f.reloadCron.mapX)[r.conn] <- t.TimeStep
} }
@ -324,7 +324,7 @@ func (f *fetchHandler) parseAjax(response *http.Response, source newsource.Sourc
} }
if _, ok := (*f.hadFetchedMap.mapX)[k]; !ok { if _, ok := (*f.hadFetchedMap.mapX)[k]; !ok {
f.hadFetchData = append(f.hadFetchData, fetchData) f.hadFetchData = append(f.hadFetchData, fetchData)
setMap(&f.hadFetchedMap, k, 1) f.hadFetchedMap.set(k, 1)
newF = append(newF, newFetch[i]) newF = append(newF, newFetch[i])
} }
} }
@ -364,7 +364,7 @@ func (f *fetchHandler) parsesDom(html *http.Response, conn string, source newsou
k := conn + "_" + fetchData.Url + "_" + fetchData.Title k := conn + "_" + fetchData.Url + "_" + fetchData.Title
if _, ok := (*f.hadFetchedMap.mapX)[k]; !ok { if _, ok := (*f.hadFetchedMap.mapX)[k]; !ok {
f.hadFetchData = append(f.hadFetchData, fetchData) f.hadFetchData = append(f.hadFetchData, fetchData)
setMap(&f.hadFetchedMap, k, 1) f.hadFetchedMap.set(k, 1)
newFetch = append(newFetch, fetchData) newFetch = append(newFetch, fetchData)
} }
}) })
@ -403,7 +403,7 @@ func (f *fetchHandler) cronFetch(conn string, c chan int) {
} }
t := time.NewTicker(step) t := time.NewTicker(step)
if _, ok := (*f.cronTime.mapX)[conn]; !ok { if _, ok := (*f.cronTime.mapX)[conn]; !ok {
setMap(&f.reloadCron, conn, make(chan int)) f.reloadCron.set(conn, make(chan int))
} }
defer t.Stop() defer t.Stop()
for { for {
@ -411,7 +411,7 @@ func (f *fetchHandler) cronFetch(conn string, c chan int) {
case <-t.C: case <-t.C:
f.handle(conn) f.handle(conn)
case tt := <-(*f.reloadCron.mapX)[conn]: case tt := <-(*f.reloadCron.mapX)[conn]:
setMap(&f.cronTime, conn, time.Duration(tt)*time.Second) f.cronTime.set(conn, time.Duration(tt)*time.Second)
go f.cronFetch(conn, c) go f.cronFetch(conn, c)
return return
case <-c: case <-c:
@ -471,7 +471,7 @@ func main() {
}) })
remote := conn.RemoteAddr().String() remote := conn.RemoteAddr().String()
if _, ok := (*h.connMap.mapX)[remote]; !ok { if _, ok := (*h.connMap.mapX)[remote]; !ok {
setMap(&h.connMap, remote, conn) h.connMap.set(remote, conn)
} }
cc := make(chan int) cc := make(chan int)
go h.cronFetch(remote, cc) go h.cronFetch(remote, cc)
@ -486,7 +486,7 @@ func main() {
err := conn.ReadJSON(&msg.msg) err := conn.ReadJSON(&msg.msg)
if err != nil { if err != nil {
if _, ok := (*h.connMap.mapX)[remote]; ok && !websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) { if _, ok := (*h.connMap.mapX)[remote]; ok && !websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
delMap(&h.connMap, remote) h.connMap.del(remote)
cc <- 1 cc <- 1
return return
} }