Skip to content

Commit

Permalink
fix ws event pool (#42)
Browse files Browse the repository at this point in the history
  • Loading branch information
markus621 authored Jun 12, 2023
2 parents 23ed772 + f3d5e4f commit f385c82
Show file tree
Hide file tree
Showing 5 changed files with 166 additions and 20 deletions.
2 changes: 2 additions & 0 deletions plugins/web/ws_event.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package web

//go:generate easyjson

import (
"encoding/json"
"sync"
Expand Down
118 changes: 118 additions & 0 deletions plugins/web/ws_event_easyjson.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 31 additions & 0 deletions plugins/web/ws_event_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package web

import (
"encoding/json"
"fmt"
"testing"

"github.com/stretchr/testify/require"
)

func TestUnit_Event(t *testing.T) {
ev := &event{}
err := json.Unmarshal([]byte(`{"e":1001,"u":"1111","d":{"token":"12345","os":"debian"}}`), ev)
require.NoError(t, err)

b, err := json.Marshal(ev)
require.NoError(t, err)
require.Equal(t, string(b), "{\"e\":1001,\"d\":{\"token\":\"12345\",\"os\":\"debian\"},\"u\":\"1111\"}")

ev.Error(fmt.Errorf("error1"))

b, err = json.Marshal(ev)
require.NoError(t, err)
require.Equal(t, string(b), "{\"e\":1001,\"d\":null,\"err\":\"error1\",\"u\":\"1111\"}")

ev.Reset()

b, err = json.Marshal(ev)
require.NoError(t, err)
require.Equal(t, string(b), "{\"e\":0,\"d\":null}")
}
27 changes: 7 additions & 20 deletions plugins/web/ws_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package web
import (
"context"
"encoding/json"
"fmt"
"net/http"
"sync"
"sync/atomic"
Expand Down Expand Up @@ -328,34 +327,22 @@ func (v *wssConn) dataBus() <-chan []byte {

func (v *wssConn) dataHandler(b []byte) {
eventModel(func(ev *event) {
var (
err error
msg string
)
defer func() {
if err != nil {
v.errLog(v.cid, err, "[ws] "+msg)
}
}()
if err = json.Unmarshal(b, ev); err != nil {
msg = "decode message"
if err := json.Unmarshal(b, ev); err != nil {
v.errLog(v.cid, err, "[ws] decode message")
return
}
call, ok := v.event(ev.EventID())
if !ok {
return
}
err = call(ev, v)
if err != nil {
if err := call(ev, v); err != nil {
ev.Error(err)
bb, er := json.Marshal(ev)
if er != nil {
msg = fmt.Sprintf("[ws] call event handler: %d", ev.EventID())
err = errors.Wrap(err, er)
if bb, er := json.Marshal(ev); er != nil {
v.errLog(v.cid, errors.Wrap(err, er), "[ws] call event handler: %d", ev.EventID())
return
} else {
v.Write(bb)
}
err = nil
v.Write(bb)
return
}
})
Expand Down
8 changes: 8 additions & 0 deletions plugins/web/ws_server_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ func (v *wssPool) Create(name string) WebsocketServer {
}
p := newWsServerProvider(v.log, u)
v.pool[name] = p

if err := p.Up(); err != nil {
v.log.WithFields(log.Fields{
"err": err,
"name": name,
}).Errorf("Create Websocket Server in pool")
}

return p
}

Expand Down

0 comments on commit f385c82

Please sign in to comment.