-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
125 lines (103 loc) · 3.06 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
package main
import (
"context"
"flag"
"strings"
"time"
"caching-proxies-cache/config"
"caching-proxies-cache/support/cache"
"caching-proxies-cache/support/connection"
"github.com/labstack/echo/v4"
"github.com/labstack/echo/v4/middleware"
"github.com/nats-io/nats.go"
"github.com/sirupsen/logrus"
)
func main() {
flag.Parse()
ctx := context.Background()
logrus.SetFormatter(&logrus.JSONFormatter{PrettyPrint: true})
cch := cache.NewCache(ctx, *config.FlagCacheTtl)
// Echo instance
e := echo.New()
e.HideBanner = true
e.HidePort = true
// Middleware
e.Use(middleware.Recover())
// Routes
e.GET("/get", requestProcessor(cch))
// Start server
go func() {
e.Logger.Fatal(e.Start("0.0.0.0:1324"))
}()
ns, _, _ := connection.Establish(*config.FlagNatsContext, *config.FlagServer, *config.FlagCreds)
defer ns.Drain()
mergedInput := make(chan *nats.Msg, 1024)
for _, shard := range strings.Split(*config.FlagListenToShards, ",") {
if shard != "" {
go listenForShard(ctx, ns, shard, mergedInput)
}
}
for {
select {
case <-ctx.Done():
return
default:
}
select {
case msg := <-mergedInput:
processMessage(msg, cch)
case <-ctx.Done():
return
}
}
}
func requestProcessor(cch *cache.Cache) func(c echo.Context) error {
return func(c echo.Context) error {
previousHashID := c.QueryParam("prev_hash")
shardID := c.QueryParam("shard_id")
ans := cch.Get(previousHashID, shardID)
if ans == nil {
logrus.WithFields(map[string]interface{}{
"previous_hash_id": previousHashID,
"shard_id": shardID,
"result": "not_found",
}).Infof("Message with previous hash id %s and shard id %s not found", previousHashID, shardID)
return c.String(404, "Not found")
}
wasInCacheFor := time.Since(ans.WasCachedAt)
logrus.WithFields(map[string]interface{}{
"previous_hash_id": previousHashID,
"shard_id": shardID,
"result": "found",
"was_in_cache_for_ns": wasInCacheFor.Nanoseconds(),
}).
Infof(
"Message with prevous hash id %s and shard id %s found and was in cache for %s",
previousHashID, shardID, wasInCacheFor.String(),
)
return c.Blob(200, "application/octet-stream", ans.Blob)
}
}
func processMessage(msg *nats.Msg, cch *cache.Cache) {
blockHash := msg.Header.Get("X-Block-Hash")
previousHashID := msg.Header.Get("X-Previous-Hash-Id")
shardID := msg.Header.Get("X-Shard-Id")
blob := msg.Data
cch.SetIfBlobIsBigger(previousHashID, shardID, blob)
logrus.WithFields(map[string]interface{}{
"previous_hash_id": previousHashID,
"shard_id": shardID,
"block_hash": blockHash,
"msg_id": msg.Header.Get(nats.MsgIdHdr),
}).Info("Processed message on nats subject: ", msg.Subject)
}
func listenForShard(ctx context.Context, ns *nats.Conn, shard string, output chan *nats.Msg) {
shardSubject := *config.FlagShardPrefix + "." + shard
subscription, err := ns.ChanSubscribe(shardSubject, output)
if err != nil {
logrus.Error(err)
}
logrus.Info("Listening for shard subject: ", shardSubject)
<-ctx.Done()
_ = subscription.Drain()
}