forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sharewatch.go
50 lines (43 loc) · 1.28 KB
/
sharewatch.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
package sharewatch
import (
"context"
"sync"
"github.com/rancher/norman/store/proxy"
"github.com/rancher/norman/types"
"github.com/rancher/rancher/pkg/broadcast"
"k8s.io/client-go/rest"
)
type WatchShare struct {
sync.Mutex
types.Store
close context.Context
clientGetter proxy.ClientGetter
broadcasters map[rest.Interface]*broadcast.Broadcaster
}
func NewWatchShare(ctx context.Context, getter proxy.ClientGetter, store types.Store) *WatchShare {
return &WatchShare{
Store: store,
close: ctx,
clientGetter: getter,
broadcasters: map[rest.Interface]*broadcast.Broadcaster{},
}
}
func (w *WatchShare) Watch(apiContext *types.APIContext, schema *types.Schema, opt *types.QueryOptions) (chan map[string]interface{}, error) {
client, err := w.clientGetter.UnversionedClient(apiContext, w.Context())
if err != nil {
return nil, err
}
var b *broadcast.Broadcaster
w.Lock()
b, ok := w.broadcasters[client]
if !ok {
b = &broadcast.Broadcaster{}
w.broadcasters[client] = b
}
w.Unlock()
return b.Subscribe(apiContext.Request.Context(), func() (chan map[string]interface{}, error) {
newAPIContext := *apiContext
newAPIContext.Request = apiContext.Request.WithContext(w.close)
return w.Store.Watch(&newAPIContext, schema, &types.QueryOptions{})
})
}