Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: Create duration wrapper #1963

Merged
merged 3 commits into from
Oct 11, 2018
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
40 changes: 40 additions & 0 deletions go/lib/util/duration_wrap.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2018 Anapaya Systems
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package util

import (
"time"

"github.com/BurntSushi/toml"
)

var _ (toml.TextUnmarshaler) = (*DurWrap)(nil)
var _ (toml.TextMarshaler) = (*DurWrap)(nil)

// DurWrap is a wrapper to enable marshalling and unmarshalling of durations
// with the custom format.
type DurWrap struct {
time.Duration
}

func (d *DurWrap) UnmarshalText(text []byte) error {
var err error
d.Duration, err = ParseDuration(string(text))
return err
}

func (d *DurWrap) MarshalText() (text []byte, err error) {
return []byte(FmtDuration(d.Duration)), nil
}
2 changes: 1 addition & 1 deletion go/path_srv/internal/handlers/segreq.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (h *segReqHandler) fetchAndSaveSegs(ctx context.Context, msger infra.Messen
h.verifyAndStore(ctx, cPSAddr, recs, revInfos)
// TODO(lukedirtwalker): If we didn't receive anything we should retry earlier.
if _, err := h.pathDB.InsertNextQuery(ctx, dst,
queryTime.Add(h.config.QueryInterval())); err != nil {
queryTime.Add(h.config.QueryInterval.Duration)); err != nil {
h.logger.Warn("Failed to insert last queried", "err", err)
}
}
Expand Down
26 changes: 4 additions & 22 deletions go/path_srv/internal/psconfig/psconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ package psconfig
import (
"time"

"github.com/BurntSushi/toml"

"github.com/scionproto/scion/go/lib/util"
)

Expand All @@ -32,29 +30,13 @@ type Config struct {
// using SegSync messages.
SegSync bool
PathDB string
// queryInterval specifies after how much time segments
// QueryInterval specifies after how much time segments
// for a destination should be refetched.
queryInterval duration `toml:"QueryInterval"`
QueryInterval util.DurWrap
}

func (c *Config) InitDefaults() {
if c.queryInterval.Duration == 0 {
c.queryInterval.Duration = DefaultQueryInterval
if c.QueryInterval.Duration == 0 {
c.QueryInterval.Duration = DefaultQueryInterval
}
}

func (c *Config) QueryInterval() time.Duration {
return c.queryInterval.Duration
}

var _ (toml.TextUnmarshaler) = (*duration)(nil)

type duration struct {
time.Duration
}

func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = util.ParseDuration(string(text))
return err
}
4 changes: 2 additions & 2 deletions go/sciond/internal/fetcher/fetcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func (f *fetcherHandler) GetPaths(ctx context.Context, req *sciond.PathReq,
case <-ctx.Done():
}
if ctx.Err() == nil {
refetchInt := f.config.QueryInterval()
_, err = f.pathDB.InsertNextQuery(ctx, req.Dst.IA(), time.Now().Add(refetchInt))
_, err = f.pathDB.InsertNextQuery(ctx, req.Dst.IA(),
time.Now().Add(f.config.QueryInterval.Duration))
if err != nil {
f.logger.Warn("Failed to update nextQuery", "err", err)
}
Expand Down
26 changes: 4 additions & 22 deletions go/sciond/internal/sdconfig/sdconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import (
"path/filepath"
"time"

"github.com/BurntSushi/toml"

"github.com/scionproto/scion/go/lib/common"
"github.com/scionproto/scion/go/lib/sciond"
"github.com/scionproto/scion/go/lib/snet"
Expand Down Expand Up @@ -49,9 +47,9 @@ type Config struct {
Bind *snet.Addr
// PathDB contains the file location of the path segment database.
PathDB string
// queryInterval specifies after how much time segments
// QueryInterval specifies after how much time segments
// for a destination should be refetched.
queryInterval duration `toml:"QueryInterval"`
QueryInterval util.DurWrap
}

func (c *Config) InitDefaults() {
Expand All @@ -61,15 +59,11 @@ func (c *Config) InitDefaults() {
if c.Unix == "" {
c.Unix = "/run/shm/sciond/default-unix.sock"
}
if c.queryInterval.Duration == 0 {
c.queryInterval.Duration = DefaultQueryInterval
if c.QueryInterval.Duration == 0 {
c.QueryInterval.Duration = DefaultQueryInterval
}
}

func (c *Config) QueryInterval() time.Duration {
return c.queryInterval.Duration
}

func (c *Config) CreateSocketDirs() error {
reliableDir := filepath.Dir(c.Reliable)
if _, err := os.Stat(reliableDir); os.IsNotExist(err) {
Expand All @@ -86,15 +80,3 @@ func (c *Config) CreateSocketDirs() error {
}
return nil
}

var _ (toml.TextUnmarshaler) = (*duration)(nil)

type duration struct {
time.Duration
}

func (d *duration) UnmarshalText(text []byte) error {
var err error
d.Duration, err = util.ParseDuration(string(text))
return err
}