forked from benalexau/ibconnect
-
Notifications
You must be signed in to change notification settings - Fork 0
/
refresh.go
39 lines (34 loc) · 1.17 KB
/
refresh.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
package server
import (
"errors"
"fmt"
"strings"
"time"
"github.com/ant0ine/go-json-rest/rest"
"github.com/benalexau/ibconnect/core"
)
// RefreshIfNeeded detects a HTTP request for an immediate refresh of the gateway backend.
// If detected, it publishes a "request" notification and blocks awaiting the "completed"
// acknowledgement reply. An error is returned if acknowledgement exceeds the timeout.
func RefreshIfNeeded(n *core.Notifier, r *rest.Request, requestRefresh core.NtType, completedRefresh core.NtType, timeout time.Duration) error {
if strings.Contains(r.Header.Get("Cache-Control"), "max-age=0") {
notifications := make(chan *core.Notification)
n.Subscribe(notifications)
defer n.Unsubscribe(notifications)
n.Publish(requestRefresh, 0)
for {
select {
case msg := <-notifications:
if msg == nil {
return errors.New("Subscription channel unexpectedly closed; did another goroutine close the notifier?")
}
if msg.Type == completedRefresh {
break
}
case <-time.After(timeout):
return fmt.Errorf("Timeout %v waiting for '%v' response to '%v' request", timeout, completedRefresh, requestRefresh)
}
}
}
return nil
}