Skip to content

Commit

Permalink
Migrate from zap to slog (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexey-mercari authored Oct 17, 2023
1 parent 2701c2a commit 0453673
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 102 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ jobs:
test:
working_directory: /go/src/go.mercari.io/go-dnscache
docker:
- image: golang:1.17-bullseye
- image: golang:1.21-bullseye
steps:
- checkout
- run:
Expand Down
22 changes: 10 additions & 12 deletions dnscache.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@ package dnscache

import (
"context"
"errors"
"log/slog"
"net"
"sync"
"time"

"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -50,14 +48,14 @@ type Resolver struct {

// defaultLookupTimeout is used when refreshing DNS cache
defaultLookupTimeout time.Duration
logger *zap.Logger
logger *slog.Logger

closer func()
}

// New initializes DNS cache resolver and starts auto refreshing in a new goroutine.
// To stop refreshing, call `Stop()` function.
func New(freq time.Duration, lookupTimeout time.Duration, logger *zap.Logger) (*Resolver, error) {
func New(freq time.Duration, lookupTimeout time.Duration, options ...Option) (*Resolver, error) {
if freq <= 0 {
freq = defaultFreq
}
Expand All @@ -66,10 +64,6 @@ func New(freq time.Duration, lookupTimeout time.Duration, logger *zap.Logger) (*
lookupTimeout = defaultLookupTimeout
}

if logger == nil {
return nil, errors.New("missing logger")
}

ticker := time.NewTicker(freq)
ch := make(chan struct{})
closer := func() {
Expand All @@ -86,10 +80,14 @@ func New(freq time.Duration, lookupTimeout time.Duration, logger *zap.Logger) (*
lookupTimeout: lookupTimeout,
cache: make(map[string][]net.IP, cacheSize),
defaultLookupTimeout: lookupTimeout,
logger: logger,
logger: slog.Default(),
closer: closer,
}

for _, o := range options {
o.apply(r)
}

go func() {
for {
select {
Expand Down Expand Up @@ -144,8 +142,8 @@ func (r *Resolver) Refresh() {
ctx, cancelF := context.WithTimeout(context.Background(), r.defaultLookupTimeout)
if _, err := r.LookupIP(ctx, addr); err != nil {
r.logger.Error("failed to refresh DNS cache",
zap.Error(err),
zap.String("addr", addr),
"error", err,
"addr", addr,
)
}
cancelF()
Expand Down
30 changes: 11 additions & 19 deletions dnscache_test.go
Original file line number Diff line number Diff line change
@@ -1,50 +1,43 @@
package dnscache

import (
"bytes"
"context"
"fmt"
"log/slog"
"net"
"reflect"
"sync"
"sync/atomic"
"testing"
"time"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest/observer"
)

var (
testLogger = zap.NewNop()
testFreq = 1 * time.Second
testDefaultLookupTimeout = 1 * time.Second
)

func testResolver(t *testing.T) *Resolver {
t.Helper()
r, err := New(testFreq, testDefaultLookupTimeout, testLogger)
r, err := New(testFreq, testDefaultLookupTimeout)
if err != nil {
t.Fatalf("err: %s", err)
}
return r
}

func TestNew(t *testing.T) {
if _, err := New(testFreq, testDefaultLookupTimeout, nil); err == nil {
t.Fatalf("expect to be failed")
}

{
resolver, err := New(testFreq, testDefaultLookupTimeout, testLogger)
resolver, err := New(testFreq, testDefaultLookupTimeout)
if err != nil {
t.Fatalf("expect not to be failed")
}
resolver.Stop()
}

{
resolver, err := New(0, 0, testLogger)
resolver, err := New(0, 0)
if err != nil {
t.Fatalf("expect not to be failed")
}
Expand All @@ -60,7 +53,7 @@ func TestLookup(t *testing.T) {
"api.mercari.jp",
},
{
"endpoint-api-origin.mercari.jp",
"ya.ru",
},
{
"google.com",
Expand Down Expand Up @@ -199,7 +192,7 @@ func TestRefreshed(t *testing.T) {
atomic.AddInt32(&counter, 1)
}

resolver, err := New(1*time.Millisecond, testDefaultLookupTimeout, testLogger)
resolver, err := New(1*time.Millisecond, testDefaultLookupTimeout)
defer resolver.Stop()
if err != nil {
t.Fatalf("err: %v", err)
Expand Down Expand Up @@ -298,18 +291,17 @@ func TestErrorLog(t *testing.T) {
return nil, fmt.Errorf("err")
}

core, observed := observer.New(zapcore.DebugLevel)
logger := zap.New(core)
buf := new(bytes.Buffer)
logger := slog.New(slog.NewTextHandler(buf, nil))

resolver, err := New(0, 0, logger)
resolver, err := New(0, 0, WithLogger(logger))
if err != nil {
t.Fatalf("err: %s", err)
}
defer resolver.Stop()

<-done
entries := observed.AllUntimed()
if got, want := len(entries), 1; got >= want {
if got, want := buf.Len(), 1; got >= want {
t.Fatalf("expect logger called more than once")
}
}
9 changes: 1 addition & 8 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
module go.mercari.io/go-dnscache

go 1.17

require go.uber.org/zap v1.20.0

require (
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.7.0 // indirect
)
go 1.21
59 changes: 0 additions & 59 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,59 +0,0 @@
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.9.0 h1:ECmE8Bn/WFTYwEW/bpKD3M8VtR/zQVbavAoalC1PYyE=
go.uber.org/atomic v1.9.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/multierr v1.7.0 h1:zaiO/rmgFjbmCXdSYJWQcdvOCsthmdaHfr3Gm2Kx4Ec=
go.uber.org/multierr v1.7.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/zap v1.20.0 h1:N4oPlghZwYG55MlU6LXk/Zp00FVNE9X9wrYO8CEs4lc=
go.uber.org/zap v1.20.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
golang.org/x/mod v0.4.2/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4/go.mod h1:p54w0d4576C0XHj96bSt6lcn1PtDYWL6XObtHCRCNQM=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 3 additions & 3 deletions net_example_test.go
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package dnscache

import (
"log/slog"
"math/rand"
"net/http"
"time"

"go.uber.org/zap"
)

func ExampleDialFunc() {
resolver, _ := New(3*time.Second, 5*time.Second, zap.NewNop())
logger := slog.Default().WithGroup("dnscache")
resolver, _ := New(3*time.Second, 5*time.Second, WithLogger(logger))

// You can create a HTTP client which selects an IP from dnscache
// randomly and dials it.
Expand Down
13 changes: 13 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package dnscache

import "log/slog"

type Option struct {
apply func(r *Resolver)
}

func WithLogger(logger *slog.Logger) Option {
return Option{apply: func(r *Resolver) {
r.logger = logger
}}
}

0 comments on commit 0453673

Please sign in to comment.