Im real bad at this whole goroutine mutex shit but getting better!
This commit is contained in:
parent
8fed511607
commit
bc3a365dea
5 changed files with 23 additions and 38 deletions
|
@ -2,7 +2,6 @@ package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/tls"
|
"crypto/tls"
|
||||||
"log"
|
|
||||||
"net/smtp"
|
"net/smtp"
|
||||||
"os"
|
"os"
|
||||||
)
|
)
|
||||||
|
|
57
lib/sse.go
57
lib/sse.go
|
@ -92,53 +92,42 @@ func SetSSEHeaders(c echo.Context) {
|
||||||
func HandleIncomingMessages(c echo.Context, pubsub pubsub.PubSubMessage, client chan string) {
|
func HandleIncomingMessages(c echo.Context, pubsub pubsub.PubSubMessage, client chan string) {
|
||||||
if c.Response().Writer == nil {
|
if c.Response().Writer == nil {
|
||||||
LogError.Printf("Cannot handle incoming messages: ResponseWriter is nil")
|
LogError.Printf("Cannot handle incoming messages: ResponseWriter is nil")
|
||||||
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
|
||||||
defer cancel()
|
|
||||||
|
|
||||||
go func() {
|
|
||||||
<-c.Request().Context().Done()
|
|
||||||
cancel()
|
|
||||||
}()
|
|
||||||
|
|
||||||
var mutex sync.Mutex
|
var mutex sync.Mutex
|
||||||
|
|
||||||
for {
|
for {
|
||||||
select {
|
msg, err := pubsub.ReceiveMessage(c.Request().Context())
|
||||||
case <-ctx.Done():
|
if err != nil {
|
||||||
|
if err == context.Canceled {
|
||||||
|
// The client has disconnected. Stop trying to send messages.
|
||||||
|
return
|
||||||
|
}
|
||||||
|
LogError.Printf("Failed to receive message: %v", err)
|
||||||
return
|
return
|
||||||
default:
|
}
|
||||||
msg, err := pubsub.ReceiveMessage(ctx)
|
|
||||||
if err != nil {
|
|
||||||
LogError.Printf("Failed to receive message: %v", err)
|
|
||||||
|
|
||||||
|
data := fmt.Sprintf("data: %s\n\n", msg.Payload)
|
||||||
|
|
||||||
|
mutex.Lock()
|
||||||
|
if c.Response().Writer != nil {
|
||||||
|
_, err := c.Response().Write([]byte(data))
|
||||||
|
if err != nil {
|
||||||
|
LogError.Printf("Failed to write message: %v", err)
|
||||||
|
mutex.Unlock()
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
data := fmt.Sprintf("data: %s\n\n", msg.Payload)
|
flusher, ok := c.Response().Writer.(http.Flusher)
|
||||||
|
if ok {
|
||||||
mutex.Lock()
|
flusher.Flush()
|
||||||
if c.Response().Writer != nil {
|
|
||||||
_, err = c.Response().Write([]byte(data))
|
|
||||||
if err != nil {
|
|
||||||
LogError.Printf("Failed to write message: %v", err)
|
|
||||||
mutex.Unlock()
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
flusher, ok := c.Response().Writer.(http.Flusher)
|
|
||||||
if ok {
|
|
||||||
flusher.Flush()
|
|
||||||
} else {
|
|
||||||
LogError.Println("Failed to flush: ResponseWriter does not implement http.Flusher")
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
LogError.Println("Failed to write: ResponseWriter is nil")
|
LogError.Println("Failed to flush: ResponseWriter does not implement http.Flusher")
|
||||||
}
|
}
|
||||||
mutex.Unlock()
|
} else {
|
||||||
|
LogError.Println("Failed to write: ResponseWriter is nil")
|
||||||
}
|
}
|
||||||
|
mutex.Unlock()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
package lib
|
package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"os"
|
"os"
|
||||||
|
|
||||||
|
|
|
@ -2,7 +2,6 @@ package lib
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"html/template"
|
"html/template"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
|
1
main.go
1
main.go
|
@ -5,7 +5,6 @@ import (
|
||||||
"embed"
|
"embed"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"log"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue