forked from berty/berty
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bridge.go
226 lines (186 loc) · 4.62 KB
/
bridge.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
package bertybridge
import (
"context"
"fmt"
"net"
"strings"
"sync"
"time"
"berty.tech/berty/v2/go/internal/grpcutil"
"berty.tech/berty/v2/go/pkg/errcode"
ma "github.com/multiformats/go-multiaddr"
"github.com/oklog/run"
"github.com/pkg/errors"
"go.uber.org/zap"
"google.golang.org/grpc"
)
type Config struct {
bridgeGrpcAddrs []string
}
func NewConfig() *Config {
return &Config{
bridgeGrpcAddrs: make([]string, 0),
}
}
// AddGRPCListener create a grpc listener with the given multiaddr
// if a normal addr is given, it will listen by default on grpcweb
// (ex: ":0" -> "/ip4/127.0.0.1/tcp/0/grpcweb")
func (c *Config) AddGRPCListener(addr string) {
c.bridgeGrpcAddrs = append(c.bridgeGrpcAddrs, addr)
}
type Bridge struct {
cerr chan error
cclose chan struct{}
once sync.Once
workers run.Group
grpcServer *grpc.Server
logger *zap.Logger
pipeListener *grpcutil.PipeListener
listeners []grpcutil.Listener
}
// newBridge is the main entrypoint for gomobile and should only take simple configuration as argument
func newBridge(s *grpc.Server, logger *zap.Logger, config *Config) (*Bridge, error) {
if config == nil {
config = NewConfig()
}
b := &Bridge{
cerr: make(chan error),
cclose: make(chan struct{}),
// noop opt
grpcServer: s,
logger: logger,
}
// Create cancel service
b.workers.Add(func() error {
// wait for closing signal
<-b.cclose
return errcode.ErrBridgeInterrupted
}, func(error) {
b.once.Do(func() { close(b.cclose) })
})
// setup grpc listeners
{
var err error
for _, addr := range config.bridgeGrpcAddrs {
if addr == "" {
err = fmt.Errorf("addr can't be empty")
} else {
err = b.addGRPCListenner(addr)
}
if err != nil {
return nil, errors.Wrap(err, "init gRPC listener failed")
}
}
}
// setup lazy grpc listener
pipeListener := grpcutil.NewPipeListener()
b.workers.Add(func() error {
return b.grpcServer.Serve(pipeListener)
}, func(error) {
pipeListener.Close()
})
b.pipeListener = pipeListener
// start Bridge
b.logger.Debug("starting Bridge")
go func() {
b.cerr <- b.workers.Run()
}()
return b, nil
}
func (b *Bridge) GRPCListenerAddr() string { return b.GetGRPCAddrFor("ip4/tcp/grpc") }
func (b *Bridge) GRPCWebListenerAddr() string { return b.GetGRPCAddrFor("ip4/tcp/grpcweb") }
func (b *Bridge) GRPCWebSocketListenerAddr() string { return b.GetGRPCAddrFor("ip4/tcp/grpcws") }
// GetGRPCAddrFor the given protocols, if not found return an empty string
func (b *Bridge) GetGRPCAddrFor(protos string) string {
for _, l := range b.listeners {
ps := make([]string, 0)
ma.ForEach(l.GRPCMultiaddr(), func(c ma.Component) bool {
ps = append(ps, c.Protocol().Name)
return true
})
if strings.Join(ps, "/") == protos {
return l.Addr().String()
}
}
return ""
}
// NewGRPCClient return client service on success
func (b *Bridge) NewGRPCClient() (client *Client, err error) {
var grpcClient *grpc.ClientConn
if grpcClient, err = b.pipeListener.NewClientConn(grpc.WithInsecure()); err != nil {
return
}
client = &Client{grpcClient}
return
}
// Close Bridge
func (b *Bridge) Close() (err error) {
if b.isClosed() {
return errcode.ErrBridgeNotRunning
}
b.logger.Info("Bridge.Close called")
// send close signal
b.once.Do(func() { close(b.cclose) })
// set close timeout
ctx, cancel := context.WithTimeout(context.Background(), time.Second*4)
defer cancel()
// wait or die
select {
case err = <-b.cerr:
case <-ctx.Done():
err = ctx.Err()
}
if errcode.Code(err) != errcode.ErrBridgeInterrupted.Code() {
return errcode.TODO.Wrap(err)
}
return nil
}
func (b *Bridge) isClosed() bool {
select {
case <-b.cclose:
return true
default:
return false
}
}
func (b *Bridge) addGRPCListenner(maddr string) error {
m, err := parseAddr(maddr)
if err != nil {
return err
}
b.logger.Info(m.String())
l, err := grpcutil.Listen(m)
if err != nil {
return err
}
server := &grpcutil.Server{Server: b.grpcServer}
b.workers.Add(func() (err error) {
if err = server.Serve(l); err != nil {
b.logger.Error("grpc serve server",
zap.String("maddr", maddr),
zap.Error(err))
}
return
}, func(err error) {
l.Close()
})
b.listeners = append(b.listeners, l)
return nil
}
// helpers
func parseAddr(addr string) (maddr ma.Multiaddr, err error) {
maddr, err = ma.NewMultiaddr(addr)
if err != nil {
// try to get a tcp multiaddr from host:port
host, port, serr := net.SplitHostPort(addr)
if serr != nil {
return
}
if host == "" {
host = "127.0.0.1"
}
addr = fmt.Sprintf("/ip4/%s/tcp/%s/grpcweb", host, port)
maddr, err = ma.NewMultiaddr(addr)
}
return
}