This commit is contained in:
poiuty 2023-05-14 13:55:57 +03:00
parent e5a0cb01f9
commit 9df4cb21b3
3 changed files with 39 additions and 36 deletions

View file

@ -28,7 +28,7 @@ var (
Mysql, Clickhouse *sqlx.DB Mysql, Clickhouse *sqlx.DB
json = jsoniter.ConfigCompatibleWithStandardLibrary json = jsoniter.ConfigCompatibleWithStandardLibrary
socketServer = make(chan []byte, 100) socketServer = make(chan []byte, 100)
save = make(chan saveData, 100) save = make(chan saveData, 100)
@ -92,16 +92,16 @@ func initClickhouse() {
} }
func socketHandler() { func socketHandler() {
var( var (
err error err error
conn net.Conn conn net.Conn
) )
for { for {
select { select {
case b := <-socketServer: case b := <-socketServer:
if conn == nil { if conn == nil {
conn, err = net.Dial("unix", "/tmp/echo.sock") conn, err = net.Dial("unix", "/tmp/echo.sock")
if err != nil { if err != nil {
@ -109,7 +109,7 @@ func socketHandler() {
continue continue
} }
} }
if conn != nil { if conn != nil {
if _, err = conn.Write(b); err != nil { if _, err = conn.Write(b); err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
@ -117,7 +117,7 @@ func socketHandler() {
conn = nil conn = nil
} }
} }
} }
} }
} }

View file

@ -69,11 +69,24 @@ func saveDB() {
index := make(map[string]int64) index := make(map[string]int64)
ticker := time.NewTicker(10 * time.Second) 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()} index = map[string]int64{"hours": int64(hours), "tokens": getSumTokens(), "last": time.Now().Unix()}
for { for {
select { 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: case <-ticker.C:
if len(bulk) > 0 { if len(bulk) > 0 {
tx, err := Mysql.Begin() tx, err := Mysql.Begin()
@ -156,27 +169,17 @@ func saveDB() {
if minutes >= 5 && now > index["last"]+30 { if minutes >= 5 && now > index["last"]+30 {
seconds += minutes * 60 seconds += minutes * 60
msg, err := json.Marshal(struct { msg, err := json.Marshal(struct {
Chanel string `json:"chanel"` Chanel string `json:"chanel"`
Index float64 `json:"index"` Index float64 `json:"index"`
}{ }{
Chanel: "stripchat", Chanel: "stripchat",
Index: float64(index["tokens"]) / float64(seconds) * 3600 * 0.05 / 1000, Index: float64(index["tokens"]) / float64(seconds) * 3600 * 0.05 / 1000,
}) })
if err == nil { if err == nil {
socketServer <- msg socketServer <- msg
} }
index["last"] = now 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() { func saveLogs() {
bulk := []saveLog{} bulk := []saveLog{}
ticker := time.NewTicker(10 * time.Second) ticker := time.NewTicker(10 * time.Second)
defer ticker.Stop()
for { for {
select { select {
case <-ticker.C: case <-ticker.C:

View file

@ -4,9 +4,9 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"github.com/gorilla/websocket" "github.com/gorilla/websocket"
"os/exec"
"net/http" "net/http"
"net/url" "net/url"
"os/exec"
"regexp" "regexp"
"strconv" "strconv"
"strings" "strings"
@ -108,11 +108,11 @@ func announceCount() {
rooms.Count <- 0 rooms.Count <- 0
l := <-rooms.Count l := <-rooms.Count
msg, err := json.Marshal(struct { msg, err := json.Marshal(struct {
Chanel string `json:"chanel"` Chanel string `json:"chanel"`
Count int `json:"count"` Count int `json:"count"`
}{ }{
Chanel: "stripchat", Chanel: "stripchat",
Count: l, Count: l,
}) })
if err == nil { if err == nil {
socketServer <- msg socketServer <- msg
@ -121,17 +121,16 @@ func announceCount() {
} }
func getToken(room string) string { func getToken(room string) string {
cmd := exec.Command("/home/stat/python/test.py", "https://stripchat.com/api/front/v2/config/data?requestPath="+room) cmd := exec.Command("/home/stat/python/test.py", "https://stripchat.com/api/front/v2/config/data?requestPath="+room)
stdout, err := cmd.Output() stdout, err := cmd.Output()
if err != nil { if err != nil {
fmt.Println(err.Error()) fmt.Println(err.Error())
return "cant exec py" return "cant exec py"
} }
//fmt.Println(string(stdout))
//fmt.Println(string(stdout))
re := regexp.MustCompile(`"websocketUrl":"*(.*?)\s*"`) re := regexp.MustCompile(`"websocketUrl":"*(.*?)\s*"`)
m := re.FindSubmatch(stdout) m := re.FindSubmatch(stdout)
@ -162,7 +161,7 @@ func xWorker(workerData Info) {
if workerData.Server == "" { if workerData.Server == "" {
workerData.Server = getToken(workerData.room) workerData.Server = getToken(workerData.room)
} }
if len(workerData.Server) < 50 { if len(workerData.Server) < 50 {
fmt.Println(workerData.Server, workerData.room) fmt.Println(workerData.Server, workerData.room)
return return