Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: timer reset #51

Merged
merged 9 commits into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: golangci-lint
uses: golangci/golangci-lint-action@v2
with:
version: v1.41
version: v1.46.0
args: --timeout=10m --skip-dirs=fix42,fix44 --exclude="Error return value of .(store.cache.*|suite.msgStore.*). is not checke"

build:
Expand Down
22 changes: 22 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
service:
golangci-lint-version: 1.46.0
run:
deadline: 30m
skip-dirs:
- cover
- fix42
- fix44
modules-download-mode: readonly

issues:
exclude-use-default: false
exclude-rules:
- linters:
- errcheck
text: " is not checke"

linters:
enable:
- exportloopref
disable:
- structcheck
3 changes: 1 addition & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ vet:
go vet ./_test

lint:
go get golang.org/x/lint/golint
golint .
golangci-lint run

test:
go test -v -cover -p=1 -count=1 . ./datadictionary ./internal
Expand Down
2 changes: 2 additions & 0 deletions in_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ func (state inSession) Timeout(session *session, event internal.Event) (nextStat
session.log.OnEvent("Sent test request TEST")
session.peerTimer.Reset(time.Duration(float64(1.2) * float64(session.HeartBtInt)))
return pendingTimeout{state}
default:
session.log.OnEventf("receive event: %s, %v", state.String(), event)
}

return state
Expand Down
17 changes: 16 additions & 1 deletion internal/event_timer.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,15 @@ type EventTimer struct {
timer *time.Timer
done chan struct{}
wg sync.WaitGroup
rst chan time.Duration
}

func NewEventTimer(task func()) *EventTimer {
t := &EventTimer{
f: task,
timer: newStoppedTimer(),
done: make(chan struct{}),
rst: make(chan time.Duration),
}

t.wg.Add(1)
Expand All @@ -33,6 +35,14 @@ func NewEventTimer(task func()) *EventTimer {
t.timer.Stop()
return

case rstTime := <-t.rst:
if !t.timer.Stop() {
select { // cleanup
case <-t.timer.C:
default:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

default はあまり意味はないですか?
if !..Stop() 句で入っている時点で必ず timer.C から読み出せる?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

関連するIssue等を読み直してみましたが、確かに Stop()==false の時点で(かつTimerの受信側のgoroutineのselectで読み出しする場合)必ず読み出せるようです。(ややこしい、、)
golang/go#27169

コメントを残して不要な処理を削除するか、分かりやすさを重視してこのままで行くか、どちらがいいでしょう?(迷い中)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

このままでいいかと思います。

select ... default 句は golang のイディオム的な意味合いも強そうなので、意図がわからないということもなさそうですので。

}
}
t.timer.Reset(rstTime)
}
}
}()
Expand All @@ -54,7 +64,12 @@ func (t *EventTimer) Reset(timeout time.Duration) {
return
}

t.timer.Reset(timeout)
go func() {
select {
case <-t.done:
case t.rst <- timeout:
}
}()
}

func newStoppedTimer() *time.Timer {
Expand Down
3 changes: 2 additions & 1 deletion latent_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ func (state latentState) FixMsgIn(session *session, msg *Message) (nextState ses
return state
}

func (state latentState) Timeout(*session, internal.Event) (nextState sessionState) {
func (state latentState) Timeout(session *session, event internal.Event) (nextState sessionState) {
session.log.OnEventf("receive event: %s, %v", state.String(), event)
return state
}

Expand Down
3 changes: 2 additions & 1 deletion not_session_time.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ func (state notSessionTime) FixMsgIn(session *session, msg *Message) (nextState
return state
}

func (state notSessionTime) Timeout(*session, internal.Event) (nextState sessionState) {
func (state notSessionTime) Timeout(session *session, event internal.Event) (nextState sessionState) {
session.log.OnEventf("receive event: %s, %v", state.String(), event)
return state
}

Expand Down
2 changes: 2 additions & 0 deletions pending_timeout.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ func (s pendingTimeout) Timeout(session *session, event internal.Event) (nextSta
case internal.PeerTimeout:
session.log.OnEvent("Session Timeout")
return latentState{}
default:
session.log.OnEventf("receive event: %s, %v", s.String(), event)
}

return s
Expand Down
5 changes: 5 additions & 0 deletions session.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,11 @@ type session struct {
hasStopByDisconnect bool
}

func (s *session) SetLog(log Log) {
s.log = log
s.stateMachine.log = &s.log
}

func (s *session) logError(err error) {
s.log.OnEvent(err.Error())
}
Expand Down
5 changes: 3 additions & 2 deletions session_factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,11 @@ func (f sessionFactory) newSession(
} else if err = f.buildAcceptorSettings(s, settings); err != nil {
return
}

if s.log, err = logFactory.CreateSessionLog(s.sessionID); err != nil {
log, err := logFactory.CreateSessionLog(s.sessionID)
if err != nil {
return
}
s.SetLog(log)

if s.store, err = storeFactory.Create(s.sessionID); err != nil {
return
Expand Down
4 changes: 4 additions & 0 deletions session_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type stateMachine struct {
State sessionState
pendingStop, stopped bool
notifyOnInSessionTime chan interface{}
log *Log
}

func (sm *stateMachine) Start(s *session) {
Expand Down Expand Up @@ -152,6 +153,9 @@ func (sm *stateMachine) setState(session *session, nextState sessionState) {
}
}

if sm.State.String() != nextState.String() && sm.log != nil {
(*sm.log).OnEventf("change state: %s -> %s", sm.State.String(), nextState.String())
}
sm.State = nextState
}

Expand Down