Remove BPA from Makefile
[icn.git] / cmd / bpa-operator / vendor / contrib.go.opencensus.io / exporter / ocagent / connection.go
1 // Copyright 2018, OpenCensus Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package ocagent
16
17 import (
18         "math/rand"
19         "sync/atomic"
20         "time"
21 )
22
23 const (
24         sDisconnected int32 = 5 + iota
25         sConnected
26 )
27
28 func (ae *Exporter) setStateDisconnected() {
29         atomic.StoreInt32(&ae.connectionState, sDisconnected)
30         select {
31         case ae.disconnectedCh <- true:
32         default:
33         }
34 }
35
36 func (ae *Exporter) setStateConnected() {
37         atomic.StoreInt32(&ae.connectionState, sConnected)
38 }
39
40 func (ae *Exporter) connected() bool {
41         return atomic.LoadInt32(&ae.connectionState) == sConnected
42 }
43
44 const defaultConnReattemptPeriod = 10 * time.Second
45
46 func (ae *Exporter) indefiniteBackgroundConnection() error {
47         defer func() {
48                 ae.backgroundConnectionDoneCh <- true
49         }()
50
51         connReattemptPeriod := ae.reconnectionPeriod
52         if connReattemptPeriod <= 0 {
53                 connReattemptPeriod = defaultConnReattemptPeriod
54         }
55
56         // No strong seeding required, nano time can
57         // already help with pseudo uniqueness.
58         rng := rand.New(rand.NewSource(time.Now().UnixNano() + rand.Int63n(1024)))
59
60         // maxJitter: 1 + (70% of the connectionReattemptPeriod)
61         maxJitter := int64(1 + 0.7*float64(connReattemptPeriod))
62
63         for {
64                 // Otherwise these will be the normal scenarios to enable
65                 // reconnections if we trip out.
66                 // 1. If we've stopped, return entirely
67                 // 2. Otherwise block until we are disconnected, and
68                 //    then retry connecting
69                 select {
70                 case <-ae.stopCh:
71                         return errStopped
72
73                 case <-ae.disconnectedCh:
74                         // Normal scenario that we'll wait for
75                 }
76
77                 if err := ae.connect(); err == nil {
78                         ae.setStateConnected()
79                 } else {
80                         ae.setStateDisconnected()
81                 }
82
83                 // Apply some jitter to avoid lockstep retrials of other
84                 // agent-exporters. Lockstep retrials could result in an
85                 // innocent DDOS, by clogging the machine's resources and network.
86                 jitter := time.Duration(rng.Int63n(maxJitter))
87                 <-time.After(connReattemptPeriod + jitter)
88         }
89 }
90
91 func (ae *Exporter) connect() error {
92         cc, err := ae.dialToAgent()
93         if err != nil {
94                 return err
95         }
96         return ae.enableConnectionStreams(cc)
97 }