Skip to content

Commit d1cb5dd

Browse files
committed
fix: post v0.13 fixes
- loop race - CI - linter
1 parent 8dcb195 commit d1cb5dd

File tree

13 files changed

+49
-46
lines changed

13 files changed

+49
-46
lines changed

.golangci.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
run:
2-
exclude-files:
3-
- "examples/.*"
4-
- "scripts/.*"
1+
issues:
2+
exclude-dirs:
3+
- "vendor"
4+
- "third_party"
5+
- "pkg/pubsub/uds"
56

67
linters:
78
enable:

pkg/helpers/help.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ import (
2020

2121
"github.com/failsafe-go/failsafe-go"
2222
"github.com/failsafe-go/failsafe-go/retrypolicy"
23+
"golang.org/x/sync/errgroup"
24+
2325
"github.com/pancsta/asyncmachine-go/internal/utils"
2426
"github.com/pancsta/asyncmachine-go/pkg/states/pipes"
25-
"golang.org/x/sync/errgroup"
2627

2728
am "github.com/pancsta/asyncmachine-go/pkg/machine"
2829
ss "github.com/pancsta/asyncmachine-go/pkg/states"

pkg/machine/machine.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ type Machine struct {
9797
stateNames S
9898
stateNamesExport S
9999
handlersLock sync.Mutex
100+
loopLock sync.Mutex
100101
handlers []*handler
101102
clock Clock
102103
cancel context.CancelFunc
@@ -396,6 +397,9 @@ func (m *Machine) doDispose(force bool) {
396397
m.tracers = nil
397398
go func() {
398399
time.Sleep(100 * time.Millisecond)
400+
m.loopLock.Lock()
401+
defer m.loopLock.Unlock()
402+
399403
closeSafe(m.handlerEnd)
400404
closeSafe(m.handlerPanic)
401405
closeSafe(m.handlerStart)
@@ -1620,21 +1624,25 @@ func (m *Machine) MustParseStates(states S) S {
16201624
}
16211625

16221626
// check if all states are defined in m.Struct
1623-
// TODO optimize?
1624-
ret := make(S, len(states))
16251627
seen := make(map[string]struct{})
1628+
dups := false
16261629
for i := range states {
16271630
if _, ok := m.schema[states[i]]; !ok {
16281631
panic(fmt.Errorf(
16291632
"%w: %s not defined in schema for %s", ErrStateMissing,
16301633
states[i], m.id))
16311634
}
16321635
if _, ok := seen[states[i]]; !ok {
1633-
ret = append(ret, states[i])
16341636
seen[states[i]] = struct{}{}
1637+
} else {
1638+
// mark as duplicated
1639+
dups = true
16351640
}
16361641
}
16371642

1643+
if dups {
1644+
return slicesUniq(states)
1645+
}
16381646
return states
16391647
}
16401648

@@ -2546,14 +2554,17 @@ func (m *Machine) handlerLoop() {
25462554
return
25472555
}
25482556

2557+
m.loopLock.Lock()
2558+
25492559
// pass the result to handlerLoop
25502560
select {
25512561
case <-m.ctx.Done():
25522562
m.handlerLoopDone()
2563+
m.loopLock.Unlock()
25532564
return
25542565

25552566
case m.handlerEnd <- ret:
2556-
// pass
2567+
m.loopLock.Unlock()
25572568
}
25582569
}
25592570
}
@@ -2675,8 +2686,8 @@ func (m *Machine) IsQueued(mutationType MutationType, states S,
26752686
}
26762687

26772688
// IsQueuedAbove allows for rate-limiting of mutations for a specific state.
2678-
func (m *Machine) IsQueuedAbove(threshold int, mutationType MutationType, states S,
2679-
withoutArgsOnly bool, statesStrictEqual bool, startIndex int,
2689+
func (m *Machine) IsQueuedAbove(threshold int, mutationType MutationType,
2690+
states S, withoutArgsOnly bool, statesStrictEqual bool, startIndex int,
26802691
) bool {
26812692
if m.disposed.Load() {
26822693
return false

pkg/machine/misc_mach.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -483,9 +483,6 @@ type Event struct {
483483
// Machine is the machine that the event belongs to. It can be used to access
484484
// the current Transition and Mutation.
485485
machine *Machine
486-
// internal events always have a nil step
487-
step *Step
488-
internal bool
489486
}
490487

491488
// Mutation returns the Mutation of an Event.

pkg/machine/transition.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,6 @@ func (t *Transition) CleanCache() {
192192

193193
// StatesBefore is a list of states before the transition.
194194
func (t *Transition) StatesBefore() S {
195-
196195
// cache
197196
if v := t.cacheStatesBefore.Load(); v != nil {
198197
return *v
@@ -216,7 +215,6 @@ func (t *Transition) StatesBefore() S {
216215

217216
// TargetStates is a list of states after parsing the relations.
218217
func (t *Transition) TargetStates() S {
219-
220218
// cache
221219
if v := t.cacheTargetStates.Load(); v != nil {
222220
return *v
@@ -455,7 +453,6 @@ func (t *Transition) emitExitEvents() Result {
455453
func (t *Transition) emitHandler(
456454
from, to string, isFinal, isEnter bool, event string, args A,
457455
) Result {
458-
459456
t.latestStepToState = to
460457
ret, handlerCalled := t.Machine.handle(event, args, isFinal, isEnter, false)
461458

pkg/machine/types.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -355,19 +355,19 @@ type Mutation struct {
355355
cacheCalled atomic.Pointer[S]
356356
}
357357

358-
func (m Mutation) String() string {
358+
func (m *Mutation) String() string {
359359
return fmt.Sprintf("[%s] %v", m.Type, m.Called)
360360
}
361361

362-
func (m Mutation) IsCalled(idx int) bool {
362+
func (m *Mutation) IsCalled(idx int) bool {
363363
return slices.Contains(m.Called, idx)
364364
}
365365

366-
func (m Mutation) CalledIndex(index S) *TimeIndex {
366+
func (m *Mutation) CalledIndex(index S) *TimeIndex {
367367
return NewTimeIndex(index, m.Called)
368368
}
369369

370-
func (m Mutation) StringFromIndex(index S) string {
370+
func (m *Mutation) StringFromIndex(index S) string {
371371
called := NewTimeIndex(index, m.Called)
372372
return "[" + m.Type.String() + "] " + j(called.ActiveStates())
373373
}

pkg/pubsub/pubsub_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ import (
1414

1515
"github.com/joho/godotenv"
1616
ma "github.com/multiformats/go-multiaddr"
17-
amtele "github.com/pancsta/asyncmachine-go/pkg/telemetry"
1817
"github.com/stretchr/testify/assert"
1918
"golang.org/x/sync/errgroup"
2019

20+
amtele "github.com/pancsta/asyncmachine-go/pkg/telemetry"
21+
2122
amhelp "github.com/pancsta/asyncmachine-go/pkg/helpers"
2223
amhelpt "github.com/pancsta/asyncmachine-go/pkg/helpers/testing"
2324
am "github.com/pancsta/asyncmachine-go/pkg/machine"
@@ -66,7 +67,8 @@ type mirrorHandlers struct {
6667
ProcessMsgsState am.HandlerFinal
6768
}
6869

69-
var mirrorStates = am.S{am.Exception,
70+
var mirrorStates = am.S{
71+
am.Exception,
7072
ss.Joined, ss.Ready, ss.MissPeersByUpdates, ss.MissPeersByGossip,
7173
ss.MissUpdatesByGossip, ss.SendGossips, ss.SendUpdates, ss.SendInfo,
7274
ss.MsgReqUpdates, ss.MsgReqInfo, ss.ProcessMsgs, ss.ReqMissingUpdates,
@@ -534,7 +536,7 @@ func TestExposingMany(t *testing.T) {
534536
wg.Add(1)
535537
go func() {
536538
ps.Dispose()
537-
amhelp.WaitForAll(ctx, 100*time.Millisecond,
539+
_ = amhelp.WaitForAll(ctx, 100*time.Millisecond,
538540
ps.Mach.When1(ss.Disposed, nil))
539541
ps.Mach.Dispose()
540542
wg.Done()

pkg/pubsub/topic.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ func (t *Topic) ExceptionEnter(e *am.Event) bool {
214214
}
215215

216216
func (t *Topic) ExceptionState(e *am.Event) {
217-
218217
// super
219218
t.ExceptionHandler.ExceptionState(e)
220219
args := am.ParseArgs(e.Args)
@@ -1023,7 +1022,6 @@ func (t *Topic) PeerJoinedEnter(e *am.Event) bool {
10231022
}
10241023

10251024
func (t *Topic) PeerJoinedState(e *am.Event) {
1026-
10271025
t.Mach.EvRemove1(e, ss.PeerJoined, nil)
10281026
args := ParseArgs(e.Args)
10291027
peerId := args.PeerId
@@ -1928,7 +1926,6 @@ func (t *Topic) metric(msg, host string) bool {
19281926
metric := t.MachMetrics.Load()
19291927
if metric == nil {
19301928
return false
1931-
19321929
}
19331930

19341931
key := msg + host
@@ -1994,5 +1991,4 @@ func (t *Topic) syncMetrics() {
19941991
add("Peer")
19951992
}
19961993
}, nil)
1997-
19981994
}

tools/debugger/debugger.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1746,7 +1746,6 @@ func (d *Debugger) initGraphGen(
17461746
snapshot *graph.Graph, id string, details, clients int, outDir,
17471747
svgName string,
17481748
) []*amvis.Visualizer {
1749-
17501749
var vizs []*amvis.Visualizer
17511750

17521751
// render single (current one)
@@ -1850,8 +1849,8 @@ func (d *Debugger) syncOptsTimelines() {
18501849

18511850
func (d *Debugger) diagramsRender(
18521851
e *am.Event, shot *amgraph.Graph, id string, details, clients int,
1853-
outDir string, svgName string) {
1854-
1852+
outDir string, svgName string,
1853+
) {
18551854
ctx := d.Mach.NewStateCtx(ss.DiagramsRendering)
18561855
if ctx.Err() != nil {
18571856
return // expired
@@ -1907,7 +1906,6 @@ func (d *Debugger) diagramsMemCache(
19071906
e *am.Event, id string, cache *goquery.Document, tx *telemetry.DbgMsgTx,
19081907
outDir, svgName string,
19091908
) {
1910-
19111909
svgPath := path.Join(outDir, svgName+".svg")
19121910
ctx := d.Mach.NewStateCtx(ss.DiagramsRendering)
19131911
if ctx.Err() != nil {
@@ -1946,7 +1944,6 @@ func (d *Debugger) diagramsFileCache(
19461944
e *am.Event, id string, tx *telemetry.DbgMsgTx, lvl int, outDir,
19471945
svgName string,
19481946
) {
1949-
19501947
svgPath := path.Join(outDir, svgName+".svg")
19511948
ctx := d.Mach.NewStateCtx(ss.DiagramsRendering)
19521949
if ctx.Err() != nil {

tools/debugger/handlers.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,6 @@ func (d *Debugger) UpdateFocusState(e *am.Event) {
15831583

15841584
// unblock bc of locks
15851585
go func() {
1586-
15871586
// change focus (or not) when changing view types
15881587
switch d.Mach.Switch(ss.GroupFocused) {
15891588
case ss.ClientListFocused:

0 commit comments

Comments
 (0)