Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
aea17570
Commit
aea17570
authored
May 16, 2011
by
Gary Burd
Committed by
Russ Cox
May 16, 2011
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
net: sort records returned by LookupSRV
R=golang-dev, rsc CC=golang-dev
https://golang.org/cl/4518061
parent
8ef0d181
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
49 additions
and
1 deletion
+49
-1
src/pkg/net/dnsclient.go
src/pkg/net/dnsclient.go
+49
-1
No files found.
src/pkg/net/dnsclient.go
View file @
aea17570
...
...
@@ -398,10 +398,49 @@ type SRV struct {
Weight
uint16
}
// byPriorityWeight sorts SRV records by ascending priority and weight.
type
byPriorityWeight
[]
*
SRV
func
(
s
byPriorityWeight
)
Len
()
int
{
return
len
(
s
)
}
func
(
s
byPriorityWeight
)
Swap
(
i
,
j
int
)
{
s
[
i
],
s
[
j
]
=
s
[
j
],
s
[
i
]
}
func
(
s
byPriorityWeight
)
Less
(
i
,
j
int
)
bool
{
return
s
[
i
]
.
Priority
<
s
[
j
]
.
Priority
||
(
s
[
i
]
.
Priority
==
s
[
j
]
.
Priority
&&
s
[
i
]
.
Weight
<
s
[
j
]
.
Weight
)
}
// shuffleSRVByWeight shuffles SRV records by weight using the algorithm
// described in RFC 2782.
func
shuffleSRVByWeight
(
addrs
[]
*
SRV
)
{
sum
:=
0
for
_
,
addr
:=
range
addrs
{
sum
+=
int
(
addr
.
Weight
)
}
for
sum
>
0
&&
len
(
addrs
)
>
1
{
s
:=
0
n
:=
rand
.
Intn
(
sum
+
1
)
for
i
:=
range
addrs
{
s
+=
int
(
addrs
[
i
]
.
Weight
)
if
s
>=
n
{
if
i
>
0
{
t
:=
addrs
[
i
]
copy
(
addrs
[
1
:
i
+
1
],
addrs
[
0
:
i
])
addrs
[
0
]
=
t
}
break
}
}
sum
-=
int
(
addrs
[
0
]
.
Weight
)
addrs
=
addrs
[
1
:
]
}
}
// LookupSRV tries to resolve an SRV query of the given service,
// protocol, and domain name, as specified in RFC 2782. In most cases
// the proto argument can be the same as the corresponding
// Addr.Network().
// Addr.Network(). The returned records are sorted by priority
// and randomized by weight within a priority.
func
LookupSRV
(
service
,
proto
,
name
string
)
(
cname
string
,
addrs
[]
*
SRV
,
err
os
.
Error
)
{
target
:=
"_"
+
service
+
"._"
+
proto
+
"."
+
name
var
records
[]
dnsRR
...
...
@@ -414,6 +453,15 @@ func LookupSRV(service, proto, name string) (cname string, addrs []*SRV, err os.
r
:=
rr
.
(
*
dnsRR_SRV
)
addrs
[
i
]
=
&
SRV
{
r
.
Target
,
r
.
Port
,
r
.
Priority
,
r
.
Weight
}
}
sort
.
Sort
(
byPriorityWeight
(
addrs
))
i
:=
0
for
j
:=
1
;
j
<
len
(
addrs
);
j
++
{
if
addrs
[
i
]
.
Priority
!=
addrs
[
j
]
.
Priority
{
shuffleSRVByWeight
(
addrs
[
i
:
j
])
i
=
j
}
}
shuffleSRVByWeight
(
addrs
[
i
:
len
(
addrs
)])
return
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment