@@ -11,11 +11,13 @@ import (
11
11
"strings"
12
12
"time"
13
13
14
+ "github.com/NethermindEth/juno/blockchain"
14
15
"github.com/NethermindEth/juno/db"
15
16
junogrpc "github.com/NethermindEth/juno/grpc"
16
17
"github.com/NethermindEth/juno/grpc/gen"
17
18
"github.com/NethermindEth/juno/jsonrpc"
18
19
"github.com/NethermindEth/juno/service"
20
+ "github.com/NethermindEth/juno/sync"
19
21
"github.com/NethermindEth/juno/utils"
20
22
"github.com/prometheus/client_golang/prometheus"
21
23
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -72,7 +74,7 @@ func exactPathServer(path string, handler http.Handler) http.HandlerFunc {
72
74
}
73
75
74
76
func makeRPCOverHTTP (host string , port uint16 , servers map [string ]* jsonrpc.Server ,
75
- log utils.SimpleLogger , metricsEnabled bool , corsEnabled bool ,
77
+ httpHandlers map [ string ]http. HandlerFunc , log utils.SimpleLogger , metricsEnabled bool , corsEnabled bool ,
76
78
) * httpService {
77
79
var listener jsonrpc.NewRequestListener
78
80
if metricsEnabled {
@@ -87,6 +89,9 @@ func makeRPCOverHTTP(host string, port uint16, servers map[string]*jsonrpc.Serve
87
89
}
88
90
mux .Handle (path , exactPathServer (path , httpHandler ))
89
91
}
92
+ for path , handler := range httpHandlers {
93
+ mux .HandleFunc (path , handler )
94
+ }
90
95
91
96
var handler http.Handler = mux
92
97
if corsEnabled {
@@ -110,6 +115,7 @@ func makeRPCOverWebsocket(host string, port uint16, servers map[string]*jsonrpc.
110
115
wsHandler = wsHandler .WithListener (listener )
111
116
}
112
117
mux .Handle (path , exactPathServer (path , wsHandler ))
118
+
113
119
wsPrefixedPath := strings .TrimSuffix ("/ws" + path , "/" )
114
120
mux .Handle (wsPrefixedPath , exactPathServer (wsPrefixedPath , wsHandler ))
115
121
}
@@ -179,3 +185,43 @@ func makePPROF(host string, port uint16) *httpService {
179
185
mux .HandleFunc ("/debug/pprof/trace" , pprof .Trace )
180
186
return makeHTTPService (host , port , mux )
181
187
}
188
+
189
+ const SyncBlockRange = 6
190
+
191
+ type readinessHandlers struct {
192
+ bcReader blockchain.Reader
193
+ syncReader sync.Reader
194
+ }
195
+
196
+ func NewReadinessHandlers (bcReader blockchain.Reader , syncReader sync.Reader ) * readinessHandlers {
197
+ return & readinessHandlers {
198
+ bcReader : bcReader ,
199
+ syncReader : syncReader ,
200
+ }
201
+ }
202
+
203
+ func (h * readinessHandlers ) HandleReadySync (w http.ResponseWriter , r * http.Request ) {
204
+ if ! h .isSynced () {
205
+ w .WriteHeader (http .StatusServiceUnavailable )
206
+ return
207
+ }
208
+
209
+ w .WriteHeader (http .StatusOK )
210
+ }
211
+
212
+ func (h * readinessHandlers ) isSynced () bool {
213
+ head , err := h .bcReader .HeadsHeader ()
214
+ if err != nil {
215
+ return false
216
+ }
217
+ highestBlockHeader := h .syncReader .HighestBlockHeader ()
218
+ if highestBlockHeader == nil {
219
+ return false
220
+ }
221
+
222
+ if head .Number > highestBlockHeader .Number {
223
+ return false
224
+ }
225
+
226
+ return head .Number + SyncBlockRange >= highestBlockHeader .Number
227
+ }
0 commit comments