Package list golang-github-go-kit-kit / e308cb4
preventing multiple Starts and adding a Stop JP Robinson 5 years ago
1 changed file(s) with 40 addition(s) and 13 deletion(s). Raw diff Collapse all Expand all
3232
3333 Start(reportInvterval time.Duration) error
3434 Flush()
35 Stop() error
3536 }
3637
3738 type emitter struct {
3839 prefix string
3940
40 addr string
41 tcp bool
42 conn net.Conn
41 addr string
42 tcp bool
43 conn net.Conn
44 start sync.Once
45 stop chan bool
4346
4447 mtx sync.Mutex
4548 counters []*counter
5861 return &emitter{
5962 addr: addr,
6063 tcp: tcp,
64 stop: make(chan bool),
6165 prefix: metricsPrefix,
6266 logger: logger,
6367 }
149153 // Start will kick off a background goroutine to
150154 // call Flush once every interval.
151155 func (e *emitter) Start(interval time.Duration) error {
152 err := e.dial()
153 if err != nil {
154 return err
155 }
156 go func() {
157 for range time.Tick(interval) {
158 e.Flush()
159 }
160 }()
161 return nil
156 var err error
157 e.start.Do(func() {
158 err = e.dial()
159 if err != nil {
160 return
161 }
162 go func() {
163 t := time.Tick(interval)
164 for {
165 select {
166 case <-t:
167 e.Flush()
168 case <-e.stop:
169 return
170 }
171 }
172 }()
173 })
174 return err
175 }
176
177 // Stop will flush the current metrics and close the
178 // current Graphite connection, if it exists.
179 func (e *emitter) Stop() error {
180 if e.conn == nil {
181 return nil
182 }
183 // stop the ticking flush loop
184 e.stop <- true
185 // get one last flush in
186 e.Flush()
187 // close the connection
188 return e.conn.Close()
162189 }
163190
164191 // Flush will attempt to create a connection with the given address