Skip to content

Commit 4f1a2ba

Browse files
committed
Implement main.go and fix path issues in lua_scripts
1 parent c9a405a commit 4f1a2ba

6 files changed

Lines changed: 104 additions & 20 deletions

File tree

cmd/server/main.go

Lines changed: 74 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,74 @@
1-
// cmd/server/main.go
1+
package main
2+
3+
import (
4+
"context"
5+
"log"
6+
"net"
7+
"net/http"
8+
"os"
9+
"os/signal"
10+
"syscall"
11+
"time"
12+
13+
"github.com/Pavan-Rana/rate-limiter/internal/config"
14+
grpcserver "github.com/Pavan-Rana/rate-limiter/internal/grpc"
15+
httpserver "github.com/Pavan-Rana/rate-limiter/internal/http"
16+
"github.com/Pavan-Rana/rate-limiter/internal/limiter"
17+
"github.com/Pavan-Rana/rate-limiter/internal/metrics"
18+
"github.com/Pavan-Rana/rate-limiter/internal/store"
19+
20+
pb "github.com/Pavan-Rana/rate-limiter/proto"
21+
"google.golang.org/grpc"
22+
)
23+
24+
func main() {
25+
cfg, err := config.Load()
26+
if err != nil {
27+
log.Fatalf("Failed to load config: %v", err)
28+
}
29+
30+
redisStore, err := store.NewRedisStore(cfg.RedisAddr)
31+
if err != nil {
32+
log.Fatalf("Failed to create Redis store: %v", err)
33+
}
34+
35+
metrics.Register()
36+
37+
lim := limiter.New(redisStore, cfg)
38+
39+
lis, err := net.Listen("tcp", cfg.GRPCAddr)
40+
if err != nil {
41+
log.Fatalf("Failed to listen: %v", err)
42+
}
43+
grpcSrv := grpc.NewServer()
44+
pb.RegisterRateLimiterServer(grpcSrv, grpcserver.New(lim))
45+
46+
go func() {
47+
log.Printf("gRPC listening on %s", cfg.GRPCAddr)
48+
if err := grpcSrv.Serve(lis); err != nil {
49+
log.Fatalf("gRPC server error: %v", err)
50+
}
51+
}()
52+
53+
httpSrv := &http.Server{
54+
Addr: cfg.HTTPAddr,
55+
Handler: httpserver.NewRouter(lim),
56+
}
57+
go func() {
58+
log.Printf("HTTP listening on %s", cfg.HTTPAddr)
59+
if err := httpSrv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
60+
log.Fatalf("HTTP server error: %v", err)
61+
}
62+
}()
63+
64+
quit := make(chan os.Signal, 1)
65+
signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT)
66+
<-quit
67+
68+
log.Println("Shutting down...")
69+
grpcSrv.GracefulStop()
70+
71+
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
72+
defer cancel()
73+
_ = httpSrv.Shutdown(ctx)
74+
}

go.mod

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
module github.com/Pavan-Rana/rate-limiter
22

3-
go 1.22
3+
go 1.24.0
44

55
require (
66
github.com/prometheus/client_golang v1.19.0
77
github.com/redis/go-redis/v9 v9.5.1
8-
google.golang.org/grpc v1.63.2
9-
google.golang.org/protobuf v1.34.1
8+
google.golang.org/grpc v1.79.3
9+
google.golang.org/protobuf v1.36.10
1010
)
1111

