vitess.io/vitess@v0.16.2/go/vt/dbconnpool/pooled_connection.go (about) 1 /* 2 Copyright 2019 The Vitess Authors. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package dbconnpool 18 19 import ( 20 "context" 21 "time" 22 23 "vitess.io/vitess/go/pools" 24 ) 25 26 // PooledDBConnection re-exposes DBConnection to be used by ConnectionPool. 27 type PooledDBConnection struct { 28 *DBConnection 29 timeCreated time.Time 30 pool *ConnectionPool 31 } 32 33 func (pc *PooledDBConnection) Expired(lifetimeTimeout time.Duration) bool { 34 return lifetimeTimeout > 0 && time.Until(pc.timeCreated.Add(lifetimeTimeout)) < 0 35 } 36 37 func (pc *PooledDBConnection) ApplySetting(context.Context, *pools.Setting) error { 38 //TODO implement me 39 panic("implement me") 40 } 41 42 func (pc *PooledDBConnection) IsSettingApplied() bool { 43 return false 44 } 45 46 func (pc *PooledDBConnection) IsSameSetting(string) bool { 47 return true 48 } 49 50 func (pc *PooledDBConnection) ResetSetting(context.Context) error { 51 //TODO implement me 52 panic("implement me") 53 } 54 55 // Recycle should be called to return the PooledDBConnection to the pool. 56 func (pc *PooledDBConnection) Recycle() { 57 if pc.IsClosed() { 58 pc.pool.Put(nil) 59 } else { 60 pc.pool.Put(pc) 61 } 62 } 63 64 // Reconnect replaces the existing underlying connection with a new one, 65 // if possible. Recycle should still be called afterwards. 66 func (pc *PooledDBConnection) Reconnect(ctx context.Context) error { 67 pc.DBConnection.Close() 68 newConn, err := NewDBConnection(ctx, pc.pool.info) 69 if err != nil { 70 return err 71 } 72 pc.DBConnection = newConn 73 return nil 74 }