-
Notifications
You must be signed in to change notification settings - Fork 465
/
Copy pathrunner.go
81 lines (68 loc) · 2.7 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
package runner
import (
"io"
"math/rand"
"net/http"
"time"
"github.com/coocood/freecache"
pb "github.com/envoyproxy/go-control-plane/envoy/service/ratelimit/v2"
pb_legacy "github.com/lyft/ratelimit/proto/ratelimit"
"github.com/lyft/ratelimit/src/config"
"github.com/lyft/ratelimit/src/redis"
"github.com/lyft/ratelimit/src/server"
ratelimit "github.com/lyft/ratelimit/src/service"
"github.com/lyft/ratelimit/src/settings"
logger "github.com/sirupsen/logrus"
)
func Run() {
s := settings.NewSettings()
logLevel, err := logger.ParseLevel(s.LogLevel)
if err != nil {
logger.Fatalf("Could not parse log level. %v\n", err)
} else {
logger.SetLevel(logLevel)
}
srv := server.NewServer("ratelimit", settings.GrpcUnaryInterceptor(nil))
var perSecondPool redis.Pool
if s.RedisPerSecond {
if s.RedisPerSecondAuth != "" || s.RedisPerSecondTls {
perSecondPool = redis.NewAuthTLSPoolImpl(srv.Scope().Scope("redis_per_second_pool"), s.RedisPerSecondAuth, s.RedisPerSecondUrl, s.RedisPerSecondPoolSize)
} else {
perSecondPool = redis.NewPoolImpl(srv.Scope().Scope("redis_per_second_pool"), s.RedisSocketType, s.RedisPerSecondUrl, s.RedisPerSecondPoolSize)
}
}
var otherPool redis.Pool
if s.RedisAuth != "" || s.RedisTls {
otherPool = redis.NewAuthTLSPoolImpl(srv.Scope().Scope("redis_pool"), s.RedisAuth, s.RedisUrl, s.RedisPoolSize)
} else {
otherPool = redis.NewPoolImpl(srv.Scope().Scope("redis_pool"), s.RedisSocketType, s.RedisUrl, s.RedisPoolSize)
}
var localCache *freecache.Cache
if s.LocalCacheSizeInBytes != 0 {
localCache = freecache.NewCache(s.LocalCacheSizeInBytes)
}
service := ratelimit.NewService(
srv.Runtime(),
redis.NewRateLimitCacheImpl(
otherPool,
perSecondPool,
redis.NewTimeSourceImpl(),
rand.New(redis.NewLockedSource(time.Now().Unix())),
s.ExpirationJitterMaxSeconds,
localCache),
config.NewRateLimitConfigLoaderImpl(),
srv.Scope().Scope("service"))
srv.AddDebugHttpEndpoint(
"/rlconfig",
"print out the currently loaded configuration for debugging",
func(writer http.ResponseWriter, request *http.Request) {
io.WriteString(writer, service.GetCurrentConfig().Dump())
})
// Ratelimit is compatible with two proto definitions
// 1. data-plane-api rls.proto: https://github.com/envoyproxy/data-plane-api/blob/master/envoy/service/ratelimit/v2/rls.proto
pb.RegisterRateLimitServiceServer(srv.GrpcServer(), service)
// 2. ratelimit.proto defined in this repository: https://github.com/lyft/ratelimit/blob/0ded92a2af8261d43096eba4132e45b99a3b8b14/proto/ratelimit/ratelimit.proto
pb_legacy.RegisterRateLimitServiceServer(srv.GrpcServer(), service.GetLegacyService())
// (1) is the current definition, and (2) is the legacy definition.
srv.Start()
}