time

package
v0.0.131 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 3, 2024 License: MIT Imports: 10 Imported by: 1

Documentation

Overview

*Copyright (c) 2022, kaydxh * *Permission is hereby granted, free of charge, to any person obtaining a copy *of this software and associated documentation files (the "Software"), to deal *in the Software without restriction, including without limitation the rights *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *copies of the Software, and to permit persons to whom the Software is *furnished to do so, subject to the following conditions: * *The above copyright notice and this permission notice shall be included in all *copies or substantial portions of the Software. * *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *SOFTWARE.

*Copyright (c) 2022, kaydxh * *Permission is hereby granted, free of charge, to any person obtaining a copy *of this software and associated documentation files (the "Software"), to deal *in the Software without restriction, including without limitation the rights *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *copies of the Software, and to permit persons to whom the Software is *furnished to do so, subject to the following conditions: * *The above copyright notice and this permission notice shall be included in all *copies or substantial portions of the Software. * *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *SOFTWARE.

*Copyright (c) 2022, kaydxh * *Permission is hereby granted, free of charge, to any person obtaining a copy *of this software and associated documentation files (the "Software"), to deal *in the Software without restriction, including without limitation the rights *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *copies of the Software, and to permit persons to whom the Software is *furnished to do so, subject to the following conditions: * *The above copyright notice and this permission notice shall be included in all *copies or substantial portions of the Software. * *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *SOFTWARE.

*Copyright (c) 2022, kaydxh * *Permission is hereby granted, free of charge, to any person obtaining a copy *of this software and associated documentation files (the "Software"), to deal *in the Software without restriction, including without limitation the rights *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *copies of the Software, and to permit persons to whom the Software is *furnished to do so, subject to the following conditions: * *The above copyright notice and this permission notice shall be included in all *copies or substantial portions of the Software. * *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *SOFTWARE.

*Copyright (c) 2022, kaydxh * *Permission is hereby granted, free of charge, to any person obtaining a copy *of this software and associated documentation files (the "Software"), to deal *in the Software without restriction, including without limitation the rights *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *copies of the Software, and to permit persons to whom the Software is *furnished to do so, subject to the following conditions: * *The above copyright notice and this permission notice shall be included in all *copies or substantial portions of the Software. * *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *SOFTWARE.

*Copyright (c) 2022, kaydxh * *Permission is hereby granted, free of charge, to any person obtaining a copy *of this software and associated documentation files (the "Software"), to deal *in the Software without restriction, including without limitation the rights *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *copies of the Software, and to permit persons to whom the Software is *furnished to do so, subject to the following conditions: * *The above copyright notice and this permission notice shall be included in all *copies or substantial portions of the Software. * *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *SOFTWARE.

*Copyright (c) 2022, kaydxh * *Permission is hereby granted, free of charge, to any person obtaining a copy *of this software and associated documentation files (the "Software"), to deal *in the Software without restriction, including without limitation the rights *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *copies of the Software, and to permit persons to whom the Software is *furnished to do so, subject to the following conditions: * *The above copyright notice and this permission notice shall be included in all *copies or substantial portions of the Software. * *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *SOFTWARE.

*Copyright (c) 2022, kaydxh * *Permission is hereby granted, free of charge, to any person obtaining a copy *of this software and associated documentation files (the "Software"), to deal *in the Software without restriction, including without limitation the rights *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *copies of the Software, and to permit persons to whom the Software is *furnished to do so, subject to the following conditions: * *The above copyright notice and this permission notice shall be included in all *copies or substantial portions of the Software. * *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR *IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, *OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE *SOFTWARE.

Index

Constants

View Source
const (
	DefaultInitialInterval     = 500 * time.Millisecond
	DefaultRandomizationFactor = 0.5
	// The default multiplier value used for increment current interval
	DefaultMultiplier      = 1.5
	DefaultMaxInterval     = 60 * time.Second
	DefaultMinInterval     = DefaultInitialInterval
	DefaultMaxElapsedTime  = 15 * time.Minute
	DefaultMaxElapsedCount = -1
)

Default values for ExponentialBackOff.

View Source
const (
	DayFormat             = "20060102"
	TimeMillFormat        = "20060102150405.000"
	ShortTimeFormat       = "20060102150405"
	ShortDashTimeFormat   = "2006-01-02-15:04:05"
	DefaultTimeFormat     = "2006-01-02 15:04:05"
	DefaultTimeMillFormat = "2006-01-02 15:04:05.000"
)

Variables

View Source
var ErrTimeout = errors.New("timeout error")

Functions

func BackOffUntilWithContext added in v0.0.55

