mirror of
https://github.com/Kioubit/ColorPing
synced 2024-12-12 17:40:41 +08:00
Various optimizations
This commit is contained in:
parent
2e9e8e2b42
commit
8f2ff955e9
4 changed files with 28 additions and 13 deletions
4
go.mod
4
go.mod
|
@ -1,7 +1,7 @@
|
||||||
module ColorPing
|
module ColorPing
|
||||||
|
|
||||||
go 1.20
|
go 1.22.4
|
||||||
|
|
||||||
require github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
|
require github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8
|
||||||
|
|
||||||
require golang.org/x/sys v0.6.0 // indirect
|
require golang.org/x/sys v0.21.0 // indirect
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -1,4 +1,4 @@
|
||||||
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 h1:TG/diQgUe0pntT/2D9tmUCz4VNwm9MfrtPr0SU2qSX8=
|
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8 h1:TG/diQgUe0pntT/2D9tmUCz4VNwm9MfrtPr0SU2qSX8=
|
||||||
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8/go.mod h1:P5HUIBuIWKbyjl083/loAegFkfbFNx5i2qEP4CNbm7E=
|
github.com/songgao/water v0.0.0-20200317203138-2b4b6d7c09d8/go.mod h1:P5HUIBuIWKbyjl083/loAegFkfbFNx5i2qEP4CNbm7E=
|
||||||
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
|
golang.org/x/sys v0.21.0 h1:rF+pYz3DAGSQAxAu1CbC7catZg4ebC4UIeIhKxBZvws=
|
||||||
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
golang.org/x/sys v0.21.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
|
||||||
|
|
23
http.go
23
http.go
|
@ -50,6 +50,9 @@ func getClientID() uint32 {
|
||||||
func clearClients() {
|
func clearClients() {
|
||||||
clientMutex.Lock()
|
clientMutex.Lock()
|
||||||
defer clientMutex.Unlock()
|
defer clientMutex.Unlock()
|
||||||
|
for _, c := range clients {
|
||||||
|
close(c.channel)
|
||||||
|
}
|
||||||
clients = make(map[uint32]*client)
|
clients = make(map[uint32]*client)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -59,7 +62,7 @@ func httpServer() {
|
||||||
htmlTemplate = template.Must(template.ParseFS(embedFS, "template.html"))
|
htmlTemplate = template.Must(template.ParseFS(embedFS, "template.html"))
|
||||||
http.HandleFunc("/stream", stream)
|
http.HandleFunc("/stream", stream)
|
||||||
http.HandleFunc("/", serveRoot)
|
http.HandleFunc("/", serveRoot)
|
||||||
err = http.ListenAndServe("0.0.0.0:9090", nil)
|
err = http.ListenAndServe(":9090", nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalln(err)
|
log.Fatalln(err)
|
||||||
}
|
}
|
||||||
|
@ -157,7 +160,7 @@ func streamServer() {
|
||||||
|
|
||||||
dataInitial, dataUpdate := getPicture(requiresInitial, requiresUpdate)
|
dataInitial, dataUpdate := getPicture(requiresInitial, requiresUpdate)
|
||||||
|
|
||||||
for _, v := range clients {
|
for clientID, v := range clients {
|
||||||
if v.state == INITIAL {
|
if v.state == INITIAL {
|
||||||
v.state = ACTIVE
|
v.state = ACTIVE
|
||||||
select {
|
select {
|
||||||
|
@ -170,6 +173,9 @@ func streamServer() {
|
||||||
select {
|
select {
|
||||||
case v.channel <- dataUpdate:
|
case v.channel <- dataUpdate:
|
||||||
default:
|
default:
|
||||||
|
// Client cannot keep up
|
||||||
|
close(v.channel)
|
||||||
|
delete(clients, clientID)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -189,7 +195,6 @@ func stream(w http.ResponseWriter, r *http.Request) {
|
||||||
w.Header().Set("Content-Type", "text/event-stream")
|
w.Header().Set("Content-Type", "text/event-stream")
|
||||||
w.Header().Set("Cache-Control", "no-cache")
|
w.Header().Set("Cache-Control", "no-cache")
|
||||||
w.Header().Set("Connection", "keep-alive")
|
w.Header().Set("Connection", "keep-alive")
|
||||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
|
||||||
messageChan := make(chan string, 40)
|
messageChan := make(chan string, 40)
|
||||||
id := getClientID()
|
id := getClientID()
|
||||||
newClient := client{
|
newClient := client{
|
||||||
|
@ -199,18 +204,24 @@ func stream(w http.ResponseWriter, r *http.Request) {
|
||||||
clientMutex.Lock()
|
clientMutex.Lock()
|
||||||
clients[id] = &newClient
|
clients[id] = &newClient
|
||||||
clientMutex.Unlock()
|
clientMutex.Unlock()
|
||||||
|
|
||||||
|
// For when clients are removed prior to connection closed, to avoid a call to delete(clients, id)
|
||||||
|
var channelClosedFirst = false
|
||||||
go func() {
|
go func() {
|
||||||
// Listen for connection close
|
// Listen for connection close
|
||||||
<-r.Context().Done()
|
<-r.Context().Done()
|
||||||
clientMutex.Lock()
|
clientMutex.Lock()
|
||||||
close(messageChan)
|
if !channelClosedFirst {
|
||||||
delete(clients, id)
|
delete(clients, id)
|
||||||
|
}
|
||||||
|
close(messageChan)
|
||||||
clientMutex.Unlock()
|
clientMutex.Unlock()
|
||||||
}()
|
}()
|
||||||
|
|
||||||
for {
|
for {
|
||||||
data := <-messageChan
|
data, ok := <-messageChan
|
||||||
if data == "" {
|
if !ok {
|
||||||
|
channelClosedFirst = true
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
_, _ = w.Write([]byte(data))
|
_, _ = w.Write([]byte(data))
|
||||||
|
|
10
main.go
10
main.go
|
@ -25,7 +25,7 @@ func main() {
|
||||||
go startInterface(packetChan)
|
go startInterface(packetChan)
|
||||||
go streamServer()
|
go streamServer()
|
||||||
fmt.Println("Kioubit ColorPing started")
|
fmt.Println("Kioubit ColorPing started")
|
||||||
fmt.Println("Interface name:", interfaceName)
|
fmt.Println("Interface name:", interfaceName, "HTTP server port: 9090")
|
||||||
httpServer()
|
httpServer()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -41,6 +41,10 @@ func prePopulatePixelArray() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var pktPool = sync.Pool{
|
||||||
|
New: func() interface{} { return make([]byte, 2000) },
|
||||||
|
}
|
||||||
|
|
||||||
func startInterface(packetChan chan *[]byte) {
|
func startInterface(packetChan chan *[]byte) {
|
||||||
config := water.Config{
|
config := water.Config{
|
||||||
DeviceType: water.TUN,
|
DeviceType: water.TUN,
|
||||||
|
@ -52,7 +56,7 @@ func startInterface(packetChan chan *[]byte) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for {
|
for {
|
||||||
packet := make([]byte, 2000)
|
packet := pktPool.Get().([]byte)
|
||||||
n, err := iFace.Read(packet)
|
n, err := iFace.Read(packet)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -99,7 +103,7 @@ func packetHandler(packetChan chan *[]byte) {
|
||||||
obj.changed = true
|
obj.changed = true
|
||||||
}
|
}
|
||||||
obj.Unlock()
|
obj.Unlock()
|
||||||
|
pktPool.Put(*packet)
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue