@@ -118,6 +118,8 @@ type RpcRequest struct {
118118type RpcResponse struct {
119119 Connection ConnectionInfo
120120 Result string
121+ err error
122+ errCode int
121123}
122124
123125func NewConnectionPool () * ConnectionPool {
@@ -305,52 +307,68 @@ func (pool *ConnectionPool) execute(info ConnectionInfo, req Action) {
305307 }
306308}
307309
310+ func writeJSONError (w http.ResponseWriter , message string , statusCode int ) {
311+ w .Header ().Set ("Content-Type" , "application/json" )
312+ w .WriteHeader (statusCode )
313+ json .NewEncoder (w ).Encode (map [string ]string {"error" : message })
314+ }
315+
308316func rpcHandler (pool * ConnectionPool ) http.HandlerFunc {
309317 return func (w http.ResponseWriter , r * http.Request ) {
318+ var request RpcRequest
319+ defer r .Body .Close ()
320+
310321 if r .Method != http .MethodPost {
311- http . Error (w , "Invalid request method " , http .StatusMethodNotAllowed )
322+ writeJSONError (w , "Method not allowed " , http .StatusMethodNotAllowed )
312323 return
313324 }
314- var request RpcRequest
325+
315326 if err := json .NewDecoder (r .Body ).Decode (& request ); err != nil {
316- http . Error (w , "Invalid JSON" , http .StatusBadRequest )
327+ writeJSONError (w , err . Error () , http .StatusBadRequest )
317328 return
318329 }
319- defer r .Body .Close ()
320330
321- done := make (chan struct {})
322331
323332 log .Infof ("Incoming RPC request: %v" , request .Method )
324333 if UNSAFE_LOGS {
325334 log .Debugf ("Full request: %v" , request )
326335 }
336+
337+ var waitResponse chan RpcResponse = make (chan RpcResponse )
327338
328- var response RpcResponse
329339 pool .execute (request .Connection , Action {
330340 method : request .Method ,
331341 payload : request .Payload ,
332342 onError : func (err error ) {
333- log .Errorf ("RPC error: %v" , err )
334- http .Error (w , err .Error (), http .StatusInternalServerError )
335- close (done )
343+ waitResponse <- RpcResponse {err : err , errCode : http .StatusInternalServerError }
344+ close (waitResponse )
336345 },
337346 onResponse : func (info ConnectionInfo , result string ) {
338347 log .Debugf ("RPC response: %v" , result )
339348 if UNSAFE_LOGS {
340349 log .Debugf ("Connection: %v" , info )
341350 }
342- response = RpcResponse {
351+ waitResponse <- RpcResponse {
343352 Connection : info ,
344353 Result : result ,
354+ err : nil ,
355+ errCode : http .StatusOK ,
345356 }
346- close (done )
357+ close (waitResponse )
347358 },
348359 })
349360
350- <- done
351- w .Header ().Set ("Content-Type" , "application/json" )
352- if err := json .NewEncoder (w ).Encode (response ); err != nil {
353- http .Error (w , err .Error (), http .StatusInternalServerError )
361+ var resp RpcResponse = <- waitResponse
362+ if resp .err == nil {
363+ w .Header ().Set ("Content-Type" , "application/json" )
364+ if err := json .NewEncoder (w ).Encode (resp ); err != nil {
365+ resp .err = err
366+ resp .errCode = http .StatusInternalServerError
367+ }
368+ }
369+
370+ if resp .err != nil {
371+ writeJSONError (w , resp .err .Error (), resp .errCode )
354372 }
355373 }
356374}
@@ -420,27 +438,6 @@ func parseKeys(localPrivKey, remotePubKey string) (
420438
421439
422440
423- func stats (pool * ConnectionPool ) {
424- ticker := time .NewTicker (LNCD_STATS_INTERVAL )
425- go func () {
426- for range ticker .C {
427- pool .mutex .Lock ()
428- numConnections := len (pool .connections )
429- log .Infof ("Number of active connections: %d" , numConnections )
430-
431- index := 0
432- for _ , conn := range pool .connections {
433- log .Infof ("Connection %d" , index )
434- pendingActions := len (conn .actions )
435- log .Infof (" Pending actions: %d" , pendingActions )
436- log .Infof (" Connection status: %v" , conn .connInfo .Status )
437- index ++
438- }
439- pool .mutex .Unlock ()
440- }
441- }()
442- }
443-
444441
445442func main () {
446443 shutdownInterceptor , err := signal .Intercept ()
@@ -463,10 +460,11 @@ func main() {
463460 }
464461
465462 var pool * ConnectionPool = NewConnectionPool ()
466- stats (pool )
463+ startStatsLoop (pool )
467464
468465 http .HandleFunc ("/rpc" , rpcHandler (pool ))
469466 http .HandleFunc ("/" , formHandler )
467+ http .HandleFunc ("/health" , healthCheckHandler )
470468
471469 log .Infof ("Server started at " + LNCD_RECEIVER_HOST + ":" + LNCD_RECEIVER_PORT )
472470 if err := http .ListenAndServe (LNCD_RECEIVER_HOST + ":" + LNCD_RECEIVER_PORT , nil ); err != nil {
0 commit comments