github.com/braveheart12/insolar-09-08-19@v0.8.7/network/transport/relay/proxy.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 relay
    19  
    20  // Proxy contains proxy addresses.
    21  type Proxy interface {
    22  	// AddProxyHost add an address to proxy list.
    23  	AddProxyHost(address string)
    24  	// RemoveProxyHost removes proxy address from proxy list.
    25  	RemoveProxyHost(address string)
    26  	// GetNextProxyAddress returns a next address to send from proxy list.
    27  	GetNextProxyAddress() string
    28  	// ProxyHostsCount return added proxy count.
    29  	ProxyHostsCount() int
    30  }
    31  
    32  // Note: thread unsafe!!!
    33  type proxy struct {
    34  	proxyList []string
    35  	iterator  int
    36  }
    37  
    38  // NewProxy instantiates proxy struct.
    39  func NewProxy() Proxy {
    40  	return &proxy{
    41  		proxyList: make([]string, 0),
    42  		iterator:  0,
    43  	}
    44  }
    45  
    46  // AddProxyHost add an address to proxy list.
    47  func (p *proxy) AddProxyHost(address string) {
    48  	i := p.getProxyIndex(address)
    49  
    50  	if i != -1 {
    51  		return
    52  	}
    53  	p.proxyList = append(p.proxyList, address)
    54  }
    55  
    56  // RemoveProxyHost removes proxy address from proxy list.
    57  func (p *proxy) RemoveProxyHost(address string) {
    58  	i := p.getProxyIndex(address)
    59  
    60  	if i == -1 {
    61  		return
    62  	}
    63  
    64  	p.proxyList = append(p.proxyList[:i], p.proxyList[i+1:]...)
    65  }
    66  
    67  // getProxyIndex returns index of address in proxy list.
    68  func (p *proxy) getProxyIndex(address string) int {
    69  	for i := 0; i < len(p.proxyList); i++ {
    70  		if p.proxyList[i] == address {
    71  			return i
    72  		}
    73  	}
    74  
    75  	return -1
    76  }
    77  
    78  // GetNextProxyAddress returns a next address to send from proxy list.
    79  func (p *proxy) GetNextProxyAddress() string {
    80  	if len(p.proxyList) == 0 {
    81  		return ""
    82  	}
    83  	if p.iterator >= len(p.proxyList) {
    84  		p.iterator = 0
    85  	}
    86  	p.iterator++
    87  	return p.proxyList[p.iterator-1]
    88  }
    89  
    90  // ProxyHostsCount return added proxy count.
    91  func (p *proxy) ProxyHostsCount() int {
    92  	return len(p.proxyList)
    93  }