Skip to content

Commit 63097be

Browse files
author
Peter Mogensen
committed
gofmt
1 parent a953f2b commit 63097be

File tree

12 files changed

+96
-106
lines changed

12 files changed

+96
-106
lines changed

metric/client.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Client struct {
1414
done *sync.WaitGroup
1515

1616
// protects the flusher map
17-
fmu sync.Mutex
17+
fmu sync.Mutex
1818
flushers map[time.Duration]*flusher
1919

2020
// The flusher handling meters which have been given no specific
@@ -40,7 +40,7 @@ func init() {
4040
func NewClient(sinkf SinkFactory, opts ...MOption) (client *Client) {
4141

4242
client = &Client{}
43-
client.done = new(sync.WaitGroup)
43+
client.done = new(sync.WaitGroup)
4444
client.flushers = make(map[time.Duration]*flusher)
4545

4646
client.defaultFlusher = newFlusher(0)
@@ -69,8 +69,8 @@ func (c *Client) SetOptions(opts ...MOption) {
6969
}
7070

7171
c.fmu.Lock()
72-
if f,ok := conf["flushInterval"]; ok {
73-
if d, ok := f.(time.Duration) ; ok {
72+
if f, ok := conf["flushInterval"]; ok {
73+
if d, ok := f.(time.Duration); ok {
7474
c.defaultFlusher.setInterval(d)
7575
}
7676
}
@@ -90,7 +90,7 @@ func (c *Client) SetSink(sinkf SinkFactory) {
9090

9191
c.sinkf = sinkf
9292
c.defaultFlusher.setSink(sinkf)
93-
for _,f := range c.flushers {
93+
for _, f := range c.flushers {
9494
if sinkf == nil {
9595
f.setSink(&nilSinkFactory{})
9696
} else {
@@ -116,7 +116,7 @@ func (c *Client) Start() {
116116

117117
go c.defaultFlusher.rundyn()
118118

119-
for _,f := range c.flushers {
119+
for _, f := range c.flushers {
120120
c.done.Add(1)
121121
f.run(c.done)
122122
}
@@ -141,7 +141,7 @@ func (c *Client) Stop() {
141141

142142
c.defaultFlusher.stop()
143143

144-
for _,f := range c.flushers {
144+
for _, f := range c.flushers {
145145
f.stop()
146146
}
147147
c.done.Wait()
@@ -159,7 +159,7 @@ func (c *Client) register(m Meter, opts ...MOption) {
159159
o(conf)
160160
}
161161

162-
if fi,ok := conf["flushInterval"]; ok {
162+
if fi, ok := conf["flushInterval"]; ok {
163163
flush = fi.(time.Duration)
164164
if f, ok = c.flushers[flush]; !ok {
165165
f = newFlusher(flush)
@@ -202,7 +202,7 @@ func (c *Client) Gauge(name string, val uint64, flush bool) {
202202
// Timer creates an ad-hoc timer metric event.
203203
// If flush is true, the sink will be instructed to flush data immediately
204204
func (c *Client) Timer(name string, d time.Duration, flush bool) {
205-
val := d.Nanoseconds()/int64(1000000)
205+
val := d.Nanoseconds() / int64(1000000)
206206
c.defaultFlusher.RecordNumeric64(MeterTimer, name, Numeric64{Type: Uint64, value: uint64(val)}, flush)
207207
}
208208

metric/counter.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@ func (c *Client) NewCounter(name string, opts ...MOption) *Counter {
2727
}
2828

2929
func (c *Counter) Flush(s Sink) {
30-
val := atomic.SwapInt64(&c.val,0)
30+
val := atomic.SwapInt64(&c.val, 0)
3131
if val != 0 {
32-
n := Numeric64{Type:Int64, value: uint64(val)}
32+
n := Numeric64{Type: Int64, value: uint64(val)}
3333
s.RecordNumeric64(MeterCounter, c.name, n)
3434
}
3535
}

metric/event.go

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,50 @@
11
package metric
22

33
import (
4+
"runtime"
45
"sync"
56
"sync/atomic"
6-
"runtime"
77
)
88

99
// An almost lock-free FIFO buffer
1010
// Locks are only used when flushing
1111

12-
const bufferMaskBits = 8 // determines the size of the buffer
13-
const bufferSize = uint64(1) << bufferMaskBits
14-
const bufferMask = bufferSize - 1
12+
const bufferMaskBits = 8 // determines the size of the buffer
13+
const bufferSize = uint64(1) << bufferMaskBits
14+
const bufferMask = bufferSize - 1
1515

16-
const indexStart = 0
16+
const indexStart = 0
1717

1818
type event struct {
1919
seq uint64
2020
val uint64 // union of 64-bit numeric values including float64
21-
mu sync.Mutex
22-
cv *sync.Cond
21+
mu sync.Mutex
22+
cv *sync.Cond
2323
}
2424

2525
type dequeueFunc func(f Sink, val uint64)
2626

2727
// A generic stream of values which all have to be propagated to the sink.
2828
type eventStream struct {
29-
widx uint64 // index of next free slot
30-
ridx uint64 // index of next unread slot
29+
widx uint64 // index of next free slot
30+
ridx uint64 // index of next unread slot
3131
slots [bufferSize]event
3232

3333
flusher Flusher
3434

3535
dequeue dequeueFunc
3636

37-
name string
37+
name string
3838
mtype int
3939
}
4040

41-
4241
func (c *Client) newEventStream(name string, mtype int, dqf dequeueFunc) *eventStream {
43-
e := &eventStream{name: name, mtype: mtype, dequeue: dqf, widx: indexStart, ridx: indexStart }
42+
e := &eventStream{name: name, mtype: mtype, dequeue: dqf, widx: indexStart, ridx: indexStart}
4443

4544
// make sure first slot is not valid from the start due to zero-value
4645
// and set all sequences to their "old" value
4746
for i := range e.slots {
48-
e.slots[i].seq = uint64(i)-bufferSize
47+
e.slots[i].seq = uint64(i) - bufferSize
4948
e.slots[i].cv = sync.NewCond(&(e.slots[i].mu))
5049
}
5150
return e
@@ -56,7 +55,7 @@ func (e *eventStream) SetFlusher(f Flusher) {
5655
}
5756

5857
// Flush as much as possible
59-
func (e* eventStream) Flush(f Sink) {
58+
func (e *eventStream) Flush(f Sink) {
6059

6160
var idx uint64
6261

@@ -80,20 +79,19 @@ func (e* eventStream) Flush(f Sink) {
8079
atomic.StoreUint64(&(e.ridx), ridx)
8180
}
8281

83-
8482
func (e *eventStream) Name() string {
8583
return e.name
8684
}
8785

8886
func (e *eventStream) MeterType() int {
89-
return e.mtype
87+
return e.mtype
9088
}
9189

9290
func (e *eventStream) Enqueue(val uint64) {
9391

94-
var ridx uint64
95-
var widx uint64
96-
var idx uint64
92+
var ridx uint64
93+
var widx uint64
94+
var idx uint64
9795

9896
// First get a slot
9997
widx = atomic.AddUint64(&(e.widx), 1)
@@ -113,7 +111,7 @@ func (e *eventStream) Enqueue(val uint64) {
113111
// We have not catched up
114112
e.slots[idx].val = val
115113
// mark the slot written
116-
oldmark := atomic.SwapUint64(&(e.slots[idx].seq),widx)
114+
oldmark := atomic.SwapUint64(&(e.slots[idx].seq), widx)
117115

118116
// test to see if someone was waiting for that mark
119117
if oldmark != widx-bufferSize {
@@ -135,16 +133,16 @@ func (e *eventStream) Enqueue(val uint64) {
135133
// we need to make an atomic operation which:
136134
// 1) Decides whether to go to sleep and wait for our slot to be ready.
137135
// 2) Informs the writer of the slot that we want to be woken.
138-
136+
139137
// The slot we are waiting for have sequence 1 buffersize back from rdix,
140138
// if it's still not ready
141139

142-
oldmark := ridx-bufferSize
140+
oldmark := ridx - bufferSize
143141

144142
idx2 := ridx & bufferMask // from here on we look at the stale read index.
145143
e.slots[idx2].mu.Lock()
146144
// Try skew the mark to indicate we're waiting
147-
mustwait := atomic.CompareAndSwapUint64(&(e.slots[idx2].seq),oldmark,oldmark+1)
145+
mustwait := atomic.CompareAndSwapUint64(&(e.slots[idx2].seq), oldmark, oldmark+1)
148146
if mustwait {
149147
// We have now at the same time determined that the slot is not ready
150148
// and set it to indicate that who ever writes it must signal us.
@@ -159,12 +157,12 @@ func (e *eventStream) Enqueue(val uint64) {
159157
if actualmark == oldmark+1 {
160158
// skewed - join the waiters.
161159
e.slots[idx2].cv.Wait()
162-
} else { // stuff can happen fast
160+
} else { // stuff can happen fast
163161
// The slot was actually up to date - so advance the reader
164162
e.flusher.FlushMeter(e)
165163
}
166164
}
167165
e.slots[idx2].mu.Unlock()
168-
166+
169167
}
170168
}

metric/flusher.go

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package metric
22

33
import (
4-
"time"
54
"sync"
5+
"time"
66
)
77

88
// A flusher is either run with a fixed flushinterval with a go-routine which
@@ -21,17 +21,17 @@ type Flusher struct {
2121

2222
type flusher struct {
2323
// Tell the flusher to exit - or (for defaultFlusher) restart
24-
stopChan chan struct{}
24+
stopChan chan struct{}
2525
// Kick the flusher to reconsider interval (used for defaultFlusher)
26-
kickChan chan struct{}
26+
kickChan chan struct{}
2727

2828
interval time.Duration
2929

30-
mu sync.Mutex
30+
mu sync.Mutex
3131
meters []Meter
3232

3333
// only set once by the run/rundyn method to fix how the flusher is used.
34-
ftype int
34+
ftype int
3535

3636
sink Sink
3737
}
@@ -91,7 +91,7 @@ RUNNING: // two cases - either with a flush or not
9191
if interval == 0 {
9292
// sit here waiting doing nothing
9393
select {
94-
case <- f.stopChan:
94+
case <-f.stopChan:
9595
break RUNNING
9696
case <-f.kickChan:
9797
}
@@ -100,13 +100,13 @@ RUNNING: // two cases - either with a flush or not
100100
LOOP:
101101
for {
102102
select {
103-
case <- f.stopChan:
103+
case <-f.stopChan:
104104
ticker.Stop()
105105
break RUNNING
106106
case <-f.kickChan:
107107
ticker.Stop()
108108
break LOOP // to test to make a new ticker
109-
case <- ticker.C:
109+
case <-ticker.C:
110110
f.Flush()
111111
}
112112
}
@@ -132,15 +132,15 @@ func (f *flusher) run(done *sync.WaitGroup) {
132132
// don't start a meaningless flusher
133133
return
134134
}
135-
135+
136136
ticker := time.NewTicker(f.interval)
137137
LOOP:
138138
for {
139139
select {
140-
case <- f.stopChan:
140+
case <-f.stopChan:
141141
ticker.Stop()
142142
break LOOP
143-
case <- ticker.C:
143+
case <-ticker.C:
144144
f.Flush()
145145
}
146146
}
@@ -170,13 +170,11 @@ func (f *flusher) register(m Meter) {
170170
f.mu.Lock()
171171
defer f.mu.Unlock()
172172
f.meters = append(f.meters, m)
173-
if a,ok := m.(AutoFlusher); ok {
173+
if a, ok := m.(AutoFlusher); ok {
174174
a.SetFlusher(Flusher{f})
175175
}
176176
}
177177

178-
179-
180178
func (f *flusher) Record(mtype int, name string, value interface{}, flush bool) {
181179
f.mu.Lock()
182180
f.sink.Record(mtype, name, value)
@@ -195,10 +193,8 @@ func (f *flusher) RecordNumeric64(mtype int, name string, value Numeric64, flush
195193
f.mu.Unlock()
196194
}
197195

198-
199196
func (f *flusher) FlushSink() {
200197
f.mu.Lock()
201198
f.sink.Flush()
202199
f.mu.Unlock()
203200
}
204-

metric/gauge.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package metric
22

33
import (
4-
"sync/atomic"
54
"math"
5+
"sync/atomic"
66
)
77

88
// A client maintained gauge which is only sampled regulary without information loss
@@ -38,6 +38,7 @@ func NewGauge(name string, opts ...MOption) *GaugeUint64 {
3838
func (c *Client) NewGauge(name string, opts ...MOption) *GaugeUint64 {
3939
return c.NewGaugeUint64(name, opts...)
4040
}
41+
4142
/*==================================================================*/
4243

4344
// NewGaugeUint64 returns a standard gauge.
@@ -50,7 +51,7 @@ func (c *Client) NewGaugeUint64(name string, opts ...MOption) *GaugeUint64 {
5051
// Flush to implement Meter interface
5152
func (g *GaugeUint64) Flush(s Sink) {
5253
val := atomic.LoadUint64(&g.val)
53-
n := Numeric64{Type:Uint64, value: val}
54+
n := Numeric64{Type: Uint64, value: val}
5455
s.RecordNumeric64(MeterGauge, g.name, n)
5556
}
5657

@@ -95,7 +96,7 @@ func (c *Client) NewGaugeInt64(name string, opts ...MOption) *GaugeInt64 {
9596

9697
func (g *GaugeInt64) Flush(s Sink) {
9798
val := atomic.LoadInt64(&g.val)
98-
n := Numeric64{Type:Int64, value: uint64(val)}
99+
n := Numeric64{Type: Int64, value: uint64(val)}
99100
s.RecordNumeric64(MeterGauge, g.name, n)
100101
}
101102

@@ -154,6 +155,6 @@ func (g *GaugeFloat64) Value() float64 {
154155

155156
func (g *GaugeFloat64) Flush(s Sink) {
156157
val := atomic.LoadUint64(&g.val)
157-
n := Numeric64{Type:Float64, value: val}
158+
n := Numeric64{Type: Float64, value: val}
158159
s.RecordNumeric64(MeterGauge, g.name, n)
159160
}

metric/histotimer.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func NewTimer(name string, opts ...MOption) *Timer {
3636
}
3737

3838
func (c *Client) NewTimer(name string, opts ...MOption) *Timer {
39-
dequeuef := func(f Sink, val uint64) {
39+
dequeuef := func(f Sink, val uint64) {
4040
n := Numeric64{Type: Uint64, value: val}
4141
f.RecordNumeric64(MeterTimer, name, n)
4242
}
@@ -48,5 +48,5 @@ func (c *Client) NewTimer(name string, opts ...MOption) *Timer {
4848
// Sample records a new duration event.
4949
func (t *Timer) Sample(d time.Duration) {
5050
e := (*eventStream)(t)
51-
e.Enqueue(uint64(d.Nanoseconds()/int64(1000000)))
51+
e.Enqueue(uint64(d.Nanoseconds() / int64(1000000)))
5252
}

0 commit comments

Comments
 (0)