by NightMKoder · 4y ago · 0 comments
In either case Loom and frameworks that use it are super exciting! I’m looking forward to removing the 20 different thread pools we need to avoid deadlocks and just using the common one in our Clojure apps.
wsHandler := func(w http.ResponseWriter, r \*http.Request) {
conn, err := upgrader.Upgrade(w, r)
if err != nil {
w.SendStatus(http.StatusInternalServerError)
return
}
ctx, ctxCancel := context.WithCancel(context.Background())
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer sync.Done()
for {
msg, err := conn.ReadMessage()
if err != nil {
return
}
}
}()
msgPipe := make(chan []byte, 1024)
wg.Add(1)
go func() {
defer wg.Done()
for {
select {
case <-ctx.Done():
return
case msg := <- msgPipe
if err := conn.WriteMessage(msg); err != nil { return }
}
}
}()
go func() {
<- ctx.Done()
wg.Wait()
close(msgPipe)
}()
}
I like Go, but I found writing WebSocket code very annoying.