mirror of
https://github.com/statbate/animated-sniffle.git
synced 2026-08-11 02:12:41 +00:00
update
This commit is contained in:
parent
51ebbd9a8b
commit
c22576a6bf
3 changed files with 29 additions and 25 deletions
16
app/main.go
16
app/main.go
|
|
@ -28,7 +28,7 @@ var (
|
|||
Mysql, Clickhouse *sqlx.DB
|
||||
|
||||
json = jsoniter.ConfigCompatibleWithStandardLibrary
|
||||
|
||||
|
||||
socketServer = make(chan []byte, 100)
|
||||
|
||||
save = make(chan saveData, 100)
|
||||
|
|
@ -92,16 +92,16 @@ func initClickhouse() {
|
|||
}
|
||||
|
||||
func socketHandler() {
|
||||
|
||||
var(
|
||||
err error
|
||||
|
||||
var (
|
||||
err error
|
||||
conn net.Conn
|
||||
)
|
||||
|
||||
|
||||
for {
|
||||
select {
|
||||
case b := <-socketServer:
|
||||
|
||||
|
||||
if conn == nil {
|
||||
conn, err = net.Dial("unix", "/tmp/echo.sock")
|
||||
if err != nil {
|
||||
|
|
@ -109,7 +109,7 @@ func socketHandler() {
|
|||
continue
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
if conn != nil {
|
||||
if _, err = conn.Write(b); err != nil {
|
||||
fmt.Println(err.Error())
|
||||
|
|
@ -117,7 +117,7 @@ func socketHandler() {
|
|||
conn = nil
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
32
app/save.go
32
app/save.go
|
|
@ -69,11 +69,24 @@ func saveDB() {
|
|||
index := make(map[string]int64)
|
||||
|
||||
ticker := time.NewTicker(10 * time.Second)
|
||||
defer ticker.Stop()
|
||||
|
||||
cleanCache := time.NewTicker(12 * time.Hour)
|
||||
defer cleanCache.Stop()
|
||||
|
||||
index = map[string]int64{"hours": int64(hours), "tokens": getSumTokens(), "last": time.Now().Unix()}
|
||||
|
||||
for {
|
||||
select {
|
||||
case <-cleanCache.C:
|
||||
l := len(data)
|
||||
now := time.Now().Unix()
|
||||
for k, v := range data {
|
||||
if now > v.Last+60*60*48 {
|
||||
delete(data, k)
|
||||
}
|
||||
}
|
||||
fmt.Println("Clean map:", l, "=>", len(data))
|
||||
case <-ticker.C:
|
||||
if len(bulk) > 0 {
|
||||
tx, err := Mysql.Begin()
|
||||
|
|
@ -156,27 +169,17 @@ func saveDB() {
|
|||
if minutes >= 5 && now > index["last"]+30 {
|
||||
seconds += minutes * 60
|
||||
msg, err := json.Marshal(struct {
|
||||
Chanel string `json:"chanel"`
|
||||
Index float64 `json:"index"`
|
||||
Chanel string `json:"chanel"`
|
||||
Index float64 `json:"index"`
|
||||
}{
|
||||
Chanel: "bongacams",
|
||||
Index: float64(index["tokens"]) / float64(seconds) * 3600 * 0.025 / 1000,
|
||||
Chanel: "bongacams",
|
||||
Index: float64(index["tokens"]) / float64(seconds) * 3600 * 0.025 / 1000,
|
||||
})
|
||||
if err == nil {
|
||||
socketServer <- msg
|
||||
}
|
||||
index["last"] = now
|
||||
}
|
||||
|
||||
if randInt(0, 10000) == 777 { // 0.001%
|
||||
l := len(data)
|
||||
for k, v := range data {
|
||||
if now > v.Last+60*60*48 {
|
||||
delete(data, k)
|
||||
}
|
||||
}
|
||||
fmt.Println("Clean map:", l, "=>", len(data))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -184,6 +187,7 @@ func saveDB() {
|
|||
func saveLogs() {
|
||||
bulk := []saveLog{}
|
||||
ticker := time.NewTicker(10 * time.Second)
|
||||
defer ticker.Stop()
|
||||
for {
|
||||
select {
|
||||
case <-ticker.C:
|
||||
|
|
|
|||
|
|
@ -81,14 +81,14 @@ func announceCount() {
|
|||
rooms.Count <- 0
|
||||
l := <-rooms.Count
|
||||
msg, err := json.Marshal(struct {
|
||||
Chanel string `json:"chanel"`
|
||||
Count int `json:"count"`
|
||||
Chanel string `json:"chanel"`
|
||||
Count int `json:"count"`
|
||||
}{
|
||||
Chanel: "bongacams",
|
||||
Count: l,
|
||||
})
|
||||
if err == nil {
|
||||
ws.Send <- msg
|
||||
socketServer <- msg
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue