github.com/braveheart12/just@v0.8.7/network/transport/pool/connectionpool.go (about) 1 /* 2 * The Clear BSD License 3 * 4 * Copyright (c) 2019 Insolar Technologies 5 * 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without modification, are permitted (subject to the limitations in the disclaimer below) provided that the following conditions are met: 9 * 10 * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 11 * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 12 * Neither the name of Insolar Technologies nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. 13 * 14 * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 15 * 16 */ 17 18 package pool 19 20 import ( 21 "context" 22 "net" 23 "sync" 24 25 "github.com/insolar/insolar/instrumentation/inslogger" 26 "github.com/insolar/insolar/metrics" 27 ) 28 29 type connectionPool struct { 30 connectionFactory connectionFactory 31 32 entryHolder entryHolder 33 mutex sync.RWMutex 34 } 35 36 func newConnectionPool(connectionFactory connectionFactory) *connectionPool { 37 return &connectionPool{ 38 connectionFactory: connectionFactory, 39 40 entryHolder: newEntryHolder(), 41 } 42 } 43 44 func (cp *connectionPool) GetConnection(ctx context.Context, address net.Addr) (net.Conn, error) { 45 logger := inslogger.FromContext(ctx) 46 47 entry, ok := cp.getEntry(address) 48 49 logger.Debugf("[ GetConnection ] Finding entry for connection to %s in pool: %t", address, ok) 50 51 if ok { 52 return entry.Open(ctx) 53 } 54 55 logger.Debugf("[ GetConnection ] Missing entry for connection to %s in pool ", address) 56 entry = cp.getOrCreateEntry(ctx, address) 57 58 return entry.Open(ctx) 59 } 60 61 func (cp *connectionPool) CloseConnection(ctx context.Context, address net.Addr) { 62 cp.mutex.Lock() 63 defer cp.mutex.Unlock() 64 65 logger := inslogger.FromContext(ctx) 66 67 entry, ok := cp.entryHolder.Get(address) 68 logger.Debugf("[ CloseConnection ] Finding entry for connection to %s in pool: %s", address, ok) 69 70 if ok { 71 entry.Close() 72 73 logger.Debugf("[ CloseConnection ] Delete entry for connection to %s from pool", address) 74 cp.entryHolder.Delete(address) 75 metrics.NetworkConnections.Dec() 76 } 77 } 78 79 func (cp *connectionPool) getEntry(address net.Addr) (entry, bool) { 80 cp.mutex.RLock() 81 defer cp.mutex.RUnlock() 82 83 return cp.entryHolder.Get(address) 84 } 85 86 func (cp *connectionPool) getOrCreateEntry(ctx context.Context, address net.Addr) entry { 87 logger := inslogger.FromContext(ctx) 88 89 cp.mutex.Lock() 90 defer cp.mutex.Unlock() 91 92 entry, ok := cp.entryHolder.Get(address) 93 logger.Debugf("[ getOrCreateEntry ] Finding entry for connection to %s in pool: %s", address, ok) 94 95 if ok { 96 return entry 97 } 98 99 logger.Debugf("[ getOrCreateEntry ] Failed to retrieve entry for connection to %s, creating it", address) 100 101 entry = newEntry(cp.connectionFactory, address, cp.CloseConnection) 102 103 cp.entryHolder.Add(address, entry) 104 size := cp.entryHolder.Size() 105 logger.Debugf( 106 "[ getOrCreateEntry ] Added entry for connection to %s. Current pool size: %d", 107 address.String(), 108 size, 109 ) 110 metrics.NetworkConnections.Inc() 111 112 return entry 113 } 114 115 func (cp *connectionPool) Reset() { 116 cp.mutex.Lock() 117 defer cp.mutex.Unlock() 118 119 cp.entryHolder.Iterate(func(entry entry) { 120 entry.Close() 121 }) 122 cp.entryHolder.Clear() 123 metrics.NetworkConnections.Set(float64(cp.entryHolder.Size())) 124 }