@@ -118,6 +118,8 @@ type RpcRequest struct {
118
118
type RpcResponse struct {
119
119
Connection ConnectionInfo
120
120
Result string
121
+ err error
122
+ errCode int
121
123
}
122
124
123
125
func NewConnectionPool () * ConnectionPool {
@@ -305,52 +307,68 @@ func (pool *ConnectionPool) execute(info ConnectionInfo, req Action) {
305
307
}
306
308
}
307
309
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
+
308
316
func rpcHandler (pool * ConnectionPool ) http.HandlerFunc {
309
317
return func (w http.ResponseWriter , r * http.Request ) {
318
+ var request RpcRequest
319
+ defer r .Body .Close ()
320
+
310
321
if r .Method != http .MethodPost {
311
- http . Error (w , "Invalid request method " , http .StatusMethodNotAllowed )
322
+ writeJSONError (w , "Method not allowed " , http .StatusMethodNotAllowed )
312
323
return
313
324
}
314
- var request RpcRequest
325
+
315
326
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 )
317
328
return
318
329
}
319
- defer r .Body .Close ()
320
330
321
- done := make (chan struct {})
322
331
323
332
log .Infof ("Incoming RPC request: %v" , request .Method )
324
333
if UNSAFE_LOGS {
325
334
log .Debugf ("Full request: %v" , request )
326
335
}
336
+
337
+ var waitResponse chan RpcResponse = make (chan RpcResponse )
327
338
328
- var response RpcResponse
329
339
pool .execute (request .Connection , Action {
330
340
method : request .Method ,
331
341
payload : request .Payload ,
332
342
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 )
336
345
},
337
346
onResponse : func (info ConnectionInfo , result string ) {
338
347
log .Debugf ("RPC response: %v" , result )
339
348
if UNSAFE_LOGS {
340
349
log .Debugf ("Connection: %v" , info )
341
350
}
342
- response = RpcResponse {
351
+ waitResponse <- RpcResponse {
343
352
Connection : info ,
344
353
Result : result ,
354
+ err : nil ,
355
+ errCode : http .StatusOK ,
345
356
}
346
- close (done )
357
+ close (waitResponse )
347
358
},
348
359
})
349
360
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 )
354
372
}
355
373
}
356
374
}
@@ -420,27 +438,6 @@ func parseKeys(localPrivKey, remotePubKey string) (
420
438
421
439
422
440
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
-
444
441
445
442
func main () {
446
443
shutdownInterceptor , err := signal .Intercept ()
@@ -463,10 +460,11 @@ func main() {
463
460
}
464
461
465
462
var pool * ConnectionPool = NewConnectionPool ()
466
- stats (pool )
463
+ startStatsLoop (pool )
467
464
468
465
http .HandleFunc ("/rpc" , rpcHandler (pool ))
469
466
http .HandleFunc ("/" , formHandler )
467
+ http .HandleFunc ("/health" , healthCheckHandler )
470
468
471
469
log .Infof ("Server started at " + LNCD_RECEIVER_HOST + ":" + LNCD_RECEIVER_PORT )
472
470
if err := http .ListenAndServe (LNCD_RECEIVER_HOST + ":" + LNCD_RECEIVER_PORT , nil ); err != nil {
0 commit comments