forked from flynn/flynn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.go
56 lines (49 loc) · 1.02 KB
/
rpc.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
package main
import (
"net/http"
"time"
ct "github.com/flynn/flynn/controller/types"
"github.com/flynn/flynn/pkg/rpcplus"
rpc "github.com/flynn/flynn/pkg/rpcplus/comborpc"
)
func rpcHandler(repo *FormationRepo) http.Handler {
rpcplus.RegisterName("Controller", &ControllerRPC{formations: repo})
return rpc.New(rpcplus.DefaultServer)
}
type ControllerRPC struct {
formations *FormationRepo
}
func (s *ControllerRPC) StreamFormations(since time.Time, stream rpcplus.Stream) error {
ch := make(chan *ct.ExpandedFormation)
done := make(chan struct{})
go func() {
outer:
for {
select {
case f := <-ch:
select {
case stream.Send <- f:
case <-stream.Error:
break outer
}
case <-stream.Error:
break outer
}
}
close(done)
}()
if err := s.formations.Subscribe(ch, since); err != nil {
return err
}
defer func() {
go func() {
// drain to prevent deadlock while removing the listener
for _ = range ch {
}
}()
s.formations.Unsubscribe(ch)
close(ch)
}()
<-done
return nil
}