-
Notifications
You must be signed in to change notification settings - Fork 0
/
component.go
128 lines (113 loc) · 3.68 KB
/
component.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
// Copyright © 2017 The Things Network
// Use of this source code is governed by the MIT license that can be found in the LICENSE file.
// Package component contains code that is shared by all components (discovery, router, broker, networkserver, handler)
package component
import (
"crypto/ecdsa"
"crypto/tls"
"fmt"
"net/http"
"runtime"
"time"
"github.com/TheThingsNetwork/go-account-lib/claims"
"github.com/TheThingsNetwork/go-account-lib/tokenkey"
ttnlog "github.com/TheThingsNetwork/go-utils/log"
pb_discovery "github.com/TheThingsNetwork/ttn/api/discovery"
pb_monitor "github.com/TheThingsNetwork/ttn/api/monitor"
"github.com/TheThingsNetwork/ttn/api/trace"
"github.com/spf13/viper"
"golang.org/x/net/context" // See https://github.com/grpc/grpc-go/issues/711"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
)
// Component contains the common attributes for all TTN components
type Component struct {
Config Config
Identity *pb_discovery.Announcement
Discovery pb_discovery.Client
Monitors pb_monitor.Registry
Ctx ttnlog.Interface
AccessToken string
privateKey *ecdsa.PrivateKey
tlsConfig *tls.Config
TokenKeyProvider tokenkey.Provider
status int64
healthServer *health.Server
}
type Interface interface {
RegisterRPC(s *grpc.Server)
Init(c *Component) error
Shutdown()
ValidateNetworkContext(ctx context.Context) (*pb_discovery.Announcement, error)
ValidateTTNAuthContext(ctx context.Context) (*claims.Claims, error)
}
type ManagementInterface interface {
RegisterManager(s *grpc.Server)
}
// New creates a new Component
func New(ctx ttnlog.Interface, serviceName string, announcedAddress string) (*Component, error) {
go func() {
memstats := new(runtime.MemStats)
for range time.Tick(time.Minute) {
runtime.ReadMemStats(memstats)
ctx.WithFields(ttnlog.Fields{
"Goroutines": runtime.NumGoroutine(),
"Memory": float64(memstats.Alloc) / 1000000,
}).Debugf("Stats")
}
}()
// Disable gRPC tracing
// SEE: https://github.com/grpc/grpc-go/issues/695
grpc.EnableTracing = false
component := &Component{
Config: ConfigFromViper(),
Ctx: ctx,
Identity: &pb_discovery.Announcement{
Id: viper.GetString("id"),
Description: viper.GetString("description"),
ServiceName: serviceName,
ServiceVersion: fmt.Sprintf("%s-%s (%s)", viper.GetString("version"), viper.GetString("gitCommit"), viper.GetString("buildDate")),
NetAddress: announcedAddress,
Public: viper.GetBool("public"),
},
AccessToken: viper.GetString("auth-token"),
}
trace.SetComponent(component.Identity.ServiceName, component.Identity.Id)
if err := component.InitAuth(); err != nil {
return nil, err
}
if serviceName != "discovery" && serviceName != "networkserver" {
var err error
component.Discovery, err = pb_discovery.NewClient(
viper.GetString("discovery-address"),
component.Identity,
func() string {
token, _ := component.BuildJWT()
return token
},
)
if err != nil {
return nil, err
}
}
if healthPort := viper.GetInt("health-port"); healthPort > 0 {
http.HandleFunc("/healthz", func(w http.ResponseWriter, req *http.Request) {
switch component.GetStatus() {
case StatusHealthy:
w.WriteHeader(200)
w.Write([]byte("Status is HEALTHY"))
return
case StatusUnhealthy:
w.WriteHeader(503)
w.Write([]byte("Status is UNHEALTHY"))
return
}
})
go http.ListenAndServe(fmt.Sprintf(":%d", healthPort), nil)
}
component.Monitors = pb_monitor.NewRegistry(ctx)
for name, addr := range viper.GetStringMapString("monitor-servers") {
go component.Monitors.InitClient(name, addr)
}
return component, nil
}