This is an automated email from the ASF dual-hosted git repository.

membphis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/apisix.git


The following commit(s) were added to refs/heads/master by this push:
     new 216debe  feat: support priority in SRV record (#3845)
216debe is described below

commit 216debe22354af8f0bf8691d1bd8f15cd4af12c9
Author: 罗泽轩 <spacewander...@gmail.com>
AuthorDate: Tue Mar 23 20:05:23 2021 +0800

    feat: support priority in SRV record (#3845)
---
 apisix/discovery/dns.lua        |  4 ++++
 docs/en/latest/discovery/dns.md | 15 ++++++++-------
 t/coredns/db.test.local         |  3 +++
 t/discovery/dns/sanity.t        | 19 +++++++++++++++++++
 4 files changed, 34 insertions(+), 7 deletions(-)

diff --git a/apisix/discovery/dns.lua b/apisix/discovery/dns.lua
index 64ffe1d..e599cf6 100644
--- a/apisix/discovery/dns.lua
+++ b/apisix/discovery/dns.lua
@@ -53,6 +53,10 @@ function _M.nodes(service_name)
     for i, r in ipairs(records) do
         if r.address then
             nodes[i] = {host = r.address, weight = r.weight or 1, port = 
r.port or port}
+            if r.priority then
+                -- for SRV record, nodes with lower priority are chosen first
+                nodes[i].priority = -r.priority
+            end
         end
     end
 
diff --git a/docs/en/latest/discovery/dns.md b/docs/en/latest/discovery/dns.md
index 0fd7977..4bb5461 100644
--- a/docs/en/latest/discovery/dns.md
+++ b/docs/en/latest/discovery/dns.md
@@ -92,8 +92,10 @@ Assumed you have the SRV record like this:
 A       300 IN      A     1.1.1.1
 B       300 IN      A     1.1.1.2
 B       300 IN      A     1.1.1.3
-srv   86400 IN    SRV 10       60     1980 A
-srv   86400 IN    SRV 10       20     1981 B
+
+; name  TTL         type    priority    weight  port
+srv     86400 IN    SRV     10          60      1980 A
+srv     86400 IN    SRV     20          20      1981 B
 ```
 
 Upstream configuration like:
@@ -114,14 +116,15 @@ is the same as:
     "id": 1,
     "type": "roundrobin",
     "nodes": [
-        {"host": "1.1.1.1", "port": 1980, "weight": 60},
-        {"host": "1.1.1.2", "port": 1981, "weight": 10},
-        {"host": "1.1.1.3", "port": 1981, "weight": 10}
+        {"host": "1.1.1.1", "port": 1980, "weight": 60, "priority": -10},
+        {"host": "1.1.1.2", "port": 1981, "weight": 10, "priority": -20},
+        {"host": "1.1.1.3", "port": 1981, "weight": 10, "priority": -20}
     ]
 }
 ```
 
 Note that two records of domain B split the weight evenly.
+For SRV record, nodes with lower priority are chosen first, so the final 
priority is negative.
 
 As for 0 weight SRV record, the [RFC 
2782](https://www.ietf.org/rfc/rfc2782.txt) says:
 
@@ -133,5 +136,3 @@ being selected.
 
 We treat weight 0 record has a weight of 1 so the node "have a very small 
chance of
 being selected", which is also the common way to treat this type of record.
-
-TODO: support priority.
diff --git a/t/coredns/db.test.local b/t/coredns/db.test.local
index e4bb2fa..27296bf 100644
--- a/t/coredns/db.test.local
+++ b/t/coredns/db.test.local
@@ -42,3 +42,6 @@ zero-weight.srv   86400 IN    SRV 10       0      1980 B
 
 split-weight.srv   86400 IN    SRV 10      100   1980 A
 split-weight.srv   86400 IN    SRV 10      0     1980 C
+
+priority.srv   86400 IN    SRV 10       60     1979 A
+priority.srv   86400 IN    SRV 20       60     1980 B
diff --git a/t/discovery/dns/sanity.t b/t/discovery/dns/sanity.t
index 89b67a1..753b9bb 100644
--- a/t/discovery/dns/sanity.t
+++ b/t/discovery/dns/sanity.t
@@ -266,3 +266,22 @@ qr/upstream nodes: \{[^}]+\}/
 qr/upstream nodes: \{(,?"127.0.0.(1:1980":200|3:1980":1|4:1980":1)){3}\}/
 --- response_body
 hello world
+
+
+
+=== TEST 13: SRV (priority)
+--- apisix_yaml
+upstreams:
+    - service_name: "priority.srv.test.local"
+      discovery_type: dns
+      type: roundrobin
+      id: 1
+--- response_body
+hello world
+--- error_log
+connect() failed
+--- grep_error_log eval
+qr/proxy request to \S+/
+--- grep_error_log_out
+proxy request to 127.0.0.1:1979
+proxy request to 127.0.0.2:1980

Reply via email to