mirror of
https://github.com/coredns/coredns.git
synced 2025-12-06 10:25:10 -05:00
plugin/forward: Allow Proxy to be used outside of forward plugin. (#5951)
* plugin/forward: Move Proxy into pkg/plugin/proxy, to allow forward.Proxy to be used outside of forward plugin. Signed-off-by: Patrick Downey <patrick.downey@dioadconsulting.com>
This commit is contained in:
@@ -4,12 +4,13 @@ import (
|
||||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/coredns/coredns/plugin/pkg/proxy"
|
||||
"github.com/coredns/coredns/plugin/pkg/rand"
|
||||
)
|
||||
|
||||
// Policy defines a policy we use for selecting upstreams.
|
||||
type Policy interface {
|
||||
List([]*Proxy) []*Proxy
|
||||
List([]*proxy.Proxy) []*proxy.Proxy
|
||||
String() string
|
||||
}
|
||||
|
||||
@@ -18,19 +19,19 @@ type random struct{}
|
||||
|
||||
func (r *random) String() string { return "random" }
|
||||
|
||||
func (r *random) List(p []*Proxy) []*Proxy {
|
||||
func (r *random) List(p []*proxy.Proxy) []*proxy.Proxy {
|
||||
switch len(p) {
|
||||
case 1:
|
||||
return p
|
||||
case 2:
|
||||
if rn.Int()%2 == 0 {
|
||||
return []*Proxy{p[1], p[0]} // swap
|
||||
return []*proxy.Proxy{p[1], p[0]} // swap
|
||||
}
|
||||
return p
|
||||
}
|
||||
|
||||
perms := rn.Perm(len(p))
|
||||
rnd := make([]*Proxy, len(p))
|
||||
rnd := make([]*proxy.Proxy, len(p))
|
||||
|
||||
for i, p1 := range perms {
|
||||
rnd[i] = p[p1]
|
||||
@@ -45,11 +46,11 @@ type roundRobin struct {
|
||||
|
||||
func (r *roundRobin) String() string { return "round_robin" }
|
||||
|
||||
func (r *roundRobin) List(p []*Proxy) []*Proxy {
|
||||
func (r *roundRobin) List(p []*proxy.Proxy) []*proxy.Proxy {
|
||||
poolLen := uint32(len(p))
|
||||
i := atomic.AddUint32(&r.robin, 1) % poolLen
|
||||
|
||||
robin := []*Proxy{p[i]}
|
||||
robin := []*proxy.Proxy{p[i]}
|
||||
robin = append(robin, p[:i]...)
|
||||
robin = append(robin, p[i+1:]...)
|
||||
|
||||
@@ -61,7 +62,7 @@ type sequential struct{}
|
||||
|
||||
func (r *sequential) String() string { return "sequential" }
|
||||
|
||||
func (r *sequential) List(p []*Proxy) []*Proxy {
|
||||
func (r *sequential) List(p []*proxy.Proxy) []*proxy.Proxy {
|
||||
return p
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user