Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ clone:
depth: 1

steps:
- name: lint
image: golangci/golangci-lint:v1.48-alpine
volumes:
- name: deps
path: /go
commands:
- golangci-lint run -v
- go install github.com/mgechev/revive@v1.2.3
- revive -config .revive.toml -formatter friendly ./...

- name: test
image: golang:1.19-alpine
environment:
Expand Down
31 changes: 31 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
linters:
enable:
- dogsled
- errname
- errorlint
- exportloopref
- funlen
- gocognit
- goconst
- gocritic
- gocyclo
- gofmt
- goimports
- staticcheck
- gosec
- govet
- misspell
- nestif
- prealloc
- unconvert
- unparam

run:
timeout: 5m

issues:
exclude-rules:
- linters:
- funlen
- gosec
path: _test\.go
44 changes: 44 additions & 0 deletions .revive.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
ignoreGeneratedHeader = false
severity = "warning"
confidence = 0.8
errorCode = 1
warningCode = 1

#Recommended rules
[rule.blank-imports]
[rule.context-as-argument]
[rule.context-keys-type]
[rule.dot-imports]
[rule.error-return]
[rule.error-strings]
[rule.error-naming]
[rule.if-return]
[rule.increment-decrement]
[rule.var-naming]
[rule.var-declaration]
[rule.package-comments]
[rule.range]
[rule.receiver-naming]
[rule.time-naming]
[rule.unexported-return]
[rule.indent-error-flow]
[rule.errorf]
[rule.empty-block]
[rule.superfluous-else]
[rule.unused-parameter]
[rule.unreachable-code]
[rule.redefines-builtin-id]

