File tree Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Expand file tree Collapse file tree 1 file changed +13
-3
lines changed Original file line number Diff line number Diff line change @@ -56,12 +56,18 @@ func NewClient(w http.ResponseWriter, req *http.Request) *Client {
56
56
return c
57
57
}
58
58
59
+ func (c * Client ) Closed () bool {
60
+ c .lock .Lock ()
61
+ defer c .lock .Unlock ()
62
+ return c .closed
63
+ }
64
+
59
65
// Send queues an event to be sent to the client.
60
66
// This does not block until the event has been sent,
61
67
// however it could block if the event queue is full.
62
68
// Returns an error if the Client has disconnected
63
69
func (c * Client ) Send (ev Event ) error {
64
- if c .closed {
70
+ if c .Closed () {
65
71
return io .ErrClosedPipe
66
72
}
67
73
c .events <- ev
@@ -73,7 +79,7 @@ func (c *Client) Send(ev Event) error {
73
79
// Returns true if blocked
74
80
// Returns an error if the Client has disconnected
75
81
func (c * Client ) SendNonBlocking (ev Event ) (bool , error ) {
76
- if c .closed {
82
+ if c .Closed () {
77
83
return false , io .ErrClosedPipe
78
84
}
79
85
select {
@@ -105,7 +111,9 @@ func (c *Client) run() {
105
111
case ev , ok := <- c .events :
106
112
// check for shutdown
107
113
if ! ok {
114
+ c .lock .Lock ()
108
115
c .closed = true
116
+ c .lock .Unlock ()
109
117
c .waiter .Done ()
110
118
return
111
119
}
@@ -119,7 +127,9 @@ func (c *Client) run() {
119
127
c .lock .Unlock ()
120
128
121
129
case <- done :
130
+ c .lock .Lock ()
122
131
c .closed = true
132
+ c .lock .Unlock ()
123
133
c .waiter .Done ()
124
134
return
125
135
}
@@ -131,7 +141,7 @@ func (c *Client) run() {
131
141
func (c * Client ) flush () {
132
142
c .lock .Lock ()
133
143
defer c .lock .Unlock ()
134
- if c .closed {
144
+ if c .closed || c . ctx . Err () != nil {
135
145
return
136
146
}
137
147
c .flushing = nil
You can’t perform that action at this time.
0 commit comments