1212
require (
1313
github.com/alicebob/miniredis/v2 v2.37.0 // indirect
1414
github.com/beorn7/perks v1.0.1 // indirect
15-
github.com/cespare/xxhash/v2 v2.2.0 // indirect
15+
github.com/cespare/xxhash/v2 v2.3.0 // indirect
1616
github.com/davecgh/go-spew v1.1.1 // indirect
1717
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
1818
github.com/pmezard/go-difflib v1.0.0 // indirect
@@ -21,9 +21,9 @@ require (
2121
github.com/prometheus/procfs v0.12.0 // indirect
2222
github.com/stretchr/testify v1.11.1 // indirect
2323
github.com/yuin/gopher-lua v1.1.1 // indirect
24-
golang.org/x/net v0.21.0 // indirect
25-
golang.org/x/sys v0.17.0 // indirect
26-
golang.org/x/text v0.14.0 // indirect
27-
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de // indirect
24+
golang.org/x/net v0.48.0 // indirect
25+
golang.org/x/sys v0.39.0 // indirect
26+
golang.org/x/text v0.32.0 // indirect
27+
google.golang.org/genproto/googleapis/rpc v0.0.0-20251202230838-ff82c1b0f217 // indirect
2828
gopkg.in/yaml.v3 v3.0.1 // indirect
2929
)

go.sum

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
44
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
55
github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
66
github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
7+
github.com/cespare/xxhash/v2 v2.3.0 h1:UL815xU9SqsFlibzuggzjXhog7bL6oX9BbNZnL2UFvs=
8+
github.com/cespare/xxhash/v2 v2.3.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
79
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
810
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
911
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f h1:lO4WD4F/rVNCu3HqELle0jiPLLBs70cWOduZpkS1E78=
@@ -26,19 +28,31 @@ github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M
2628
github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw=
2729
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
2830
golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
31+
golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU=
32+
golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY=
2933
golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
3034
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
35+
golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk=
36+
golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks=
3137
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
3238
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
39+
golang.org/x/text v0.32.0 h1:ZD01bjUt1FQ9WJ0ClOL5vxgxOI/sVCNgX1YtKwcY0mU=
40+
golang.org/x/text v0.32.0/go.mod h1:o/rUWzghvpD5TXrTIBuJU77MTaN0ljMWE47kxGJQ7jY=
3341
google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de h1:F6qOa9AZTYJXOUEr4jDysRDLrm4PHePlge4v4TGAlxY=
3442
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de h1:cZGRis4/ot9uVm639a+rHCUaG0JJHEsdyzSQTMX+suY=
3543
google.golang.org/genproto/googleapis/rpc v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:H4O17MA/PE9BsGx3w+a+W2VOLLD1Qf7oJneAoU6WktY=
44+
google.golang.org/genproto/googleapis/rpc v0.0.0-20251202230838-ff82c1b0f217 h1:gRkg/vSppuSQoDjxyiGfN4Upv/h/DQmIR10ZU8dh4Ww=
45+
google.golang.org/genproto/googleapis/rpc v0.0.0-20251202230838-ff82c1b0f217/go.mod h1:7i2o+ce6H/6BluujYR+kqX3GKH+dChPTQU19wjRPiGk=
3646
google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM=
3747
google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA=
48+
google.golang.org/grpc v1.79.3 h1:sybAEdRIEtvcD68Gx7dmnwjZKlyfuc61Dyo9pGXXkKE=
49+
google.golang.org/grpc v1.79.3/go.mod h1:KmT0Kjez+0dde/v2j9vzwoAScgEPx/Bw1CYChhHLrHQ=
3850
google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI=
3951
google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
4052
google.golang.org/protobuf v1.34.1 h1:9ddQBjfCyZPOHPUiPxpYESBLc+T8P3E+Vo4IbKZgFWg=
4153
google.golang.org/protobuf v1.34.1/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos=
54+
google.golang.org/protobuf v1.36.10 h1:AYd7cD/uASjIL6Q9LiTjz8JLcrh/88q5UObnmY3aOOE=
55+
google.golang.org/protobuf v1.36.10/go.mod h1:HTf+CrKn2C3g5S8VImy6tdcUvCska2kB7j23XfzDpco=
4256
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
4357
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
4458
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/store/lua_scripts.go

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package store
33
import (
44
"context"
55
_ "embed"
6-
"os"
76

87
"github.com/redis/go-redis/v9"
98
)
@@ -15,24 +14,22 @@ type LuaScripts struct {
1514
TokenBucketSHA string
1615
}
1716

17+
//go:embed scripts/lua/sliding_window.lua
18+
var slidingWindowLua []byte
19+
20+
//go:embed scripts/lua/token_bucket.lua
21+
var tokenBucketLua []byte
22+
1823
// LoadScripts pre-loads Lua scripts into Redis at startup.
1924
// Subsequent calls use EVALSHA with the digest - faster and avoids redundant parsing.
2025
func LoadScripts(ctx context.Context, client *redis.Client) (*LuaScripts, error) {
21-
sw, err := os.ReadFile("../../scripts/lua/sliding_window.lua")
22-
if err != nil {
23-
return nil, err
24-
}
25-
tb, err := os.ReadFile("../../scripts/lua/token_bucket.lua")
26-
if err != nil {
27-
return nil, err
28-
}
2926

30-
swSHA, err := client.ScriptLoad(ctx, string(sw)).Result()
27+
swSHA, err := client.ScriptLoad(ctx, string(slidingWindowLua)).Result()
3128
if err != nil {
3229
return nil, err
3330
}
3431

35-
tbSHA, err := client.ScriptLoad(ctx, string(tb)).Result()
32+
tbSHA, err := client.ScriptLoad(ctx, string(tokenBucketLua)).Result()
3633
if err != nil {
3734
return nil, err
3835
}
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)