-
Notifications
You must be signed in to change notification settings - Fork 49
/
wait_status.go
101 lines (88 loc) · 2.69 KB
/
wait_status.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
package irma
import (
"context"
"strings"
"time"
sseclient "github.com/sietseringers/go-sse"
)
const pollInterval = 1000 * time.Millisecond
func WaitStatus(transport *HTTPTransport, initialStatus ServerStatus, statuschan chan ServerStatus, errorchan chan error) {
if err := subscribeSSE(transport, statuschan, errorchan, false); err != nil {
go poll(transport, initialStatus, statuschan, errorchan)
}
}
func WaitStatusChanged(transport *HTTPTransport, initialStatus ServerStatus, statuschan chan ServerStatus, errorchan chan error) {
if err := subscribeSSE(transport, statuschan, errorchan, true); err != nil {
go pollUntilChange(transport, initialStatus, statuschan, errorchan)
}
}
// Start listening for server-sent events
func subscribeSSE(transport *HTTPTransport, statuschan chan ServerStatus, errorchan chan error, untilNextOnly bool) error {
ctx, cancel := context.WithCancel(context.Background())
events := make(chan *sseclient.Event)
cancelled := false
go func() {
for {
e := <-events
if e == nil || e.Type == "open" {
continue
}
status := ServerStatus(strings.Trim(string(e.Data), `"`))
statuschan <- status
if untilNextOnly || status.Finished() {
errorchan <- nil
cancelled = true
cancel()
return
}
}
}()
err := sseclient.Notify(ctx, transport.Server+"statusevents", true, events)
// When sse was cancelled, an error is expected to be returned. The channels are already closed then.
if cancelled {
return nil
}
close(events)
return err
}
// poll recursively polls the session status until a final status is received.
func poll(transport *HTTPTransport, initialStatus ServerStatus, statuschan chan ServerStatus, errorchan chan error) {
status := initialStatus
statuschanPolling := make(chan ServerStatus)
errorchanPolling := make(chan error)
go pollUntilChange(transport, status, statuschanPolling, errorchanPolling)
for {
select {
case status = <-statuschanPolling:
statuschan <- status
if status.Finished() {
errorchan <- nil
return
}
case err := <-errorchanPolling:
if err != nil {
errorchan <- err
return
}
go pollUntilChange(transport, status, statuschanPolling, errorchanPolling)
}
}
}
func pollUntilChange(transport *HTTPTransport, initialStatus ServerStatus, statuschan chan ServerStatus, errorchan chan error) {
// First we wait
<-time.NewTimer(pollInterval).C
// Get session status
var s string
if err := transport.Get("status", &s); err != nil {
errorchan <- err
return
}
status := ServerStatus(strings.Trim(s, `"`))
// report if status changed
if status != initialStatus {
statuschan <- status
errorchan <- nil
return
}
go pollUntilChange(transport, status, statuschan, errorchan)
}