#Custom rules
[rule.modifies-parameter]
[rule.unnecessary-stmt]
[rule.modifies-value-receiver]
[rule.range-val-in-closure]
[rule.range-val-address]
[rule.waitgroup-by-value]
[rule.atomic]
[rule.unused-receiver]
[rule.early-return]
[rule.unconditional-recursion]
[rule.identical-branches]
[rule.defer]
2 changes: 1 addition & 1 deletion https.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func newHTTPS(from string, client dnsClient, opts ...httpsOption) *HTTPS {
var _ plugin.Handler = (*HTTPS)(nil)

// Name implements plugin.Handler.
func (h *HTTPS) Name() string { return "https" }
func (*HTTPS) Name() string { return "https" }

// ServeDNS implements plugin.Handler.
func (h *HTTPS) ServeDNS(ctx context.Context, w dns.ResponseWriter, r *dns.Msg) (status int, err error) {
Expand Down
34 changes: 17 additions & 17 deletions policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ func TestRandomPolicy(t *testing.T) {
name: "OneElement",
poolLen: 1,
expected: [][]int{
[]int{0},
[]int{0},
{0},
{0},
},
},
{
Expand Down Expand Up @@ -92,27 +92,27 @@ func TestRoundRobinPolicy(t *testing.T) {
name: "OneElement",
poolLen: 1,
expected: [][]int{
[]int{0},
[]int{0},
{0},
{0},
},
},
{
name: "TwoElements",
poolLen: 2,
expected: [][]int{
[]int{0, 1},
[]int{1, 0},
[]int{0, 1},
{0, 1},
{1, 0},
{0, 1},
},
},
{
name: "ThreeElements",
poolLen: 3,
expected: [][]int{
[]int{0, 1, 2},
[]int{1, 2, 0},
[]int{2, 0, 1},
[]int{0, 1, 2},
{0, 1, 2},
{1, 2, 0},
{2, 0, 1},
{0, 1, 2},
},
},
}
Expand Down Expand Up @@ -152,24 +152,24 @@ func TestSequentialPolicy(t *testing.T) {
name: "OneElement",
poolLen: 1,
expected: [][]int{
[]int{0},
[]int{0},
{0},
{0},
},
},
{
name: "TwoElements",
poolLen: 2,
expected: [][]int{
[]int{0, 1},
[]int{0, 1},
{0, 1},
{0, 1},
},
},
{
name: "ThreeElements",
poolLen: 3,
expected: [][]int{
[]int{0, 1, 2},
[]int{0, 1, 2},
{0, 1, 2},
{0, 1, 2},
},
},
}
Expand Down
11 changes: 7 additions & 4 deletions proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,12 +163,15 @@ func withLbMaxFails(maxFails int) lbDNSClientOption {
func (c *lbDNSClient) Query(ctx context.Context, dnsreq []byte) (r *dns.Msg, err error) {
ids := c.p.List(len(c.clients))
for i := 0; i < c.maxFails; i++ {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
if r, err = c.clients[ids[i]].Query(ctx, dnsreq); err == nil {
if r, err = c.query(ctx, dnsreq, ids[i]); err == nil {
return
}
cancel()
}
return
}

func (c *lbDNSClient) query(ctx context.Context, dnsreq []byte, clientID int) (*dns.Msg, error) {
ctx, cancel := context.WithTimeout(ctx, c.timeout)
defer cancel()
return c.clients[clientID].Query(ctx, dnsreq)
}
16 changes: 8 additions & 8 deletions proxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"context"
"errors"
"io"
"io/ioutil"
"net"
"net/http"
"strings"
Expand Down Expand Up @@ -70,7 +69,7 @@ func TestDNSClient(t *testing.T) {
require.Equal(t, []byte("abc"), buf, "invalid request body")

resp = &http.Response{
Body: ioutil.NopCloser(bytes.NewReader(packMsg(t, expectedMsg))),
Body: io.NopCloser(bytes.NewReader(packMsg(t, expectedMsg))),
StatusCode: http.StatusOK,
}
return
Expand Down Expand Up @@ -101,7 +100,7 @@ func TestDNSClientHttpClientError(t *testing.T) {
httpClient := mockHTTPClientFunc(func(req *http.Request) (resp *http.Response, err error) {
err = errors.New("http error")
resp = &http.Response{
Body: ioutil.NopCloser(bytes.NewReader(packMsg(t, newExpectedDNSMsg()))),
Body: io.NopCloser(bytes.NewReader(packMsg(t, newExpectedDNSMsg()))),
StatusCode: http.StatusOK,
}
return
Expand All @@ -115,7 +114,7 @@ func TestDNSClientHttpClientError(t *testing.T) {
func TestDNSClientHttpStatusError(t *testing.T) {
httpClient := mockHTTPClientFunc(func(req *http.Request) (resp *http.Response, err error) {
resp = &http.Response{
Body: ioutil.NopCloser(bytes.NewReader(packMsg(t, newExpectedDNSMsg()))),
Body: io.NopCloser(bytes.NewReader(packMsg(t, newExpectedDNSMsg()))),
StatusCode: http.StatusInternalServerError,
}
return
Expand Down Expand Up @@ -147,7 +146,7 @@ func TestDNSClientResponseReadError(t *testing.T) {
err: errors.New("io error"),
}
resp = &http.Response{
Body: ioutil.NopCloser(reader),
Body: io.NopCloser(reader),
StatusCode: http.StatusOK,
}
return
Expand All @@ -161,7 +160,7 @@ func TestDNSClientResponseReadError(t *testing.T) {
func TestDNSClientMsgUnpackError(t *testing.T) {
httpClient := mockHTTPClientFunc(func(req *http.Request) (resp *http.Response, err error) {
resp = &http.Response{
Body: ioutil.NopCloser(strings.NewReader("def")),
Body: io.NopCloser(strings.NewReader("def")),
StatusCode: http.StatusOK,
}
return
Expand All @@ -173,15 +172,15 @@ func TestDNSClientMsgUnpackError(t *testing.T) {
}

func TestDNSClientLargeResponseError(t *testing.T) {
//create large buffer with the correct DNS message at the beginning
// create large buffer with the correct DNS message at the beginning
var buf bytes.Buffer
dnsMsg := packMsg(t, newExpectedDNSMsg())
buf.Write(dnsMsg)
buf.WriteString(strings.Repeat("a", maxDNSMessageSize))

httpClient := mockHTTPClientFunc(func(req *http.Request) (resp *http.Response, err error) {
resp = &http.Response{
Body: ioutil.NopCloser(&buf),
Body: io.NopCloser(&buf),
StatusCode: http.StatusOK,
}
return
Expand Down Expand Up @@ -289,6 +288,7 @@ func TestLoadBalanceDNSClientMaxFails(t *testing.T) {

lbClient := newLoadBalanceDNSClient(clients,
withLbPolicy(newSequentialPolicy()),
withLbRequestTimeout(defaultRequestTimeout),
withLbMaxFails(tt.maxFails))

result, err := lbClient.Query(context.Background(), []byte("abc"))
Expand Down