-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathconn.go
104 lines (85 loc) · 1.85 KB
/
conn.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
package etp
import (
"context"
"fmt"
"net/http"
"github.com/coder/websocket"
"github.com/txix-open/etp/v4/bpool"
"github.com/txix-open/etp/v4/internal"
"github.com/txix-open/etp/v4/msg"
"github.com/txix-open/etp/v4/store"
)
type Conn struct {
id string
request *http.Request
ws *websocket.Conn
data *store.Store
acks *internal.Acks
}
func newConn(
id string,
request *http.Request,
ws *websocket.Conn,
) *Conn {
return &Conn{
id: id,
request: request,
ws: ws,
data: store.New(),
acks: internal.NewAcks(),
}
}
func (c *Conn) Id() string {
return c.id
}
func (c *Conn) HttpRequest() *http.Request {
return c.request
}
func (c *Conn) Data() *store.Store {
return c.data
}
func (c *Conn) Emit(ctx context.Context, event string, data []byte) error {
message := msg.Event{
Name: event,
AckId: 0,
Data: data,
}
return c.emit(ctx, message)
}
func (c *Conn) EmitWithAck(ctx context.Context, event string, data []byte) ([]byte, error) {
ack := c.acks.NextAck()
defer c.acks.DeleteAck(ack.Id())
message := msg.Event{
Name: event,
AckId: ack.Id(),
Data: data,
}
err := c.emit(ctx, message)
if err != nil {
return nil, err
}
response, err := ack.Wait(ctx)
if err != nil {
return nil, fmt.Errorf("failed to wait ack: %w", err)
}
return response, nil
}
func (c *Conn) Ping(ctx context.Context) error {
return c.ws.Ping(ctx)
}
func (c *Conn) Close() error {
return c.ws.Close(websocket.StatusNormalClosure, "")
}
func (c *Conn) emit(ctx context.Context, event msg.Event) error {
buff := bpool.Get()
defer bpool.Put(buff)
msg.EncodeEvent(buff, event)
err := c.ws.Write(ctx, websocket.MessageText, buff.Bytes())
if err != nil {
return fmt.Errorf("failed to write event: %w", err)
}
return nil
}
func (c *Conn) notifyAck(ackId uint64, data []byte) {
c.acks.NotifyAck(ackId, data)
}