-
Notifications
You must be signed in to change notification settings - Fork 457
/
runner.go
136 lines (116 loc) · 3.73 KB
/
runner.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
package runner
import (
"github.com/envoyproxy/ratelimit/src/metrics"
"io"
"math/rand"
"net/http"
"strings"
"sync"
"time"
stats "github.com/lyft/gostats"
"github.com/coocood/freecache"
pb_legacy "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2"
pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v3"
"github.com/envoyproxy/ratelimit/src/config"
"github.com/envoyproxy/ratelimit/src/limiter"
"github.com/envoyproxy/ratelimit/src/memcached"
"github.com/envoyproxy/ratelimit/src/redis"
"github.com/envoyproxy/ratelimit/src/server"
ratelimit "github.com/envoyproxy/ratelimit/src/service"
"github.com/envoyproxy/ratelimit/src/settings"
"github.com/envoyproxy/ratelimit/src/utils"
logger "github.com/sirupsen/logrus"
)
type Runner struct {
statsStore stats.Store
settings settings.Settings
srv server.Server
mu sync.Mutex
}
func NewRunner(s settings.Settings) Runner {
return Runner{
statsStore: stats.NewDefaultStore(),
settings: s,
}
}
func (runner *Runner) GetStatsStore() stats.Store {
return runner.statsStore
}
func createLimiter(srv server.Server, s settings.Settings, localCache *freecache.Cache) limiter.RateLimitCache {
switch s.BackendType {
case "redis", "":
return redis.NewRateLimiterCacheImplFromSettings(
s,
localCache,
srv,
utils.NewTimeSourceImpl(),
rand.New(utils.NewLockedSource(time.Now().Unix())),
s.ExpirationJitterMaxSeconds)
case "memcache":
return memcached.NewRateLimitCacheImplFromSettings(
s,
utils.NewTimeSourceImpl(),
rand.New(utils.NewLockedSource(time.Now().Unix())),
localCache,
srv.Scope())
default:
logger.Fatalf("Invalid setting for BackendType: %s", s.BackendType)
panic("This line should not be reachable")
}
}
func (runner *Runner) Run() {
s := runner.settings
logLevel, err := logger.ParseLevel(s.LogLevel)
if err != nil {
logger.Fatalf("Could not parse log level. %v\n", err)
} else {
logger.SetLevel(logLevel)
}
if strings.ToLower(s.LogFormat) == "json" {
logger.SetFormatter(&logger.JSONFormatter{
TimestampFormat: time.RFC3339Nano,
FieldMap: logger.FieldMap{
logger.FieldKeyTime: "@timestamp",
logger.FieldKeyMsg: "@message",
},
})
}
var localCache *freecache.Cache
if s.LocalCacheSizeInBytes != 0 {
localCache = freecache.NewCache(s.LocalCacheSizeInBytes)
}
serverReporter := metrics.NewServerReporter(runner.statsStore.ScopeWithTags("ratelimit_server", s.ExtraTags))
srv := server.NewServer(s, "ratelimit", runner.statsStore, localCache, settings.GrpcUnaryInterceptor(serverReporter.UnaryServerInterceptor()))
runner.mu.Lock()
runner.srv = srv
runner.mu.Unlock()
service := ratelimit.NewService(
srv.Runtime(),
createLimiter(srv, s, localCache),
config.NewRateLimitConfigLoaderImpl(),
srv.Scope().Scope("service"),
s.RuntimeWatchRoot,
)
srv.AddDebugHttpEndpoint(
"/rlconfig",
"print out the currently loaded configuration for debugging",
func(writer http.ResponseWriter, request *http.Request) {
io.WriteString(writer, service.GetCurrentConfig().Dump())
})
srv.AddJsonHandler(service)
// Ratelimit is compatible with two proto definitions
// 1. data-plane-api v3 rls.proto: https://github.com/envoyproxy/data-plane-api/blob/master/envoy/service/ratelimit/v3/rls.proto
pb.RegisterRateLimitServiceServer(srv.GrpcServer(), service)
// 1. data-plane-api v2 rls.proto: https://github.com/envoyproxy/data-plane-api/blob/master/envoy/service/ratelimit/v2/rls.proto
pb_legacy.RegisterRateLimitServiceServer(srv.GrpcServer(), service.GetLegacyService())
// (1) is the current definition, and (2) is the legacy definition.
srv.Start()
}
func (runner *Runner) Stop() {
runner.mu.Lock()
srv := runner.srv
runner.mu.Unlock()
if srv != nil {
srv.Stop()
}
}