Skip to content

Commit f308b4b

Browse files
author
Guy Baron
authored
added golangcli lint configuration and fixed linting failures (#165)
1 parent abf9e9d commit f308b4b

File tree

8 files changed

+98
-52
lines changed

8 files changed

+98
-52
lines changed

.golangci.yml

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
linters-settings:
2+
golint:
3+
# minimal confidence for issues, default is 0.8
4+
min-confidence: 0.8
5+
gocyclo:
6+
min-complexity: 15
7+
8+
govet:
9+
# report about shadowed variables
10+
check-shadowing: false
11+
12+
# settings per analyzer
13+
settings:
14+
printf: # analyzer name, run `go tool vet help` to see all analyzers
15+
funcs: # run `go tool vet help printf` to see available settings for `printf` analyzer
16+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Infof
17+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Warnf
18+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Errorf
19+
- (github.com/golangci/golangci-lint/pkg/logutils.Log).Fatalf
20+
linters:
21+
disable-all: true
22+
enable:
23+
- deadcode
24+
# - errcheck
25+
- gosimple
26+
- govet
27+
- ineffassign
28+
# - staticcheck
29+
# - typecheck
30+
- unused
31+
# - varcheck
32+
# - deadcode
33+
# - dupl
34+
# - gocritic
35+
- gocyclo
36+
- golint
37+
- misspell

gbus/metrics/handler_metrics.go

+12-10
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package metrics
22

33
import (
44
"fmt"
5-
"github.com/prometheus/client_golang/prometheus/promauto"
65
"sync"
76

7+
"github.com/prometheus/client_golang/prometheus/promauto"
8+
io_prometheus_client "github.com/prometheus/client_model/go"
9+
810
"github.com/prometheus/client_golang/prometheus"
9-
"github.com/prometheus/client_model/go"
1011
"github.com/sirupsen/logrus"
1112
)
1213

@@ -37,7 +38,8 @@ const (
3738
handler = "handler"
3839
)
3940

40-
type handlerMetrics struct {
41+
//HandlerMetrics holds the metrics results for a handler
42+
type HandlerMetrics struct {
4143
result *prometheus.CounterVec
4244
latency prometheus.Summary
4345
}
@@ -84,17 +86,17 @@ func RunHandlerWithMetric(handleMessage func() error, handlerName, messageType s
8486
}
8587

8688
//GetHandlerMetrics gets the metrics handler associated with the handlerName
87-
func GetHandlerMetrics(handlerName string) *handlerMetrics {
89+
func GetHandlerMetrics(handlerName string) *HandlerMetrics {
8890
entry, ok := handlerMetricsByHandlerName.Load(handlerName)
8991
if ok {
90-
return entry.(*handlerMetrics)
92+
return entry.(*HandlerMetrics)
9193
}
9294

9395
return nil
9496
}
9597

96-
func newHandlerMetrics(handlerName string) *handlerMetrics {
97-
return &handlerMetrics{
98+
func newHandlerMetrics(handlerName string) *HandlerMetrics {
99+
return &HandlerMetrics{
98100
result: prometheus.NewCounterVec(
99101
prometheus.CounterOpts{
100102
Namespace: grabbitPrefix,
@@ -150,17 +152,17 @@ func GetLatencySampleCountByMessageTypeAndHandlerName(messageType, handlerName s
150152
}
151153

152154
//GetSuccessCount gets the value of the handlers success value
153-
func (hm *handlerMetrics) GetSuccessCount() (float64, error) {
155+
func (hm *HandlerMetrics) GetSuccessCount() (float64, error) {
154156
return getCounterValue(hm.result.WithLabelValues(success))
155157
}
156158

157159
//GetFailureCount gets the value of the handlers failure value
158-
func (hm *handlerMetrics) GetFailureCount() (float64, error) {
160+
func (hm *HandlerMetrics) GetFailureCount() (float64, error) {
159161
return getCounterValue(hm.result.WithLabelValues(failure))
160162
}
161163

162164
//GetLatencySampleCount gets the value of the handlers latency value
163-
func (hm *handlerMetrics) GetLatencySampleCount() (*uint64, error) {
165+
func (hm *HandlerMetrics) GetLatencySampleCount() (*uint64, error) {
164166
return getSummarySampleCount(hm.latency)
165167
}
166168

gbus/saga/glue.go

+34-30
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,39 @@ func (imsm *Glue) getDefsForMsgName(msgName string) []*Def {
101101
return defs
102102
}
103103

104+
func (imsm *Glue) handleNewSaga(def *Def, invocation gbus.Invocation, message *gbus.BusMessage) error {
105+
newInstance := def.newInstance()
106+
newInstance.StartedBy = invocation.InvokingSvc()
107+
newInstance.StartedBySaga = message.SagaCorrelationID
108+
newInstance.StartedByRPCID = message.RPCID
109+
newInstance.StartedByMessageID = message.ID
110+
111+
imsm.Log().
112+
WithFields(logrus.Fields{"saga_def": def.String(), "saga_id": newInstance.ID}).
113+
Info("created new saga")
114+
if invkErr := imsm.invokeSagaInstance(def, newInstance, invocation, message); invkErr != nil {
115+
imsm.Log().WithError(invkErr).WithField("saga_id", newInstance.ID).Error("failed to invoke saga")
116+
return invkErr
117+
}
118+
119+
if !newInstance.isComplete() {
120+
imsm.Log().WithField("saga_id", newInstance.ID).Info("saving new saga")
121+
122+
if e := imsm.sagaStore.SaveNewSaga(invocation.Tx(), def.sagaType, newInstance); e != nil {
123+
imsm.Log().WithError(e).WithField("saga_id", newInstance.ID).Error("saving new saga failed")
124+
return e
125+
}
126+
127+
if requestsTimeout, duration := newInstance.requestsTimeout(); requestsTimeout {
128+
imsm.Log().WithFields(logrus.Fields{"saga_id": newInstance.ID, "timeout_duration": duration}).Info("new saga requested timeout")
129+
if tme := imsm.timeoutManager.RegisterTimeout(invocation.Tx(), newInstance.ID, duration); tme != nil {
130+
return tme
131+
}
132+
}
133+
}
134+
return nil
135+
}
136+
104137
//SagaHandler is the generic handler invoking saga instances
105138
func (imsm *Glue) SagaHandler(invocation gbus.Invocation, message *gbus.BusMessage) error {
106139

@@ -121,37 +154,8 @@ func (imsm *Glue) SagaHandler(invocation gbus.Invocation, message *gbus.BusMessa
121154
*/
122155
startNew := def.shouldStartNewSaga(message)
123156
if startNew {
157+
return imsm.handleNewSaga(def, invocation, message)
124158

125-
newInstance := def.newInstance()
126-
newInstance.StartedBy = invocation.InvokingSvc()
127-
newInstance.StartedBySaga = message.SagaCorrelationID
128-
newInstance.StartedByRPCID = message.RPCID
129-
newInstance.StartedByMessageID = message.ID
130-
131-
imsm.Log().
132-
WithFields(logrus.Fields{"saga_def": def.String(), "saga_id": newInstance.ID}).
133-
Info("created new saga")
134-
if invkErr := imsm.invokeSagaInstance(def, newInstance, invocation, message); invkErr != nil {
135-
imsm.Log().WithError(invkErr).WithField("saga_id", newInstance.ID).Error("failed to invoke saga")
136-
return invkErr
137-
}
138-
139-
if !newInstance.isComplete() {
140-
imsm.Log().WithField("saga_id", newInstance.ID).Info("saving new saga")
141-
142-
if e := imsm.sagaStore.SaveNewSaga(invocation.Tx(), def.sagaType, newInstance); e != nil {
143-
imsm.Log().WithError(e).WithField("saga_id", newInstance.ID).Error("saving new saga failed")
144-
return e
145-
}
146-
147-
if requestsTimeout, duration := newInstance.requestsTimeout(); requestsTimeout {
148-
imsm.Log().WithFields(logrus.Fields{"saga_id": newInstance.ID, "timeout_duration": duration}).Info("new saga requested timeout")
149-
if tme := imsm.timeoutManager.RegisterTimeout(invocation.Tx(), newInstance.ID, duration); tme != nil {
150-
return tme
151-
}
152-
}
153-
}
154-
return nil
155159
} else if message.SagaCorrelationID != "" {
156160
instance, getErr := imsm.sagaStore.GetSagaByID(invocation.Tx(), message.SagaCorrelationID)
157161

gbus/saga/instance.go

+1
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func (si *Instance) invoke(exchange, routingKey string, invocation *sagaInvocati
6363
}).Info("invoking method on saga")
6464

6565
span, sctx := opentracing.StartSpanFromContext(invocation.Ctx(), methodName)
66+
6667
// replace the original context with the conext built around the span so we ca
6768
// trace the saga handler that is invoked
6869
invocation.ctx = sctx

gbus/tx/mysql/migrations.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func sagaStoreAddSagaCreatorDetails(svcName string) *migrator.Migration {
4747
}
4848
}
4949

50-
func sagaStoreAdd_RPCID_Details(svcName string) *migrator.Migration {
50+
func sagaStoreAddRPCIDDetails(svcName string) *migrator.Migration {
5151
tblName := tx.GrabbitTableNameTemplate(svcName, "sagas")
5252

5353
addCreatorDetailsSQL := `ALTER TABLE ` + tblName + ` ADD COLUMN started_by_msg_id VARCHAR(50) AFTER started_by_request_of_svc, ADD COLUMN started_by_rpcid VARCHAR(50) AFTER started_by_msg_id`
@@ -157,7 +157,7 @@ func EnsureSchema(db *sql.DB, svcName string) {
157157
legacyMigrationsTable(svcName),
158158
outboxChangeColumnLength(svcName),
159159
sagaStoreAddSagaCreatorDetails(svcName),
160-
sagaStoreAdd_RPCID_Details(svcName),
160+
sagaStoreAddRPCIDDetails(svcName),
161161
))
162162
if err != nil {
163163
panic(err)

gbus/tx/mysql/txoutbox.go

+11-6
Original file line numberDiff line numberDiff line change
@@ -294,12 +294,8 @@ func (outbox *TxOutbox) sendMessages(recordSelector func(tx *sql.Tx) (*sql.Rows,
294294
}
295295
}
296296

297-
for recid := range failedDeliveries {
298-
_, updateErr := tx.Exec("UPDATE "+getOutboxName(outbox.svcName)+" SET delivery_attempts=delivery_attempts+1 WHERE rec_id=?", recid)
299-
if updateErr != nil {
300-
outbox.log().WithError(updateErr).WithField("record_id", recid).Warn("failed to update transactional outbox with failed deivery attempt for record")
301-
}
302-
}
297+
outbox.updateFailedDeliveries(tx, failedDeliveries)
298+
303299
if cmtErr := tx.Commit(); cmtErr != nil {
304300
outbox.log().WithError(cmtErr).Error("Error committing outbox transaction")
305301
} else {
@@ -314,6 +310,15 @@ func (outbox *TxOutbox) sendMessages(recordSelector func(tx *sql.Tx) (*sql.Rows,
314310
return nil
315311
}
316312

313+
func (outbox *TxOutbox) updateFailedDeliveries(tx *sql.Tx, failedDeliveries []int) {
314+
for recid := range failedDeliveries {
315+
_, updateErr := tx.Exec("UPDATE "+getOutboxName(outbox.svcName)+" SET delivery_attempts=delivery_attempts+1 WHERE rec_id=?", recid)
316+
if updateErr != nil {
317+
outbox.log().WithError(updateErr).WithField("record_id", recid).Warn("failed to update transactional outbox with failed deivery attempt for record")
318+
}
319+
}
320+
}
321+
317322
func getOutboxName(svcName string) string {
318323

319324
return tx.GrabbitTableNameTemplate(svcName, "outbox")

tests/bus_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ func TestRawMessageHandling(t *testing.T) {
319319
func TestReturnDeadToQueue(t *testing.T) {
320320

321321
var visited bool
322-
proceed := make(chan bool, 0)
322+
proceed := make(chan bool)
323323
poison := gbus.NewBusMessage(Command1{})
324324

325325
service1 := createBusWithConfig(testSvc1, "grabbit-dead", true, true,

tests/testMessages.go

-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ type Reply2 struct {
6464
Data string
6565
}
6666

67-
6867
//SchemaName implementing gbus.Message
6968
func (Reply2) SchemaName() string {
7069
return "grabbit.tests.Reply2"
@@ -80,8 +79,6 @@ func (Reply3) SchemaName() string {
8079
return "grabbit.tests.Reply3"
8180
}
8281

83-
84-
8582
//Event1 for testing
8683
type Event1 struct {
8784
Data string

0 commit comments

Comments
 (0)