Skip to content

Commit d4af2df

Browse files
refactor!: avoid to use caddy duration as pointer. (#26)
1 parent d57743b commit d4af2df

File tree

4 files changed

+13
-15
lines changed

4 files changed

+13
-15
lines changed

caddyfile.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,7 @@ reverse_proxy {
113113
return err
114114
}
115115

116-
duration := caddy.Duration(dt)
117-
h.FetchSchemaInterval = &duration
116+
h.FetchSchemaInterval = caddy.Duration(dt)
118117
case "fetch_schema_timeout":
119118
if !d.NextArg() {
120119
return d.ArgErr()
@@ -127,8 +126,7 @@ reverse_proxy {
127126
return err
128127
}
129128

130-
duration := caddy.Duration(dt)
131-
h.FetchSchemaTimeout = &duration
129+
h.FetchSchemaTimeout = caddy.Duration(dt)
132130
case "fetch_schema_header":
133131
if !d.NextArg() {
134132
return d.ArgErr()

handler.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ type Handler struct {
3535
Upstream string `json:"upstream,omitempty"`
3636

3737
// Fetch schema interval, disabled by default.
38-
FetchSchemaInterval *caddy.Duration `json:"fetch_schema_interval,omitempty"`
38+
FetchSchemaInterval caddy.Duration `json:"fetch_schema_interval,omitempty"`
3939

4040
// Fetch schema request timeout, "30s" by default
41-
FetchSchemaTimeout *caddy.Duration `json:"fetch_schema_timeout,omitempty"`
41+
FetchSchemaTimeout caddy.Duration `json:"fetch_schema_timeout,omitempty"`
4242

4343
// Fetch schema headers
4444
FetchSchemaHeader http.Header `json:"fetch_schema_headers,omitempty"`
@@ -120,16 +120,15 @@ func (h *Handler) Provision(ctx caddy.Context) (err error) {
120120
h.Caching.withMetrics(h)
121121
}
122122

123-
if h.FetchSchemaTimeout == nil {
123+
if h.FetchSchemaTimeout == 0 {
124124
timeout, _ := caddy.ParseDuration("30s")
125-
fetchTimeout := caddy.Duration(timeout)
126-
h.FetchSchemaTimeout = &fetchTimeout
125+
h.FetchSchemaTimeout = caddy.Duration(timeout)
127126
}
128127

129128
sf := &schemaFetcher{
130129
upstream: h.Upstream,
131130
header: h.FetchSchemaHeader,
132-
timeout: *h.FetchSchemaTimeout,
131+
timeout: h.FetchSchemaTimeout,
133132
interval: h.FetchSchemaInterval,
134133
logger: h.logger,
135134
context: h.ctxBackground,

schema_fetcher.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type schemaFetcher struct {
2929
// Upstream url
3030
upstream string
3131
header http.Header
32-
interval *caddy.Duration
32+
interval caddy.Duration
3333
timeout caddy.Duration
3434

3535
caching *Caching
@@ -53,7 +53,9 @@ func (s *schemaFetcher) Provision(ctx caddy.Context) (err error) {
5353
return err
5454
}
5555

56-
if s.interval == nil {
56+
if s.interval == 0 {
57+
s.logger.Info("fetch schema interval disabled")
58+
5759
return nil
5860
}
5961

@@ -63,7 +65,7 @@ func (s *schemaFetcher) Provision(ctx caddy.Context) (err error) {
6365
}
6466

6567
func (s *schemaFetcher) startInterval() {
66-
interval := time.NewTicker(time.Duration(*s.interval))
68+
interval := time.NewTicker(time.Duration(s.interval))
6769

6870
defer interval.Stop()
6971

schema_fetcher_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ type SchemaFetcherTestSuite struct {
2121
}
2222

2323
func (s *SchemaFetcherTestSuite) TestInterval() {
24-
i := caddy.Duration(time.Millisecond)
2524
ctx, cancel := context.WithCancel(context.Background())
2625
sh := schemaChangedHandler(func(oldDocument, newDocument *ast.Document, oldSchema, newSchema *graphql.Schema) {
2726
s.Require().NotNil(newDocument)
@@ -30,7 +29,7 @@ func (s *SchemaFetcherTestSuite) TestInterval() {
3029
})
3130
f := &schemaFetcher{
3231
upstream: "http://localhost:9091",
33-
interval: &i,
32+
interval: caddy.Duration(time.Millisecond),
3433
onSchemaChanged: sh,
3534
context: ctx,
3635
header: make(http.Header),

0 commit comments

Comments
 (0)