func BackOffUntilWithContext(
	ctx context.Context,
	f func(ctx context.Context) error,
	backoff Backoff,
	sliding bool,
	loop bool,
) (err error)

loop true -> BackOffUntilWithContext return until time expired loop false -> BackOffUntilWithContext return if f return nil, or time expired

func BeginningOfDay added in v0.0.7

func BeginningOfDay(days int) time.Time

func BeginningOfDayString added in v0.0.7

func BeginningOfDayString(days int, layout string) string

func CallWithTimeout added in v0.0.111

func CallWithTimeout(ctx context.Context, timeout time.Duration, f func(ctx context.Context) error) error

func EndOfDay added in v0.0.7

func EndOfDay(days int) time.Time

func EndOfDayString added in v0.0.7

func EndOfDayString(days int, layout string) string

func JitterUntilWithContext added in v0.0.55

func JitterUntilWithContext(
	ctx context.Context,
	f func(ctx context.Context) error,
	period time.Duration,
)

func Now added in v0.0.12

func Now() time.Time

func NowString added in v0.0.12

func NowString(layout string) string

func RetryWithContext added in v0.0.101

func RetryWithContext(
	ctx context.Context,
	f func(ctx context.Context) error,
	period time.Duration,
	retryTimes int,
) error

RetryWithContext retryTime is not include the first call

func TruncateToUTC added in v0.0.27

func TruncateToUTC(t time.Time, d time.Duration) time.Time

Truncate only happens in UTC semantics, apparently. observed values for truncating given time with 86400 secs:

before truncation: 2018/06/01 03:54:54 2018-06-01T03:18:00+09:00 after truncation: 2018/06/01 03:54:54 2018-05-31T09:00:00+09:00

This is really annoying when we want to truncate in local time so we hack: we take the apparent local time in the local zone, and pretend that it's in UTC. do our math, and put it back to the local zone

func TruncateToUTCString added in v0.0.27

func TruncateToUTCString(t time.Time, d time.Duration, layout string) string

func UntilWithContxt added in v0.0.55

func UntilWithContxt(
	ctx context.Context,
	f func(ctx context.Context) error, period time.Duration)

Until loops until context timout, running f every period. Until is syntactic sugar on top of JitterUntil with zero jitter factor and with sliding = true (which means the timer for period starts after the f completes).

Types

type Backoff added in v0.0.55

type Backoff interface {
	NextBackOff() (time.Duration, bool)
	Reset()
}

type EmptyExponentialBackOffOption added in v0.0.6

type EmptyExponentialBackOffOption struct{}

EmptyExponentialBackOffUrlOption does not alter the ExponentialBackOffuration. It can be embedded in another structure to build custom options.

This API is EXPERIMENTAL.

type ExponentialBackOff added in v0.0.6

type ExponentialBackOff struct {
	// contains filtered or unexported fields
}

func NewExponentialBackOff added in v0.0.6

func NewExponentialBackOff(opts ...ExponentialBackOffOption) *ExponentialBackOff

func (*ExponentialBackOff) ApplyOptions added in v0.0.6

func (o *ExponentialBackOff) ApplyOptions(options ...ExponentialBackOffOption) *ExponentialBackOff

func (*ExponentialBackOff) GetCurrentInterval added in v0.0.6

func (b *ExponentialBackOff) GetCurrentInterval() time.Duration

func (*ExponentialBackOff) GetElapsedTime added in v0.0.6

func (b *ExponentialBackOff) GetElapsedTime() time.Duration

func (*ExponentialBackOff) MaxElapsedTime added in v0.0.101

func (b *ExponentialBackOff) MaxElapsedTime() time.Duration

func (*ExponentialBackOff) NextBackOff added in v0.0.6

func (b *ExponentialBackOff) NextBackOff() (time.Duration, bool)

NextBackOff is get next time duration

func (*ExponentialBackOff) PreBackOff added in v0.0.78

func (b *ExponentialBackOff) PreBackOff() (time.Duration, bool)

PreBackOff is get previos time duration false : have gone over the maximu elapsed time true : return remaining time

func (*ExponentialBackOff) Reset added in v0.0.6

func (b *ExponentialBackOff) Reset()

func (*ExponentialBackOff) ResetWithInterval added in v0.0.75

func (b *ExponentialBackOff) ResetWithInterval(initialInterval time.Duration)

type ExponentialBackOffMap added in v0.0.75

type ExponentialBackOffMap sync.Map

func (*ExponentialBackOffMap) Delete added in v0.0.75

func (m *ExponentialBackOffMap) Delete(key string)

