@@ -6,7 +6,7 @@ workers(1);
6
6
my $ pwd = cwd();
7
7
8
8
our $ HttpConfig = qq {
9
- lua_package_path "$ pwd /lib/?.lua;;";
9
+ lua_package_path "$ pwd /lib/?.lua;$ pwd /t/?.lua; ;";
10
10
lua_shared_dict test_shm 8m;
11
11
lua_shared_dict my_worker_events 8m;
12
12
} ;
@@ -499,3 +499,146 @@ event: target status '(127.0.0.1:2119)' from 'true' to 'false'
499
499
healthy SUCCESS increment (1 / 2 )
500
500
healthy SUCCESS increment (2 / 2 )
501
501
event: target status ' (127.0.0.1:2119)' from ' false' to ' true'
502
+
503
+
504
+
505
+ === TEST 8 : start xrpc healthcheck with xrpc_conf
506
+ -- - http_config eval
507
+ qq {
508
+ $::HttpConfig
509
+
510
+ server {
511
+ listen 2119 ;
512
+ location = /status {
513
+ return 200 ;
514
+ }
515
+ }
516
+ }
517
+ --- config
518
+ location = /t {
519
+ content_by_lua_block {
520
+ local we = require " resty.worker.events"
521
+ assert(we. configure{ shm = " my_worker_events" , interval = 0.1 })
522
+ local healthcheck = require (" resty.healthcheck" )
523
+ local checker = healthcheck. new ({
524
+ name = " testing" ,
525
+ shm_name = " test_shm" ,
526
+ type = " xrpc" ,
527
+ checks = {
528
+ active = {
529
+ healthy = {
530
+ interval = 0.5 , -- we don't want active checks
531
+ successes = 2 ,
532
+ },
533
+ unhealthy = {
534
+ interval = 0.5 , -- we don't want active checks
535
+ failures = 2 ,
536
+ },
537
+ xrpc_conf = {
538
+ timeout = 1000 ,
539
+ },
540
+ xrpc_handler = function(node, conf)
541
+ ngx. log (ngx. INFO, " node.host: " , node. host, " , node.port: " , node. port)
542
+ ngx. log (ngx. INFO, " conf.timeout: " , conf. timeout)
543
+ end
544
+ },
545
+ },
546
+ })
547
+ ngx. sleep (0.1 ) -- wait for initial timers to run once
548
+ checker: add_target(" 127.0.0.1" , 2119 , nil, true)
549
+ ngx. sleep (1 )
550
+ }
551
+ }
552
+ -- - request
553
+ GET / t
554
+ -- - timeout: 10
555
+ -- - error_log
556
+ node. host: 127.0 . 0. 1, node. port: 2119
557
+ conf. timeout: 1000
558
+
559
+
560
+
561
+ === TEST 9 : start xrpc healthcheck with xrpc_function_module
562
+ -- - http_config eval
563
+ qq {
564
+ $::HttpConfig
565
+
566
+ lua_shared_dict request_counters 1m;
567
+
568
+ server {
569
+ listen 2119 ;
570
+
571
+ location / status {
572
+ content_by_lua_block {
573
+ local uri = ngx. var. request_uri
574
+ local counter = ngx. shared. request_counters
575
+
576
+ counter: incr(uri, 1 , 0 )
577
+
578
+ local current_count = counter: get(uri) or 0
579
+
580
+ if current_count < 4 or current_count > 8 then
581
+ ngx. status = 200
582
+ ngx. say (" OK" )
583
+ else
584
+ ngx. status = 500
585
+ ngx. say (" ERROR" )
586
+ end
587
+ }
588
+ }
589
+ }
590
+ }
591
+ -- - config
592
+ location = /t {
593
+ content_by_lua_block {
594
+ local we = require " resty.worker.events"
595
+ assert(we. configure{ shm = " my_worker_events" , interval = 0.1 })
596
+ local healthcheck = require (" resty.healthcheck" )
597
+ local checker, err = healthcheck. new ({
598
+ name = " testing" ,
599
+ shm_name = " test_shm" ,
600
+ type = " xrpc" ,
601
+ checks = {
602
+ active = {
603
+ healthy = {
604
+ interval = 0.5 , -- we don't want active checks
605
+ successes = 2 ,
606
+ },
607
+ unhealthy = {
608
+ interval = 0.5 , -- we don't want active checks
609
+ failures = 2 ,
610
+ },
611
+ xrpc_function_module = " util.http_handler" ,
612
+ },
613
+ },
614
+ })
615
+ if not checker then
616
+ ngx. log (ngx. ERR, " failed to create healthcheck: " , err)
617
+ return
618
+ end
619
+ ngx. sleep (0.1 ) -- wait for initial timers to run once
620
+ checker: add_target(" 127.0.0.1" , 2119 , nil, true)
621
+ ngx. sleep (0.5 )
622
+ ngx. say (checker: get_target_status(" 127.0.0.1" , 2119 , nil)) -- true
623
+ ngx. sleep (3.1 )
624
+ ngx. say (checker: get_target_status(" 127.0.0.1" , 2119 , nil)) -- false
625
+ ngx. sleep (4.2 )
626
+ ngx. say (checker: get_target_status(" 127.0.0.1" , 2119 , nil)) -- true
627
+ }
628
+ }
629
+ --- request
630
+ GET /t
631
+ -- - timeout: 10
632
+ -- - response_body
633
+ true
634
+ false
635
+ true
636
+ -- - error_log
637
+ checking healthy targets: nothing to do
638
+ checking unhealthy targets: nothing to do
639
+ unhealthy XRPC increment (1 / 2 ) for ' (127.0.0.1:2119)'
640
+ unhealthy XRPC increment (2 / 2 ) for ' (127.0.0.1:2119)'
641
+ event: target status ' (127.0.0.1:2119)' from ' true' to ' false'
642
+ healthy SUCCESS increment (1 / 2 )
643
+ healthy SUCCESS increment (2 / 2 )
644
+ event: target status ' (127.0.0.1:2119)' from ' false' to ' true'
0 commit comments