Skip to content

Commit

Permalink
optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
sdfsdhgjkbmnmxc committed Mar 10, 2021
1 parent 3eb1565 commit d8b385a
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
16 changes: 10 additions & 6 deletions speedup.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,18 @@ func releaseServerConfirm(c *tdproto.ServerConfirm) {
}

func xNewClientPing(confirmId string) []byte {
const (
begin = `{"event":"client.ping","confirm_id":"`
end = `"}`
)
return xConcat( `{"event":"client.ping","confirm_id":"`, confirmId,`"}`)
}

func xNewClientConfirm(confirmId string) []byte {
return xConcat(`{"event":"client.confirm","params":{"confirm_id":"`, confirmId, `"}}`)
}

func xConcat(begin, mid, end string) []byte {
var b bytes.Buffer
b.Grow(len(begin) + len(confirmId) + len(end))
b.Grow(len(begin) + len(mid) + len(end))
b.WriteString(begin)
b.WriteString(confirmId)
b.WriteString(mid)
b.WriteString(end)
return b.Bytes()
}
20 changes: 12 additions & 8 deletions ws.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,21 +194,25 @@ func (w WsSession) inboxLoop() {
return
}

w.session.logger.Println("got:", string(data))
v, err := parser.ParseBytes(data)
if err != nil {
w.fail <- errors.Wrap(err, "invalid json")
w.fail <- errors.Wrapf(err, "invalid json: `%s`", string(data))
return
}

eventName := v.GetStringBytes("event")
w.inbox <- serverEvent{
name: string(eventName),
raw: data,
confirmId := string(v.GetStringBytes("confirm_id"))
if confirmId != "" {
w.SendRaw(xNewClientConfirm(confirmId))
}

confirmId := v.GetStringBytes("confirm_id")
if len(confirmId) > 0 {
w.Send(tdproto.NewClientConfirm(string(confirmId)))
select {
case w.inbox <- serverEvent{
name: string(v.GetStringBytes("event")),
raw: data,
}:
default:
w.fail <- errors.Wrapf(err, "full inbox")
}
}
}

0 comments on commit d8b385a

Please sign in to comment.