小优化

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
}
func setMap[T mapT](obj *mapXS[T], key string, v T) {
obj.Lock()
(*obj.mapX)[key] = v
obj.Unlock()
func (r *mapXS[T]) set(k string, v T) {
r.Lock()
(*r.mapX)[k] = v
r.Unlock()
}
func delMap[T mapT](obj *mapXS[T], key string) {
obj.Lock()
delete(*obj.mapX, key)
obj.Unlock()
func (r *mapXS[T]) del(k string) {
r.Lock()
delete(*r.mapX, k)
r.Unlock()
}
type mapT interface {
@ -185,8 +185,8 @@ func (f *fetchHandler) receiveMsg() {
switch r.msg.Action {
case "search":
if t, ok := r.msg.Data.(*setting); ok {
setMap[string](&f.keyword, r.conn, t.Keyword)
setMap[[]string](&f.searchSource, r.conn, t.SearchSource)
f.keyword.set(r.conn, t.Keyword)
f.searchSource.set(r.conn, t.SearchSource)
f.handle(r.conn)
(*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 {
f.hadFetchData = append(f.hadFetchData, fetchData)
setMap(&f.hadFetchedMap, k, 1)
f.hadFetchedMap.set(k, 1)
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
if _, ok := (*f.hadFetchedMap.mapX)[k]; !ok {
f.hadFetchData = append(f.hadFetchData, fetchData)
setMap(&f.hadFetchedMap, k, 1)
f.hadFetchedMap.set(k, 1)
newFetch = append(newFetch, fetchData)
}
})
@ -403,7 +403,7 @@ func (f *fetchHandler) cronFetch(conn string, c chan int) {
}
t := time.NewTicker(step)
if _, ok := (*f.cronTime.mapX)[conn]; !ok {
setMap(&f.reloadCron, conn, make(chan int))
f.reloadCron.set(conn, make(chan int))
}
defer t.Stop()
for {
@ -411,7 +411,7 @@ func (f *fetchHandler) cronFetch(conn string, c chan int) {
case <-t.C:
f.handle(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)
return
case <-c:
@ -471,7 +471,7 @@ func main() {
})
remote := conn.RemoteAddr().String()
if _, ok := (*h.connMap.mapX)[remote]; !ok {
setMap(&h.connMap, remote, conn)
h.connMap.set(remote, conn)
}
cc := make(chan int)
go h.cronFetch(remote, cc)
@ -486,7 +486,7 @@ func main() {
err := conn.ReadJSON(&msg.msg)
if err != nil {
if _, ok := (*h.connMap.mapX)[remote]; ok && !websocket.IsUnexpectedCloseError(err, websocket.CloseGoingAway) {
delMap(&h.connMap, remote)
h.connMap.del(remote)
cc <- 1
return
}