Initial commit

This commit is contained in:
Donny
2019-04-22 20:46:32 +08:00
commit 49ab8aadd1
25441 changed files with 4055000 additions and 0 deletions

35
library/time/BUILD Normal file
View File

@@ -0,0 +1,35 @@
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
package(default_visibility = ["//visibility:public"])
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
go_library(
name = "go_default_library",
srcs = ["time.go"],
importpath = "go-common/library/time",
tags = ["automanaged"],
)
go_test(
name = "go_default_test",
srcs = ["time_test.go"],
embed = [":go_default_library"],
rundir = ".",
tags = ["automanaged"],
)

10
library/time/CHANGELOG.md Normal file
View File

@@ -0,0 +1,10 @@
### time
##### Version 1.0.1
1. 实现了从context中读取超时并比较的功能
##### Version 1.0.0
1. 实现了Mysql时间戳转换
2. 实现了配置文件读取字符串并转换成Duration

10
library/time/README.md Normal file
View File

@@ -0,0 +1,10 @@
#### time
##### 项目简介
Kratos 的时间模块主要用于mysql时间戳转换、配置文件读取并转换、Context超时时间比较
##### 编译环境
- **请只用 Golang v1.8.x 以上版本编译执行**

59
library/time/time.go Normal file
View File

@@ -0,0 +1,59 @@
package time
import (
"context"
"database/sql/driver"
"strconv"
xtime "time"
)
// Time be used to MySql timestamp converting.
type Time int64
// Scan scan time.
func (jt *Time) Scan(src interface{}) (err error) {
switch sc := src.(type) {
case xtime.Time:
*jt = Time(sc.Unix())
case string:
var i int64
i, err = strconv.ParseInt(sc, 10, 64)
*jt = Time(i)
}
return
}
// Value get time value.
func (jt Time) Value() (driver.Value, error) {
return xtime.Unix(int64(jt), 0), nil
}
// Time get time.
func (jt Time) Time() xtime.Time {
return xtime.Unix(int64(jt), 0)
}
// Duration be used toml unmarshal string time, like 1s, 500ms.
type Duration xtime.Duration
// UnmarshalText unmarshal text to duration.
func (d *Duration) UnmarshalText(text []byte) error {
tmp, err := xtime.ParseDuration(string(text))
if err == nil {
*d = Duration(tmp)
}
return err
}
// Shrink will decrease the duration by comparing with context's timeout duration
// and return new timeout\context\CancelFunc.
func (d Duration) Shrink(c context.Context) (Duration, context.Context, context.CancelFunc) {
if deadline, ok := c.Deadline(); ok {
if ctimeout := xtime.Until(deadline); ctimeout < xtime.Duration(d) {
// deliver small timeout
return Duration(ctimeout), c, func() {}
}
}
ctx, cancel := context.WithTimeout(c, xtime.Duration(d))
return d, ctx, cancel
}

60
library/time/time_test.go Normal file
View File

@@ -0,0 +1,60 @@
package time
import (
"context"
"testing"
"time"
)
func TestShrink(t *testing.T) {
var d Duration
err := d.UnmarshalText([]byte("1s"))
if err != nil {
t.Fatalf("TestShrink: d.UnmarshalText failed!err:=%v", err)
}
c := context.Background()
to, ctx, cancel := d.Shrink(c)
defer cancel()
if time.Duration(to) != time.Second {
t.Fatalf("new timeout must be equal 1 second")
}
if deadline, ok := ctx.Deadline(); !ok || time.Until(deadline) > time.Second || time.Until(deadline) < time.Millisecond*500 {
t.Fatalf("ctx deadline must be less than 1s and greater than 500ms")
}
}
func TestShrinkWithTimeout(t *testing.T) {
var d Duration
err := d.UnmarshalText([]byte("1s"))
if err != nil {
t.Fatalf("TestShrink: d.UnmarshalText failed!err:=%v", err)
}
c, cancel := context.WithTimeout(context.Background(), time.Second*2)
defer cancel()
to, ctx, cancel := d.Shrink(c)
defer cancel()
if time.Duration(to) != time.Second {
t.Fatalf("new timeout must be equal 1 second")
}
if deadline, ok := ctx.Deadline(); !ok || time.Until(deadline) > time.Second || time.Until(deadline) < time.Millisecond*500 {
t.Fatalf("ctx deadline must be less than 1s and greater than 500ms")
}
}
func TestShrinkWithDeadline(t *testing.T) {
var d Duration
err := d.UnmarshalText([]byte("1s"))
if err != nil {
t.Fatalf("TestShrink: d.UnmarshalText failed!err:=%v", err)
}
c, cancel := context.WithTimeout(context.Background(), time.Millisecond*500)
defer cancel()
to, ctx, cancel := d.Shrink(c)
defer cancel()
if time.Duration(to) >= time.Millisecond*500 {
t.Fatalf("new timeout must be less than 500 ms")
}
if deadline, ok := ctx.Deadline(); !ok || time.Until(deadline) > time.Millisecond*500 || time.Until(deadline) < time.Millisecond*200 {
t.Fatalf("ctx deadline must be less than 500ms and greater than 200ms")
}
}