2023-08-03 10:34:20 +00:00
|
|
|
package fusenrender
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/gorilla/websocket"
|
|
|
|
"github.com/lni/dragonboat/v4"
|
|
|
|
)
|
|
|
|
|
|
|
|
var UidCreater = NewUniqueId(1)
|
|
|
|
var PopChannel chan []byte = make(chan []byte, 3) // chan *QueueItem = make(chan *QueueItem, 1)
|
|
|
|
|
|
|
|
var upgrader = websocket.Upgrader{
|
|
|
|
ReadBufferSize: 1024,
|
|
|
|
WriteBufferSize: 1024,
|
|
|
|
}
|
|
|
|
|
2023-08-05 19:54:11 +00:00
|
|
|
func HttpStart(ns *dragonboat.NodeHost, port int) {
|
|
|
|
http.HandleFunc("/api/queue/push", pushRenderTaskHandler)
|
|
|
|
http.HandleFunc("/ws/pop/queue", queueHandler)
|
|
|
|
http.HandleFunc("/ws/callback", callbackHandler)
|
|
|
|
log.Printf(":%d", port)
|
|
|
|
http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
|
|
|
|
}
|
|
|
|
|
2023-08-03 10:34:20 +00:00
|
|
|
func queueHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var err error
|
2023-08-04 10:59:53 +00:00
|
|
|
conn, err := upgrader.Upgrade(w, r, nil)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2023-08-05 19:54:11 +00:00
|
|
|
log.Println("建立连接", DequeueHandler.RefCountAdd(1))
|
2023-08-04 10:59:53 +00:00
|
|
|
defer func() {
|
2023-08-05 19:54:11 +00:00
|
|
|
log.Println("退出连接", DequeueHandler.RefCountAdd(-1))
|
2023-08-04 10:59:53 +00:00
|
|
|
}()
|
|
|
|
|
2023-08-03 10:34:20 +00:00
|
|
|
for {
|
|
|
|
|
2023-08-04 10:59:53 +00:00
|
|
|
// msgType, msg, err := conn.ReadMessage()
|
|
|
|
|
|
|
|
// if err != nil {
|
|
|
|
// log.Println(msgType, msg, err)
|
|
|
|
// return
|
|
|
|
// }
|
|
|
|
|
2023-08-03 10:34:20 +00:00
|
|
|
itemdata := <-PopChannel
|
|
|
|
|
|
|
|
// 写回消息
|
|
|
|
err = conn.WriteMessage(websocket.BinaryMessage, itemdata)
|
|
|
|
if err != nil {
|
2023-08-04 10:59:53 +00:00
|
|
|
log.Println(err)
|
|
|
|
var item QueueItem
|
|
|
|
err = item.Decode(itemdata)
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
log.Println("重新回队")
|
2023-08-05 19:54:11 +00:00
|
|
|
stateClient.PushItem(&item)
|
2023-08-03 10:34:20 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// 打印消息
|
2023-08-05 19:54:11 +00:00
|
|
|
fmt.Printf("%s len=%d sent: \n ", conn.RemoteAddr(), len(itemdata))
|
2023-08-03 10:34:20 +00:00
|
|
|
|
|
|
|
// 读取消息
|
|
|
|
// msgType, msg, err := conn.ReadMessage()
|
2023-08-04 10:59:53 +00:00
|
|
|
|
2023-08-03 10:34:20 +00:00
|
|
|
// if err != nil {
|
2023-08-04 10:59:53 +00:00
|
|
|
// log.Println(msgType, msg, err)
|
2023-08-03 10:34:20 +00:00
|
|
|
// return
|
|
|
|
// }
|
2023-08-04 10:59:53 +00:00
|
|
|
|
2023-08-03 10:34:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func callbackHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
conn, _ := upgrader.Upgrade(w, r, nil)
|
|
|
|
|
|
|
|
for {
|
|
|
|
// 读取消息
|
|
|
|
msgType, msg, err := conn.ReadMessage()
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 打印消息
|
|
|
|
fmt.Printf("%s sent: %s\n", conn.RemoteAddr(), string(msg))
|
|
|
|
|
|
|
|
// 写回消息
|
|
|
|
err = conn.WriteMessage(msgType, msg)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-05 19:54:11 +00:00
|
|
|
func pushRenderTaskHandler(w http.ResponseWriter, r *http.Request) {
|
2023-08-03 10:34:20 +00:00
|
|
|
// 1. 读取Body内容
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. 定义结构体
|
|
|
|
|
2023-08-05 19:54:11 +00:00
|
|
|
item := QueueItem{}
|
2023-08-03 10:34:20 +00:00
|
|
|
|
|
|
|
// 3. 解析JSON到结构体
|
2023-08-05 19:54:11 +00:00
|
|
|
err = json.Unmarshal(body, &item)
|
2023-08-03 10:34:20 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2023-08-04 10:59:53 +00:00
|
|
|
|
2023-08-05 19:54:11 +00:00
|
|
|
stateClient.PushItem(&item)
|
|
|
|
}
|
|
|
|
|
|
|
|
type StateClient struct {
|
|
|
|
nh *dragonboat.NodeHost
|
2023-08-03 10:34:20 +00:00
|
|
|
}
|
|
|
|
|
2023-08-05 19:54:11 +00:00
|
|
|
var stateClient *StateClient
|
2023-08-03 10:34:20 +00:00
|
|
|
|
2023-08-05 19:54:11 +00:00
|
|
|
func (cli *StateClient) PushItem(item *QueueItem) {
|
|
|
|
cs := cli.nh.GetNoOPSession(128)
|
2023-08-03 10:34:20 +00:00
|
|
|
cmd := &CmdEnqueue{Command: Command{Group: "test"}}
|
|
|
|
cmd.Item = item
|
|
|
|
data, err := FsPasser.PackToBytes(cmd)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
|
2023-08-05 19:54:11 +00:00
|
|
|
_, err = cli.nh.SyncPropose(ctx, cs, data)
|
2023-08-03 10:34:20 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Println(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// log.Println("enqueue", len(result.Data))
|
|
|
|
cancel()
|
|
|
|
}
|