forked from davidfowl/signalr-ports
-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamer.go
57 lines (52 loc) · 1.28 KB
/
streamer.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
package signalr
import (
"reflect"
"sync"
)
func newStreamer(conn hubConnection) *streamer {
return &streamer{make(map[string]chan bool), sync.Mutex{}, conn}
}
type streamer struct {
streamCancelChans map[string]chan bool
sccMutex sync.Mutex
conn hubConnection
}
func (s *streamer) Start(invocationID string, reflectedChannel reflect.Value) {
cancelChan := make(chan bool)
s.sccMutex.Lock()
defer s.sccMutex.Unlock()
s.streamCancelChans[invocationID] = cancelChan
go func(cancelChan chan bool) {
defer func() {
s.sccMutex.Lock()
defer s.sccMutex.Unlock()
delete(s.streamCancelChans, invocationID)
close(cancelChan)
}()
for {
// Waits for channel, so might hang
if chanResult, ok := reflectedChannel.Recv(); ok {
select {
case <-cancelChan:
s.conn.Completion(invocationID, nil, "")
return
default:
}
s.conn.StreamItem(invocationID, chanResult.Interface())
} else {
s.conn.Completion(invocationID, nil, "")
break
}
}
}(cancelChan)
}
func (s *streamer) Stop(invocationID string) {
// in goroutine, because cancel might not be read when stream producer hangs
go func() {
s.sccMutex.Lock()
defer s.sccMutex.Unlock()
if cancel, ok := s.streamCancelChans[invocationID]; ok {
cancel <- true
}
}()
}