-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
339 lines (302 loc) · 8.71 KB
/
main.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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
// SPDX-License-Identifier: AGPL-3.0-or-later
// DMRHub - Run a DMR network server in a single binary
// Copyright (C) 2023-2024 Jacob McSwain
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// The source code is available at <https://github.com/USA-RedDragon/DMRHub>
package main
import (
"context"
"os"
"runtime"
"sync"
"syscall"
"time"
"github.com/USA-RedDragon/DMRHub/internal/config"
"github.com/USA-RedDragon/DMRHub/internal/db"
"github.com/USA-RedDragon/DMRHub/internal/db/models"
"github.com/USA-RedDragon/DMRHub/internal/dmr/calltracker"
"github.com/USA-RedDragon/DMRHub/internal/dmr/servers"
"github.com/USA-RedDragon/DMRHub/internal/dmr/servers/hbrp"
"github.com/USA-RedDragon/DMRHub/internal/dmr/servers/openbridge"
"github.com/USA-RedDragon/DMRHub/internal/featureflags"
"github.com/USA-RedDragon/DMRHub/internal/http"
"github.com/USA-RedDragon/DMRHub/internal/logging"
"github.com/USA-RedDragon/DMRHub/internal/metrics"
"github.com/USA-RedDragon/DMRHub/internal/repeaterdb"
"github.com/USA-RedDragon/DMRHub/internal/userdb"
"github.com/go-co-op/gocron/v2"
"github.com/redis/go-redis/extra/redisotel/v9"
"github.com/redis/go-redis/v9"
_ "github.com/tinylib/msgp/printer"
"github.com/ztrue/shutdown"
"go.opentelemetry.io/otel"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace"
"go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc"
"go.opentelemetry.io/otel/sdk/resource"
sdktrace "go.opentelemetry.io/otel/sdk/trace"
_ "go.uber.org/automaxprocs"
"golang.org/x/sync/errgroup"
)
func initTracer() func(context.Context) error {
exporter, err := otlptrace.New(
context.Background(),
otlptracegrpc.NewClient(
otlptracegrpc.WithInsecure(),
otlptracegrpc.WithEndpoint(config.GetConfig().OTLPEndpoint),
),
)
if err != nil {
logging.Errorf("Failed tracing app: %v", err)
}
resources, err := resource.New(
context.Background(),
resource.WithAttributes(
attribute.String("service.name", "DMRHub"),
attribute.String("library.language", "go"),
),
)
if err != nil {
logging.Errorf("Could not set resources: %v", err)
}
otel.SetTracerProvider(
sdktrace.NewTracerProvider(
sdktrace.WithSampler(sdktrace.AlwaysSample()),
sdktrace.WithBatcher(exporter),
sdktrace.WithResource(resources),
),
)
return exporter.Shutdown
}
// https://goreleaser.com/cookbooks/using-main.version/
//
//nolint:golint,gochecknoglobals
var (
version = "dev"
commit = "none"
)
func main() {
os.Exit(start())
}
func start() int {
logging.Errorf("DMRHub v%s-%s", version, commit)
logging.Logf("DMRHub v%s-%s", version, commit)
defer logging.Close()
ctx := context.Background()
featureflags.Init(config.GetConfig())
scheduler, err := gocron.NewScheduler()
if err != nil {
logging.Errorf("Failed to create scheduler: %s", err)
return 1
}
var cleanup func(context.Context) error
if config.GetConfig().OTLPEndpoint != "" {
cleanup = initTracer()
defer func() {
err := cleanup(ctx)
if err != nil {
logging.Errorf("Failed to shutdown tracer: %s", err)
}
}()
}
go metrics.CreateMetricsServer()
database := db.MakeDB()
// Dummy call to get the data decoded into memory early
go func() {
err := repeaterdb.Update()
if err != nil {
logging.Errorf("Failed to update repeater database: %s using built in one", err)
}
}()
_, err = scheduler.NewJob(
gocron.DailyJob(1, gocron.NewAtTimes(
gocron.NewAtTime(0, 0, 0),
)),
gocron.NewTask(func() {
err := repeaterdb.Update()
if err != nil {
logging.Errorf("Failed to update repeater database: %s", err)
}
}),
)
if err != nil {
logging.Errorf("Failed to schedule repeater update: %s", err)
}
go func() {
err = userdb.Update()
if err != nil {
logging.Errorf("Failed to update user database: %s using built in one", err)
}
}()
_, err = scheduler.NewJob(
gocron.DailyJob(1, gocron.NewAtTimes(
gocron.NewAtTime(0, 0, 0),
)),
gocron.NewTask(func() {
err := userdb.Update()
if err != nil {
logging.Errorf("Failed to update user database: %s", err)
}
}),
)
if err != nil {
logging.Errorf("Failed to schedule user update: %s", err)
}
scheduler.Start()
const connsPerCPU = 10
const maxIdleTime = 10 * time.Minute
redis := redis.NewClient(&redis.Options{
Addr: config.GetConfig().RedisHost,
Password: config.GetConfig().RedisPassword,
PoolFIFO: true,
PoolSize: runtime.GOMAXPROCS(0) * connsPerCPU,
MinIdleConns: runtime.GOMAXPROCS(0),
ConnMaxIdleTime: maxIdleTime,
})
_, err = redis.Ping(ctx).Result()
if err != nil {
logging.Errorf("Failed to connect to redis: %s", err)
return 1
}
defer func() {
err := redis.Close()
if err != nil {
logging.Errorf("Failed to close redis: %s", err)
}
}()
if config.GetConfig().OTLPEndpoint != "" {
if err := redisotel.InstrumentTracing(redis); err != nil {
logging.Errorf("Failed to trace redis: %s", err)
return 1
}
// Enable metrics instrumentation.
if err := redisotel.InstrumentMetrics(redis); err != nil {
logging.Errorf("Failed to instrument redis: %s", err)
return 1
}
}
callTracker := calltracker.NewCallTracker(database, redis)
redisClient := servers.MakeRedisClient(redis)
hbrpServer := hbrp.MakeServer(database, redis, redisClient, callTracker, version, commit)
err = hbrpServer.Start(ctx)
if err != nil {
logging.Errorf("Failed to start HBRP server: %v", err)
return 1
}
defer hbrpServer.Stop(ctx)
g := new(errgroup.Group)
g.Go(func() error {
// For each repeater in the DB, start a gofunc to listen for calls
repeaters, err := models.ListRepeaters(database)
if err != nil {
return err //nolint:golint,wrapcheck
}
for _, repeater := range repeaters {
go hbrp.GetSubscriptionManager(database).ListenForCalls(redis, repeater.ID)
}
return nil
})
if config.GetConfig().OpenBridgePort != 0 {
// Start the OpenBridge server
openbridgeServer := openbridge.MakeServer(database, redisClient, callTracker)
err := openbridgeServer.Start(ctx)
if err != nil {
logging.Errorf("Failed to start OpenBridge server: %v", err)
return 1
}
defer openbridgeServer.Stop(ctx)
go func() {
// For each peer in the DB, start a gofunc to listen for calls
peers := models.ListPeers(database)
for _, peer := range peers {
go openbridge.GetSubscriptionManager().Subscribe(ctx, redis, peer)
}
}()
}
http := http.MakeServer(database, redis, version, commit)
err = http.Start()
if err != nil {
logging.Errorf("Failed to start HTTP server %v", err)
return 1
}
defer http.Stop()
if err := g.Wait(); err != nil {
logging.Errorf("Failed to start repeater listeners: %s", err)
return 1
}
stop := func(sig os.Signal) {
logging.Errorf("Shutting down due to %v", sig)
wg := new(sync.WaitGroup)
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
err = scheduler.StopJobs()
if err != nil {
logging.Errorf("Failed to stop scheduler jobs: %s", err)
}
err = scheduler.Shutdown()
if err != nil {
logging.Errorf("Failed to stop scheduler: %s", err)
}
}(wg)
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
hbrp.GetSubscriptionManager(database).CancelAllSubscriptions()
hbrpServer.Stop(ctx)
}(wg)
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
if config.GetConfig().OTLPEndpoint != "" {
const timeout = 5 * time.Second
ctx, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
err := cleanup(ctx)
if err != nil {
logging.Errorf("Failed to shutdown tracer: %s", err)
}
}
}(wg)
wg.Add(1)
go func(wg *sync.WaitGroup) {
defer wg.Done()
http.Stop()
}(wg)
// Wait for all the servers to stop
const timeout = 10 * time.Second
c := make(chan struct{})
go func() {
defer close(c)
wg.Wait()
}()
select {
case <-c:
redis.Close()
logging.Error("Shutdown safely completed")
logging.Close()
os.Exit(0)
case <-time.After(timeout):
logging.Error("Shutdown timed out")
logging.Close()
os.Exit(1)
}
}
defer stop(syscall.SIGINT)
shutdown.AddWithParam(stop)
shutdown.Listen(syscall.SIGINT, syscall.SIGKILL, syscall.SIGTERM, syscall.SIGQUIT, syscall.SIGHUP)
return 0
}