Delete deletes the value for a key.

func (*ExponentialBackOffMap) Load added in v0.0.75

Load returns the value stored in the map for a key, or nil if no value is present. The ok result indicates whether value was found in the map.

func (*ExponentialBackOffMap) LoadAndDelete added in v0.0.75

func (m *ExponentialBackOffMap) LoadAndDelete(key string) (value ExponentialBackOff, loaded bool)

LoadAndDelete deletes the value for a key, returning the previous value if any. The loaded result reports whether the key was present.

func (*ExponentialBackOffMap) LoadOrStore added in v0.0.75

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored.

func (*ExponentialBackOffMap) Range added in v0.0.75

func (m *ExponentialBackOffMap) Range(f func(key string, value ExponentialBackOff) bool)

Range calls f sequentially for each key and value present in the map. If f returns false, range stops the iteration.

Range does not necessarily correspond to any consistent snapshot of the Map's contents: no key will be visited more than once, but if the value for any key is stored or deleted concurrently, Range may reflect any mapping for that key from any point during the Range call.

Range may be O(N) with the number of elements in the map even if f returns false after a constant number of calls.

func (*ExponentialBackOffMap) Store added in v0.0.75

func (m *ExponentialBackOffMap) Store(key string, value ExponentialBackOff)

Store sets the value for a key.

type ExponentialBackOffOption added in v0.0.6

type ExponentialBackOffOption interface {
	// contains filtered or unexported methods
}

A ExponentialBackOffOption sets options.

func WithExponentialBackOffOptionInitialInterval added in v0.0.6

func WithExponentialBackOffOptionInitialInterval(initialInterval time.Duration) ExponentialBackOffOption

func WithExponentialBackOffOptionMaxElapsedCount added in v0.0.101

func WithExponentialBackOffOptionMaxElapsedCount(maxElapsedCount int) ExponentialBackOffOption

func WithExponentialBackOffOptionMaxElapsedTime added in v0.0.6

func WithExponentialBackOffOptionMaxElapsedTime(maxElapsedTime time.Duration) ExponentialBackOffOption

func WithExponentialBackOffOptionMaxInterval added in v0.0.6

func WithExponentialBackOffOptionMaxInterval(maxInterval time.Duration) ExponentialBackOffOption

func WithExponentialBackOffOptionMinInterval added in v0.0.75

func WithExponentialBackOffOptionMinInterval(minInterval time.Duration) ExponentialBackOffOption

func WithExponentialBackOffOptionMultiplier added in v0.0.6

func WithExponentialBackOffOptionMultiplier(multiplier float64) ExponentialBackOffOption

func WithExponentialBackOffOptionRandomizationFactor added in v0.0.6

func WithExponentialBackOffOptionRandomizationFactor(randomizationFactor float64) ExponentialBackOffOption

type ExponentialBackOffOptionFunc added in v0.0.6

type ExponentialBackOffOptionFunc func(*ExponentialBackOff)

ExponentialBackOffOptionFunc wraps a function that modifies ExponentialBackOff into an implementation of the ExponentialBackOffOption interface.

type TimeCounter

type TimeCounter struct {
	// contains filtered or unexported fields
}

func New

func New(effect bool) *TimeCounter

func (*TimeCounter) Elapse added in v0.0.36

func (t *TimeCounter) Elapse() time.Duration

func (*TimeCounter) Reset

func (t *TimeCounter) Reset()

func (*TimeCounter) String

func (t *TimeCounter) String() string

func (*TimeCounter) Summary

func (t *TimeCounter) Summary(f func(idx int, msg string, cost time.Duration, at time.Time))

func (*TimeCounter) Tick

func (t *TimeCounter) Tick(msg string)

Directories

Path Synopsis
*Copyright (c) 2022, kaydxh * *Permission is hereby granted, free of charge, to any person obtaining a copy *of this software and associated documentation files (the "Software"), to deal *in the Software without restriction, including without limitation the rights *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *copies of the Software, and to permit persons to whom the Software is *furnished to do so, subject to the following conditions: * *The above copyright notice and this permission notice shall be included in all *copies or substantial portions of the Software.
*Copyright (c) 2022, kaydxh * *Permission is hereby granted, free of charge, to any person obtaining a copy *of this software and associated documentation files (the "Software"), to deal *in the Software without restriction, including without limitation the rights *to use, copy, modify, merge, publish, distribute, sublicense, and/or sell *copies of the Software, and to permit persons to whom the Software is *furnished to do so, subject to the following conditions: * *The above copyright notice and this permission notice shall be included in all *copies or substantial portions of the Software.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL