simplifying json

master
Jordan Orelli 4 years ago
parent a4c60c2fd8
commit abaaa9e908

@ -163,8 +163,9 @@ func (s *Server) Shutdown() {
log := s.Child("sessions") log := s.Child("sessions")
s.Lock() s.Lock()
if len(s.sessions) > 0 { numSessions := len(s.sessions)
log.Info("broadcasting shutdown to %d active sessions", len(s.sessions)) if numSessions > 0 {
log.Info("broadcasting shutdown to %d active sessions", numSessions)
for id, sn := range s.sessions { for id, sn := range s.sessions {
log.Info("sending done signal to session: %d", id) log.Info("sending done signal to session: %d", id)
sn.done <- true sn.done <- true
@ -174,7 +175,9 @@ func (s *Server) Shutdown() {
} }
s.Unlock() s.Unlock()
log.Info("waiting on connected sessions to shut down") if numSessions > 0 {
log.Info("waiting on %d connected sessions to shut down", numSessions)
}
s.waitOnSessions.Wait() s.waitOnSessions.Wait()
log.Info("all sessions have shut down") log.Info("all sessions have shut down")
}() }()

@ -87,6 +87,7 @@ func (c *Client) readLoop(notifications chan<- Response) {
defer close(notifications) defer close(notifications)
for { for {
c.Info("waiting for a reader frame")
_, r, err := c.conn.NextReader() _, r, err := c.conn.NextReader()
if err != nil { if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure) { if websocket.IsCloseError(err, websocket.CloseNormalClosure) {
@ -130,7 +131,7 @@ func (c *Client) writeLoop() {
select { select {
case p := <-c.outbox: case p := <-c.outbox:
c.lastSeq++ c.lastSeq++
req := NewRequest(c.lastSeq, p.v) req := Request{c.lastSeq, p.v}
w, err := c.conn.NextWriter(websocket.TextMessage) w, err := c.conn.NextWriter(websocket.TextMessage)
if err != nil { if err != nil {

@ -1,15 +1,41 @@
package wire package wire
import (
"encoding/json"
"fmt"
)
type Request struct { type Request struct {
Seq int `json:"seq"` Seq int
Type string `json:"type"` Body Value
Body interface{} `json:"body"` }
func (r Request) MarshalJSON() ([]byte, error) {
return json.Marshal([]interface{}{r.Seq, r.Body.NetTag(), r.Body})
} }
func NewRequest(seq int, v Value) Request { func (r *Request) UnmarshalJSON(b []byte) error {
return Request{ var parts [3]json.RawMessage
Seq: seq, if err := json.Unmarshal(b, &parts); err != nil {
Type: v.NetTag(), return err
Body: v, }
if err := json.Unmarshal(parts[0], &r.Seq); err != nil {
return err
}
var tag string
if err := json.Unmarshal(parts[1], &tag); err != nil {
return err
}
f, ok := registry[tag]
if !ok {
return fmt.Errorf("unknown tag: %q", tag)
}
v := f()
if err := json.Unmarshal(parts[2], v); err != nil {
return err
} }
r.Body = v
return nil
} }

Loading…
Cancel
Save