aboutsummaryrefslogtreecommitdiff
path: root/plugin/grpc
diff options
context:
space:
mode:
authorGravatar Zou Nengren <zounengren@cmss.chinamobile.com> 2019-12-17 16:15:31 +0800
committerGravatar Miek Gieben <miek@miek.nl> 2019-12-17 08:15:31 +0000
commit5e04c272382a7d7aca15f8ac9a8100a373ca1d1e (patch)
treedcfe54de270b62c2ecadb054e5d1d0549b3b81b4 /plugin/grpc
parentacb75ea904c2405b7b821d90d8cb7bf6a7e3f7df (diff)
downloadcoredns-5e04c272382a7d7aca15f8ac9a8100a373ca1d1e.tar.gz
coredns-5e04c272382a7d7aca15f8ac9a8100a373ca1d1e.tar.zst
coredns-5e04c272382a7d7aca15f8ac9a8100a373ca1d1e.zip
Dedup policy implement between grpc and proxy plugin (#3537)
Signed-off-by: zouyee <zounengren@cmss.chinamobile.com>
Diffstat (limited to 'plugin/grpc')
-rw-r--r--plugin/grpc/grpc.go12
-rw-r--r--plugin/grpc/policy.go64
-rw-r--r--plugin/grpc/setup.go7
3 files changed, 13 insertions, 70 deletions
diff --git a/plugin/grpc/grpc.go b/plugin/grpc/grpc.go
index 3dda225df..878b6c3f4 100644
--- a/plugin/grpc/grpc.go
+++ b/plugin/grpc/grpc.go
@@ -7,6 +7,7 @@ import (
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/debug"
+ "github.com/coredns/coredns/plugin/pkg/policy"
"github.com/coredns/coredns/request"
"github.com/miekg/dns"
@@ -17,7 +18,7 @@ import (
// It has a list of proxies each representing one upstream proxy.
type GRPC struct {
proxies []*Proxy
- p Policy
+ p policy.Policy
from string
ignored []string
@@ -93,7 +94,7 @@ func (g *GRPC) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (
// NewGRPC returns a new GRPC.
func newGRPC() *GRPC {
g := &GRPC{
- p: new(random),
+ p: new(policy.Random),
}
return g
}
@@ -126,6 +127,11 @@ func (g *GRPC) isAllowedDomain(name string) bool {
}
// List returns a set of proxies to be used for this client depending on the policy in p.
-func (g *GRPC) list() []*Proxy { return g.p.List(g.proxies) }
+func (g *GRPC) list() []*Proxy {
+ if len(g.p.List(g.proxies)) == 1 {
+ return g.p.List(g.proxies)[0].([]*Proxy)
+ }
+ return nil
+}
const defaultTimeout = 5 * time.Second
diff --git a/plugin/grpc/policy.go b/plugin/grpc/policy.go
deleted file mode 100644
index 66351d822..000000000
--- a/plugin/grpc/policy.go
+++ /dev/null
@@ -1,64 +0,0 @@
-package grpc
-
-import (
- "math/rand"
- "sync/atomic"
-)
-
-// Policy defines a policy we use for selecting upstreams.
-type Policy interface {
- List([]*Proxy) []*Proxy
- String() string
-}
-
-// random is a policy that implements random upstream selection.
-type random struct{}
-
-func (r *random) String() string { return "random" }
-
-func (r *random) List(p []*Proxy) []*Proxy {
- switch len(p) {
- case 1:
- return p
- case 2:
- if rand.Int()%2 == 0 {
- return []*Proxy{p[1], p[0]} // swap
- }
- return p
- }
-
- perms := rand.Perm(len(p))
- rnd := make([]*Proxy, len(p))
-
- for i, p1 := range perms {
- rnd[i] = p[p1]
- }
- return rnd
-}
-
-// roundRobin is a policy that selects hosts based on round robin ordering.
-type roundRobin struct {
- robin uint32
-}
-
-func (r *roundRobin) String() string { return "round_robin" }
-
-func (r *roundRobin) List(p []*Proxy) []*Proxy {
- poolLen := uint32(len(p))
- i := atomic.AddUint32(&r.robin, 1) % poolLen
-
- robin := []*Proxy{p[i]}
- robin = append(robin, p[:i]...)
- robin = append(robin, p[i+1:]...)
-
- return robin
-}
-
-// sequential is a policy that selects hosts based on sequential ordering.
-type sequential struct{}
-
-func (r *sequential) String() string { return "sequential" }
-
-func (r *sequential) List(p []*Proxy) []*Proxy {
- return p
-}
diff --git a/plugin/grpc/setup.go b/plugin/grpc/setup.go
index a234efb37..c178932c4 100644
--- a/plugin/grpc/setup.go
+++ b/plugin/grpc/setup.go
@@ -8,6 +8,7 @@ import (
"github.com/coredns/coredns/plugin"
"github.com/coredns/coredns/plugin/metrics"
"github.com/coredns/coredns/plugin/pkg/parse"
+ "github.com/coredns/coredns/plugin/pkg/policy"
pkgtls "github.com/coredns/coredns/plugin/pkg/tls"
"github.com/caddyserver/caddy"
@@ -132,11 +133,11 @@ func parseBlock(c *caddy.Controller, g *GRPC) error {
}
switch x := c.Val(); x {
case "random":
- g.p = &random{}
+ g.p = &policy.Random{}
case "round_robin":
- g.p = &roundRobin{}
+ g.p = &policy.RoundRobin{}
case "sequential":
- g.p = &sequential{}
+ g.p = &policy.Sequential{}
default:
return c.Errf("unknown policy '%s'", x)
}