github.com/braveheart12/just@v0.8.7/network/utils/utils.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 utils
    19  
    20  import (
    21  	"hash/crc32"
    22  	"io"
    23  	"sync"
    24  	"sync/atomic"
    25  	"time"
    26  
    27  	"github.com/insolar/insolar/core"
    28  	"github.com/insolar/insolar/log"
    29  )
    30  
    31  func WaitTimeout(wg *sync.WaitGroup, timeout time.Duration) bool {
    32  	c := make(chan struct{})
    33  	go func() {
    34  		defer close(c)
    35  		wg.Wait()
    36  	}()
    37  	select {
    38  	case <-c:
    39  		return true // completed normally
    40  	case <-time.After(timeout):
    41  		return false // timed out
    42  	}
    43  }
    44  
    45  // AtomicLoadAndIncrementUint64 performs CAS loop, increments counter and returns old value.
    46  func AtomicLoadAndIncrementUint64(addr *uint64) uint64 {
    47  	for {
    48  		val := atomic.LoadUint64(addr)
    49  		if atomic.CompareAndSwapUint64(addr, val, val+1) {
    50  			return val
    51  		}
    52  	}
    53  }
    54  
    55  // GenerateShortID generate short ID for node without checking collisions
    56  func GenerateShortID(ref core.RecordRef) core.ShortNodeID {
    57  	return core.ShortNodeID(GenerateUintShortID(ref))
    58  }
    59  
    60  // GenerateShortID generate short ID for node without checking collisions
    61  func GenerateUintShortID(ref core.RecordRef) uint32 {
    62  	return crc32.ChecksumIEEE(ref[:])
    63  }
    64  
    65  func OriginIsDiscovery(cert core.Certificate) bool {
    66  	bNodes := cert.GetDiscoveryNodes()
    67  	for _, discoveryNode := range bNodes {
    68  		if cert.GetNodeRef().Equal(*discoveryNode.GetNodeRef()) {
    69  			return true
    70  		}
    71  	}
    72  	return false
    73  }
    74  
    75  func CloseVerbose(closer io.Closer) {
    76  	err := closer.Close()
    77  	if err != nil {
    78  		log.Errorf("[ CloseVerbose ] Failed to close: %s", err.Error())
    79  	}
    80  }