forked from antage/eventsource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consumer.go
144 lines (124 loc) · 2.73 KB
/
consumer.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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package eventsource
import (
"compress/gzip"
"io"
"net"
"net/http"
"strings"
"time"
)
type consumer struct {
subscribeKey string
sessionKey string
conn io.WriteCloser
es *eventSource
in chan []byte
staled bool
}
type gzipConn struct {
net.Conn
*gzip.Writer
}
func (gc gzipConn) Write(b []byte) (int, error) {
n, err := gc.Writer.Write(b)
if err != nil {
return n, err
}
return n, gc.Writer.Flush()
}
func (gc gzipConn) Close() error {
err := gc.Writer.Close()
if err != nil {
return err
}
return gc.Conn.Close()
}
func newConsumer(resp http.ResponseWriter, req *http.Request, es *eventSource) (*consumer, error) {
conn, _, err := resp.(http.Hijacker).Hijack()
if err != nil {
return nil, err
}
subscribeKey := ""
sessionKey := ""
if es.getConsumerSessionKey != nil {
sessionKey = es.getConsumerSessionKey(req)
}
if es.getConsumerSubscribeKey != nil {
subscribeKey = es.getConsumerSubscribeKey(req)
}
consumer := &consumer{
subscribeKey: subscribeKey,
sessionKey: sessionKey,
conn: conn,
es: es,
in: make(chan []byte, 10),
staled: false,
}
_, err = conn.Write([]byte("HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\n"))
if err != nil {
conn.Close()
return nil, err
}
_, err = conn.Write([]byte("Vary: Accept-Encoding\r\n"))
if err != nil {
conn.Close()
return nil, err
}
if es.gzip && (req == nil || strings.Contains(req.Header.Get("Accept-Encoding"), "gzip")) {
_, err = conn.Write([]byte("Content-Encoding: gzip\r\n"))
if err != nil {
conn.Close()
return nil, err
}
consumer.conn = gzipConn{conn, gzip.NewWriter(conn)}
}
if es.customHeadersFunc != nil {
for _, header := range es.customHeadersFunc(req) {
_, err = conn.Write(header)
if err != nil {
conn.Close()
return nil, err
}
_, err = conn.Write([]byte("\r\n"))
if err != nil {
conn.Close()
return nil, err
}
}
}
_, err = conn.Write([]byte("\r\n"))
if err != nil {
conn.Close()
return nil, err
}
go func() {
idleTimer := time.NewTimer(es.idleTimeout)
defer idleTimer.Stop()
for {
select {
case message, open := <-consumer.in:
if !open {
consumer.conn.Close()
return
}
conn.SetWriteDeadline(time.Now().Add(consumer.es.timeout))
_, err := consumer.conn.Write(message)
if err != nil {
netErr, ok := err.(net.Error)
if !ok || !netErr.Timeout() || consumer.es.closeOnTimeout {
consumer.staled = true
consumer.conn.Close()
consumer.es.staled <- consumer
return
}
}
idleTimer.Reset(es.idleTimeout)
case <-idleTimer.C:
consumer.conn.Close()
consumer.es.staled <- consumer
return
}
}
}()
return consumer, nil
}