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

utils: Remove unused functions, stdlib replacements #744

Closed
wants to merge 1 commit into from
Closed
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
3 changes: 1 addition & 2 deletions pkg/config/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/icinga/icingadb/pkg/icingaredis"
"github.com/icinga/icingadb/pkg/logging"
"github.com/icinga/icingadb/pkg/retry"
"github.com/icinga/icingadb/pkg/utils"
"github.com/pkg/errors"
"github.com/redis/go-redis/v9"
"go.uber.org/zap"
Expand Down Expand Up @@ -64,7 +63,7 @@ func (r *Redis) NewClient(logger *logging.Logger) (*icingaredis.Client, error) {
c := redis.NewClient(options)

opts := c.Options()
opts.PoolSize = utils.MaxInt(32, opts.PoolSize)
opts.PoolSize = max(32, opts.PoolSize)
opts.MaxRetries = opts.PoolSize + 1 // https://github.com/go-redis/redis/issues/1737
c = redis.NewClient(opts)

Expand Down
11 changes: 4 additions & 7 deletions pkg/types/unix_milli.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
"encoding"
"encoding/json"
"github.com/icinga/icingadb/internal"
"github.com/icinga/icingadb/pkg/utils"
"github.com/pkg/errors"
"strconv"
"time"
)

// UnixMilli is a nullable millisecond UNIX timestamp in databases and JSON.
// UnixMilli is a nullable millisecond Unix timestamp in databases and JSON.
type UnixMilli time.Time

// Time returns the time.Time conversion of UnixMilli.
Expand All @@ -37,7 +36,7 @@ func (t *UnixMilli) UnmarshalText(text []byte) error {
return internal.CantParseFloat64(err, string(text))
}

*t = UnixMilli(utils.FromUnixMilli(int64(parsed)))
*t = UnixMilli(time.UnixMilli(int64(parsed)))
return nil
}

Expand All @@ -52,8 +51,7 @@ func (t *UnixMilli) UnmarshalJSON(data []byte) error {
if err != nil {
return internal.CantParseFloat64(err, string(data))
}
tt := utils.FromUnixMilli(int64(ms))
*t = UnixMilli(tt)
*t = UnixMilli(time.UnixMilli(int64(ms)))

return nil
}
Expand All @@ -69,8 +67,7 @@ func (t *UnixMilli) Scan(src interface{}) error {
if !ok {
return errors.Errorf("bad int64 type assertion from %#v", src)
}
tt := utils.FromUnixMilli(v)
*t = UnixMilli(tt)
*t = UnixMilli(time.UnixMilli(v))

return nil
}
Expand Down
58 changes: 0 additions & 58 deletions pkg/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,28 +4,16 @@ import (
"context"
"crypto/sha1"
"fmt"
"github.com/go-sql-driver/mysql"
"github.com/icinga/icingadb/pkg/contracts"
"github.com/lib/pq"
"github.com/pkg/errors"
"golang.org/x/exp/utf8string"
"math"
"net"
"os"
"path/filepath"
"strings"
"time"
"unicode"
)

// FromUnixMilli creates and returns a time.Time value
// from the given milliseconds since the Unix epoch ms.
func FromUnixMilli(ms int64) time.Time {
sec, dec := math.Modf(float64(ms) / 1e3)

return time.Unix(int64(sec), int64(dec*(1e9)))
}

// Name returns the declared name of type t.
// Name is used in combination with Key
// to automatically guess an entity's
Expand All @@ -51,20 +39,6 @@ func Key(name string, sep byte) string {
return ConvertCamelCase(name, unicode.LowerCase, sep)
}

// Timed calls the given callback with the time that has elapsed since the start.
//
// Timed should be installed by defer:
//
// func TimedExample(logger *zap.SugaredLogger) {
// defer utils.Timed(time.Now(), func(elapsed time.Duration) {
// logger.Debugf("Executed job in %s", elapsed)
// })
// job()
// }
func Timed(start time.Time, callback func(elapsed time.Duration)) {
callback(time.Since(start))
}

// BatchSliceOfStrings groups the given keys into chunks of size count and streams them into a returned channel.
func BatchSliceOfStrings(ctx context.Context, keys []string, count int) <-chan []string {
batches := make(chan []string)
Expand Down Expand Up @@ -115,29 +89,6 @@ func Fatal(err error) {
panic(err)
}

// IsDeadlock returns whether the given error signals serialization failure.
func IsDeadlock(err error) bool {
var e *mysql.MySQLError
if errors.As(err, &e) {
switch e.Number {
case 1205, 1213:
return true
default:
return false
}
}

var pe *pq.Error
if errors.As(err, &pe) {
switch pe.Code {
case "40001", "40P01":
return true
}
}

return false
}

var ellipsis = utf8string.NewString("...")

// Ellipsize shortens s to <=limit runes and indicates shortening by "...".
Expand Down Expand Up @@ -199,15 +150,6 @@ func AppName() string {
return filepath.Base(exe)
}

// MaxInt returns the larger of the given integers.
func MaxInt(x, y int) int {
if x > y {
return x
}

return y
}

// JoinHostPort is like its equivalent in net., but handles UNIX sockets as well.
func JoinHostPort(host string, port int) string {
if strings.HasPrefix(host, "/") {
Expand Down
Loading