- big refactor for scheduler.

This commit is contained in:
gazebo
2018-07-25 20:43:41 +08:00
parent f3df85c8e0
commit c0770e9ab5
16 changed files with 515 additions and 321 deletions

View File

@@ -15,16 +15,13 @@ import (
"git.rosy.net.cn/jx-callback/globals/api"
)
const (
DefaultOrderCacheTimeout = 24 * time.Hour
)
var (
routinePool *routinepool.Pool
)
type SyncMapWithTimeout struct {
sync.Map
timers sync.Map
}
func init() {
@@ -34,13 +31,18 @@ func init() {
func (m *SyncMapWithTimeout) StoreWithTimeout(key, value interface{}, timeout time.Duration) {
m.Map.Store(key, value)
time.AfterFunc(timeout, func() {
m.timers.Store(key, time.AfterFunc(timeout, func() {
m.Delete(key)
})
}))
}
func (m *SyncMapWithTimeout) Store(key, value interface{}) {
m.StoreWithTimeout(key, value, DefaultOrderCacheTimeout)
func (m *SyncMapWithTimeout) Delete(key interface{}) {
m.Map.Delete(key)
if value, ok := m.timers.Load(key); ok {
timer := value.(*time.Timer)
timer.Stop()
}
m.timers.Delete(key)
}
func GetJxStoreIDFromOrder(order *model.GoodsOrder) (retVal int) {
@@ -90,14 +92,6 @@ func GetUniversalOrderIDFromOrderStatus(status *model.OrderStatus) string {
return ComposeUniversalOrderID(status.VendorOrderID, status.VendorID)
}
func GetRealTimeout(beginTime time.Time, timeout time.Duration, minTimeout time.Duration) time.Duration {
retVal := beginTime.Add(timeout).Sub(time.Now())
if retVal < minTimeout {
retVal = minTimeout + time.Duration(rand.Int31n(5*1000))*time.Millisecond // 随机分布在5秒内这样写的原因是避免启动时加载订单TIMER同一瞬间启动
}
return retVal
}
func EarthDistance(lat1, lng1, lat2, lng2 float64) float64 {
radius := 6378.137
rad := math.Pi / 180.0
@@ -168,3 +162,13 @@ func SplitSkuName(fullName string) (name string, unit string) {
}
return fullName, "份"
}
func MapValue2Scope(value, fromMin, fromMax, toMin, toMax int64) int64 {
if value < fromMin {
value = fromMin
}
if value > fromMax {
value = fromMax
}
return int64(math.Round(float64(value-fromMin)/float64(fromMax-fromMin)*float64(toMax-toMin) + float64(toMin)))
}

View File

@@ -11,3 +11,20 @@ func TestEarthDistance(t *testing.T) {
distance := EarthDistance(lat1, lng1, lat2, lng2)
fmt.Print(distance)
}
func TestMapValue2Scope(t *testing.T) {
result := MapValue2Scope(-4, -10, 0, 0, 100)
if result != 60 {
t.Fatalf("result:%d is wrong", result)
}
result = MapValue2Scope(-4, 0, 10, 0, 100)
if result != 0 {
t.Fatalf("result:%d is wrong", result)
}
result = MapValue2Scope(100, 0, 10, 0, 100)
if result != 100 {
t.Fatalf("result:%d is wrong", result)
}
}