github.com/KinWaiYuen/client-go/v2@v2.5.4/oracle/oracles/local.go (about) 1 // Copyright 2021 TiKV Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 // NOTE: The code in this file is based on code from the 16 // TiDB project, licensed under the Apache License v 2.0 17 // 18 // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/oracle/oracles/local.go 19 // 20 21 // Copyright 2016 PingCAP, Inc. 22 // 23 // Licensed under the Apache License, Version 2.0 (the "License"); 24 // you may not use this file except in compliance with the License. 25 // You may obtain a copy of the License at 26 // 27 // http://www.apache.org/licenses/LICENSE-2.0 28 // 29 // Unless required by applicable law or agreed to in writing, software 30 // distributed under the License is distributed on an "AS IS" BASIS, 31 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 32 // See the License for the specific language governing permissions and 33 // limitations under the License. 34 35 package oracles 36 37 import ( 38 "context" 39 "sync" 40 "time" 41 42 "github.com/KinWaiYuen/client-go/v2/oracle" 43 ) 44 45 var _ oracle.Oracle = &localOracle{} 46 47 type localOracle struct { 48 sync.Mutex 49 lastTimeStampTS uint64 50 n uint64 51 hook *struct { 52 currentTime time.Time 53 } 54 } 55 56 // NewLocalOracle creates an Oracle that uses local time as data source. 57 func NewLocalOracle() oracle.Oracle { 58 return &localOracle{} 59 } 60 61 func (l *localOracle) IsExpired(lockTS, TTL uint64, _ *oracle.Option) bool { 62 now := time.Now() 63 if l.hook != nil { 64 now = l.hook.currentTime 65 } 66 expire := oracle.GetTimeFromTS(lockTS).Add(time.Duration(TTL) * time.Millisecond) 67 return !now.Before(expire) 68 } 69 70 func (l *localOracle) GetTimestamp(ctx context.Context, _ *oracle.Option) (uint64, error) { 71 l.Lock() 72 defer l.Unlock() 73 now := time.Now() 74 if l.hook != nil { 75 now = l.hook.currentTime 76 } 77 ts := oracle.GoTimeToTS(now) 78 if l.lastTimeStampTS == ts { 79 l.n++ 80 return ts + l.n, nil 81 } 82 l.lastTimeStampTS = ts 83 l.n = 0 84 return ts, nil 85 } 86 87 func (l *localOracle) GetTimestampAsync(ctx context.Context, _ *oracle.Option) oracle.Future { 88 return &future{ 89 ctx: ctx, 90 l: l, 91 } 92 } 93 94 func (l *localOracle) GetLowResolutionTimestamp(ctx context.Context, opt *oracle.Option) (uint64, error) { 95 return l.GetTimestamp(ctx, opt) 96 } 97 98 func (l *localOracle) GetLowResolutionTimestampAsync(ctx context.Context, opt *oracle.Option) oracle.Future { 99 return l.GetTimestampAsync(ctx, opt) 100 } 101 102 // GetStaleTimestamp return physical 103 func (l *localOracle) GetStaleTimestamp(ctx context.Context, txnScope string, prevSecond uint64) (ts uint64, err error) { 104 return oracle.GoTimeToTS(time.Now().Add(-time.Second * time.Duration(prevSecond))), nil 105 } 106 107 type future struct { 108 ctx context.Context 109 l *localOracle 110 } 111 112 func (f *future) Wait() (uint64, error) { 113 return f.l.GetTimestamp(f.ctx, &oracle.Option{}) 114 } 115 116 // UntilExpired implement oracle.Oracle interface. 117 func (l *localOracle) UntilExpired(lockTimeStamp, TTL uint64, opt *oracle.Option) int64 { 118 now := time.Now() 119 if l.hook != nil { 120 now = l.hook.currentTime 121 } 122 return oracle.ExtractPhysical(lockTimeStamp) + int64(TTL) - oracle.GetPhysical(now) 123 } 124 125 func (l *localOracle) Close() { 126 }