github.com/braveheart12/just@v0.8.7/network/transport/pool/entry.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/instrumentation/instracer" 27 "github.com/insolar/insolar/network/utils" 28 "github.com/pkg/errors" 29 "go.opencensus.io/trace" 30 ) 31 32 type entryImpl struct { 33 connectionFactory connectionFactory 34 address net.Addr 35 onClose onClose 36 37 mutex *sync.Mutex 38 39 conn net.Conn 40 } 41 42 func newEntryImpl(connectionFactory connectionFactory, address net.Addr, onClose onClose) *entryImpl { 43 return &entryImpl{ 44 connectionFactory: connectionFactory, 45 address: address, 46 mutex: &sync.Mutex{}, 47 onClose: onClose, 48 } 49 } 50 51 func (e *entryImpl) Open(ctx context.Context) (net.Conn, error) { 52 e.mutex.Lock() 53 defer e.mutex.Unlock() 54 55 if e.conn != nil { 56 return e.conn, nil 57 } 58 59 conn, err := e.open(ctx) 60 if err != nil { 61 return nil, err 62 } 63 64 e.conn = conn 65 return e.conn, nil 66 } 67 68 func (e *entryImpl) open(ctx context.Context) (net.Conn, error) { 69 logger := inslogger.FromContext(ctx) 70 ctx, span := instracer.StartSpan(ctx, "connectionPool.open") 71 span.AddAttributes( 72 trace.StringAttribute("create connect to", e.address.String()), 73 ) 74 defer span.End() 75 76 conn, err := e.connectionFactory.CreateConnection(ctx, e.address) 77 if err != nil { 78 return nil, errors.Wrap(err, "[ Open ] Failed to create TCP connection") 79 } 80 81 go func(e *entryImpl, conn net.Conn) { 82 b := make([]byte, 1) 83 _, err := conn.Read(b) 84 if err != nil { 85 logger.Infof("[ Open ] remote host 'closed' connection to %s: %s", e.address, err) 86 e.onClose(ctx, e.address) 87 return 88 } 89 90 logger.Errorf("[ Open ] unexpected data on connection to %s", e.address) 91 }(e, conn) 92 93 return conn, nil 94 } 95 96 func (e *entryImpl) Close() { 97 e.mutex.Lock() 98 defer e.mutex.Unlock() 99 100 if e.conn != nil { 101 utils.CloseVerbose(e.conn) 102 } 103 }