@@ -11,11 +11,13 @@ import (
1111	"strings" 
1212	"time" 
1313
14+ 	"github.com/NethermindEth/juno/blockchain" 
1415	"github.com/NethermindEth/juno/db" 
1516	junogrpc "github.com/NethermindEth/juno/grpc" 
1617	"github.com/NethermindEth/juno/grpc/gen" 
1718	"github.com/NethermindEth/juno/jsonrpc" 
1819	"github.com/NethermindEth/juno/service" 
20+ 	"github.com/NethermindEth/juno/sync" 
1921	"github.com/NethermindEth/juno/utils" 
2022	"github.com/prometheus/client_golang/prometheus" 
2123	"github.com/prometheus/client_golang/prometheus/promhttp" 
@@ -72,7 +74,7 @@ func exactPathServer(path string, handler http.Handler) http.HandlerFunc {
7274}
7375
7476func  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 ,
7678) * httpService  {
7779	var  listener  jsonrpc.NewRequestListener 
7880	if  metricsEnabled  {
@@ -87,6 +89,9 @@ func makeRPCOverHTTP(host string, port uint16, servers map[string]*jsonrpc.Serve
8789		}
8890		mux .Handle (path , exactPathServer (path , httpHandler ))
8991	}
92+ 	for  path , handler  :=  range  httpHandlers  {
93+ 		mux .HandleFunc (path , handler )
94+ 	}
9095
9196	var  handler  http.Handler  =  mux 
9297	if  corsEnabled  {
@@ -110,6 +115,7 @@ func makeRPCOverWebsocket(host string, port uint16, servers map[string]*jsonrpc.
110115			wsHandler  =  wsHandler .WithListener (listener )
111116		}
112117		mux .Handle (path , exactPathServer (path , wsHandler ))
118+ 
113119		wsPrefixedPath  :=  strings .TrimSuffix ("/ws" + path , "/" )
114120		mux .Handle (wsPrefixedPath , exactPathServer (wsPrefixedPath , wsHandler ))
115121	}
@@ -179,3 +185,43 @@ func makePPROF(host string, port uint16) *httpService {
179185	mux .HandleFunc ("/debug/pprof/trace" , pprof .Trace )
180186	return  makeHTTPService (host , port , mux )
181187}
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