github.com/polarismesh/polaris@v1.17.8/store/mysql/tool.go (about) 1 /** 2 * Tencent is pleased to support the open source community by making Polaris available. 3 * 4 * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. 5 * 6 * Licensed under the BSD 3-Clause License (the "License"); 7 * you may not use this file except in compliance with the License. 8 * You may obtain a copy of the License at 9 * 10 * https://opensource.org/licenses/BSD-3-Clause 11 * 12 * Unless required by applicable law or agreed to in writing, software distributed 13 * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR 14 * CONDITIONS OF ANY KIND, either express or implied. See the License for the 15 * specific language governing permissions and limitations under the License. 16 */ 17 18 package sqldb 19 20 import ( 21 "time" 22 ) 23 24 // toolStore 实现了ToolStoreStore 25 type toolStore struct { 26 db *BaseDB 27 } 28 29 const ( 30 nowSql = `select UNIX_TIMESTAMP(SYSDATE())` 31 maxQueryInterval = time.Second 32 ) 33 34 // GetUnixSecond 获取当前时间,单位秒 35 func (t *toolStore) GetUnixSecond(maxWait time.Duration) (int64, error) { 36 startTime := time.Now() 37 rows, err := t.db.Query(nowSql) 38 if err != nil { 39 log.Errorf("[Store][database] query now err: %s", err.Error()) 40 return 0, err 41 } 42 defer rows.Close() 43 timePass := time.Since(startTime) 44 if maxWait != 0 && timePass > maxWait { 45 log.Infof("[Store][database] query now spend %s, exceed %s, skip", timePass, maxWait) 46 return 0, nil 47 } 48 var value int64 49 for rows.Next() { 50 if err := rows.Scan(&value); err != nil { 51 log.Errorf("[Store][database] get now rows scan err: %s", err.Error()) 52 return 0, err 53 } 54 } 55 return value, nil 56 }