Posted on 

limit-conn report error attribute does not exist

https://github.com/apache/apisix/pull/9663/files

install depends

redis

install redis and start the redis service

1
2
3
4
5
6
7
8
9
10
$ sudo apt install redis                                                                                                  100 [09:23:03]
[sudo] password for guohao:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
redis is already the newest version (5:6.0.16-1ubuntu1).
The following package was automatically installed and is no longer required:
openjdk-11-jre
Use 'sudo apt autoremove' to remove it.
0 upgraded, 0 newly installed, 0 to remove and 17 not upgraded.

test the redis

1
2
3
4
5
$ redis-cli -h localhost
localhost:6379> set a test
OK
localhost:6379> get a
"test"

enable APISIX stream proxy

update the conf/config-default.yaml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
diff --git a/conf/config-default.yaml b/conf/config-default.yaml
index e40dc174..3d8dd4c4 100755
--- a/conf/config-default.yaml
+++ b/conf/config-default.yaml
@@ -73,15 +73,15 @@ apisix:
# radixtree_uri_with_parameter: similar to radixtree_uri but match URI with parameters. See https://github.com/api7/lua-resty-radixtree/#parameters-in-path for more details.
ssl: radixtree_sni # radixtree_sni: match route by SNI

- # stream_proxy: # TCP/UDP L4 proxy
- # only: true # Enable L4 proxy only without L7 proxy.
- # tcp:
- # - addr: 9100 # Set the TCP proxy listening ports.
- # tls: true
- # - addr: "127.0.0.1:9101"
- # udp: # Set the UDP proxy listening ports.
- # - 9200
- # - "127.0.0.1:9201"
+ stream_proxy: # TCP/UDP L4 proxy
+ only: true # Enable L4 proxy only without L7 proxy.
+ tcp:
+ - addr: 9100 # Set the TCP proxy listening ports.
+ tls: true
+ - addr: "127.0.0.1:9101"
+ udp: # Set the UDP proxy listening ports.
+ - 9200
+ - "127.0.0.1:9201"

# dns_resolver: # If not set, read from `/etc/resolv.conf`
# - 1.1.1.1
@@ -141,7 +141,7 @@ nginx_config: # Config for render the template to generate n
# user: root # Set the execution user of the worker process. This is only
# effective if the master process runs with super-user privileges.
error_log: logs/error.log # Location of the error log.
- error_log_level: warn # Logging level: info, debug, notice, warn, error, crit, alert, or emerg.
+ error_log_level: debug # Logging level: info, debug, notice, warn, error, crit, alert, or emerg.
worker_processes: auto # Automatically determine the optimal number of worker processes based

create proxy

create a proxy and enable the limit-conn plugin with conn 1.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
curl http://127.0.0.1:9180/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
"limit-conn": {
"conn": 5,
"burst": 0,
"default_conn_delay": 0.1,
"rejected_code": 503,
"key_type": "var",
"key": "remote_addr"
}
},
"upstream": {
"type": "none",
"nodes": {
"127.0.0.1:6379": 1
}
}
}
'

reload the APISIX instance

1
2
3
4
5
6
$ make reload                                                                                                                 [09:24:37]
[ info ] reload -> [ Start ]
/home/guohao/apisix/bin/apisix reload
/usr/local/openresty//luajit/bin/luajit ./apisix/cli/apisix.lua reload
Warning! Current maximum number of open file descriptors [1024] is not greater than 1024, please increase user limits by execute 'ulimit -n <new user limits>' , otherwise the performance is low.
[ info ] reload -> [ Done ]

test the proxy

test the stream proxy

1
2
3
4
5
6
7
8
9
$ 2023/07/10 18:35:08 [info] 40958#1379024: *542041 [lua] timers.lua:39: run timer[plugin#server-info], context: ngx.timer
2023/07/10 18:35:08 [info] 40956#1379020: *541986 client disconnected, bytes from/to client:47/171634, bytes from/to upstream:171634/47
2023/07/10 18:35:08 [info] 40956#1379020: *541986 stream [lua] init.lua:1114: stream_log_phase(): enter stream_log_phase while proxying connection, client: 127.0.0.1, server: 127.0.0.1:9101, upstream: "127.0.0.1:6379", bytes from/to client:47/171634, bytes from/to upstream:171634/0
2023/07/10 18:35:08 [debug] 40956#1379020: *541986 stream [lua] init.lua:125: phase_func(): request latency is nil
2023/07/10 18:35:09 [info] 40958#1379024: *542073 [lua] timers.lua:39: run timer[plugin#server-info], context: ngx.timer [09:24:53]
localhost:9101> set key test
OK
localhost:9101> get key
"test"

can’t reproduce the error log.

1
2
3
4
...
2023/07/10 09:26:19 [info] 6971#6971: *242376 stream [lua] init.lua:1114: stream_log_phase(): enter stream_log_phase while proxying connection, client: 127.0.0.1, server: 127.0.0.1:9101, upstream: "127.0.0.1:6379", bytes from/to client:117/18626, bytes from/to upstream:18626/0
2023/07/10 09:26:19 [debug] 6971#6971: *242376 stream [lua] init.lua:125: phase_func(): request latency is nil
...

update the config-default.yaml again.

1
2
3
4
5
6
7
-# xrpc:
-# protocols:
-# - name: pingpong
+xrpc:
+ protocols:
+ - name: redis
+

and create a router again

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
$ curl http://127.0.0.1:9180/apisix/admin/stream_routes/1 -H 'X-API-KEY: edd1c9f034335f136f87ad84b625c8f1' -X PUT -d '
{
"plugins": {
"limit-conn": {
"conn": 5,
"burst": 0,
"default_conn_delay": 0.1,
"rejected_code": 503,
"key_type": "var",
"key": "remote_addr"
}
},
"upstream": {
"type": "none",
"nodes": {
"127.0.0.1:6379": 1
}
},
"protocol": {
"name": "redis"
}
}
'

and the log error

1
2
3
4
5
6
7
8
...
2023/07/10 18:30:34 [error] 40018#1373140: *526473 failed to run log_by_lua*: ...ohao/workspace/apisix/apisix/plugins/limit-conn/init.lua:122: attempt to perform arithmetic on field 'request_time' (a nil value)
stack traceback:
...ohao/workspace/apisix/apisix/plugins/limit-conn/init.lua:122: in function 'phase_func'
/Users/guohao/workspace/apisix/apisix/plugin.lua:1134: in function 'run_plugin'
/Users/guohao/workspace/apisix/apisix/init.lua:1116: in function 'stream_log_phase'
log_by_lua(nginx.conf:113):2: in main chunk while prereading client data, client: 127.0.0.1, server: 127.0.0.1:9101
2023/07/10 18:30:35 [info] 40020#1373151: *526570 [lua] timers.lua:39: run timer[plugin#server-info], context: ngx.timer

At present, the reason for the two differences is unknown, and it takes time to look at the code.