Coder Social home page Coder Social logo

timer crash about clock HOT 13 CLOSED

alex023 avatar alex023 commented on July 17, 2024
timer crash

from clock.

Comments (13)

MarkLi2011 avatar MarkLi2011 commented on July 17, 2024

看到有新版本发布,已经更新到新版本0.7.2, 使用接口没有变吧?参考上次反馈的例子:https://github.com/alex023/clock/issues/2

from clock.

MarkLi2011 avatar MarkLi2011 commented on July 17, 2024

更新到新版本0.7.2版本还是会挂掉,日志如下:
panic: send on closed channel

goroutine 16 [running]:
gitlab/service/timer.(*jobItem).action(0xc4201fcee0, 0xc420073301)
/home/gofile/src/service/timer/job.go:56 +0x94
gitlab/service/timer.(*Clock).schedule(0xc4201a8140)
/home/gofile/src/service/timer/clock.go:135 +0x216
created by gitlab/service/timer.(*Clock).start
/home/gofile/src/service/timer/clock.go:86 +0xff

from clock.

alex023 avatar alex023 commented on July 17, 2024

@MarkLi2011
我初步估计找到了问题,但粘贴了你的代码,在macos和linux下执行,却没有报错。麻烦提供一下go version、os呢。

package main

import (
	"fmt"
	"sync"
	"time"
"github.com/alex023/clock"
)

// reference TTL Session example

// test code

// global clock
var globalClock = clock.NewClock()

// a session data
type sessionData struct {
	Job clock.Job
	ID  uint64
}

// session map
var sessionMap = struct {
	sync.RWMutex
	m map[uint64]interface{}
}{m: make(map[uint64]interface{})}

// add timer
func addTimer(timeout time.Duration, s *sessionData) {
	fmt.Printf("session[%v] addTimer ...\n", s.ID)
	s.Job, _ = globalClock.AddJobWithInterval(timeout, func() { OnTimeoutMsg(s.ID) })
}

// OnTimeoutMsg timeout message entry
func OnTimeoutMsg(id uint64) {
	fmt.Printf("session[%v] OnTimeoutMsg ...\n", id)

	deleteSession(id, true)
	// do something
}

func saveSession(id uint64, s *sessionData) {
	sessionMap.Lock()
	defer sessionMap.Unlock()

	sessionMap.m[id] = s
	fmt.Printf("session[%v] saveSession ...\n", id)
}

func deleteSession(id uint64, istimeout bool) {
	sessionMap.Lock()
	defer sessionMap.Unlock()

	fmt.Printf("session[%v] deleteSession ...\n", id)

	if false == istimeout {
		v, ok := sessionMap.m[id]
		if ok {
			s, _ := v.(*sessionData)
			s.Job.Cancel() // 超时任务执行前释放job资源
			fmt.Printf("session[%v] deleteSession release job resource ...\n", id)
		}
	} else {
		// 超时任务执行后不需要再调用Cancel释放job资源吧?
	}

	delete(sessionMap.m, id)
}

// 超时任务执行前,更新任务
func updateTimeout(id uint64) {
	sessionMap.Lock()
	defer sessionMap.Unlock()

	v, ok := sessionMap.m[id]
	if ok {
		s, _ := v.(*sessionData)
		globalClock.UpdateJobTimeout(s.Job, time.Second*40)
		fmt.Printf("session[%v] updateTimeout ...\n", id)
	}
}

// 超时任务执行前,撤销任务并释放资源
func jobBeforeProccess() {
	session1 := &sessionData{ID: 1}
	addTimer(time.Second*10, session1)
	saveSession(1, session1)

	session2 := &sessionData{ID: 2}
	addTimer(time.Second*20, session2)
	saveSession(2, session2)
	updateTimeout(2)

	session3 := &sessionData{ID: 3}
	addTimer(time.Second*30, session3)
	saveSession(3, session3)

	deleteSession(1, false)
	deleteSession(2, false)
	deleteSession(3, false)
}

// 常规超时任务执行时,释放资源
func jobNormalProccess() {
	session1 := &sessionData{ID: 1}
	addTimer(time.Second*10, session1)
	saveSession(1, session1)

	session2 := &sessionData{ID: 2}
	addTimer(time.Second*20, session2)
	saveSession(2, session2)
	updateTimeout(2)

	session3 := &sessionData{ID: 3}
	addTimer(time.Second*30, session3)
	saveSession(3, session3)

	waitCount := 1
	for {
		time.Sleep(time.Second * 1)
		waitCount++

		if waitCount > 50 {
			break
		}
	}
}

func main() {
	fmt.Println("Begin test jobBeforeProccess ...")
	jobBeforeProccess()
	fmt.Println("End test jobBeforeProccess ...")

	fmt.Println("Begin test jobNormalProccess ...")
	jobNormalProccess()
	fmt.Println("End test jobNormalProccess ...")

	fmt.Println("test over.")
}

from clock.

MarkLi2011 avatar MarkLi2011 commented on July 17, 2024

@alex023
[root@service]# go version
go version go1.8.3 linux/amd64

系统版本:CentOS7

from clock.

MarkLi2011 avatar MarkLi2011 commented on July 17, 2024

不是必出,程序运行后有时很久才挂,有时运行不久就挂

如果您找到了问题麻烦更新下,有点急用,感谢!!!

from clock.

MarkLi2011 avatar MarkLi2011 commented on July 17, 2024

我看代码中使用了timer.Stop(),不知道是否有这篇文章提到的问题:http://ju.outofmemory.cn/entry/111446

from clock.

alex023 avatar alex023 commented on July 17, 2024

问题在今天之前版本的example中能够重复出现,是关闭job后依然使用消息通道造成。你用新版看看,我将最近我们内部的反馈意见一并提交,做了个0.7.3。

你提供链接描述的是时钟竞争,这个直到golang1.9依然没有一个很好的封装。之前版本,我通过safetimer.go,应该已经解决。在测试(TestClock_Count ,10微秒)以及我们自己的业务(偶尔1微秒内)没有出现。

from clock.

MarkLi2011 avatar MarkLi2011 commented on July 17, 2024

@alex023 非常感谢您的即时更新!你也是重庆的?可否交个朋友?

from clock.

MarkLi2011 avatar MarkLi2011 commented on July 17, 2024

@alex023 更新到0.7.3还是有crash,有可能是我使用上的问题,我暂时改成golang自带的newtimer

go 1.9 环境编译

panic: send on closed channel

goroutine 20 [running]:
gitlab/service/timer.(*jobItem).action(0xc4201ee770, 0x1)
/home/gofile/src/service/timer/job.go:57 +0x88
gitlab/service/timer.(*Clock).schedule(0xc4200577c0)
/home/gofile/src/service/timer/clock.go:135 +0x251
created by gitlab/service/timer.(*Clock).start
/home/gofile/src/service/timer/clock.go:86 +0xfc

from clock.

alex023 avatar alex023 commented on July 17, 2024

@MarkLi2011
不涉及到提前撤销,用默认的没问题的。
如果能够提供问题出现的代码更好,先谢了。
如果不方便,加我QQ:12242284。我想了解一下具体场景

from clock.

MarkLi2011 avatar MarkLi2011 commented on July 17, 2024

@alex023 好的,我晚上加你

from clock.

alex023 avatar alex023 commented on July 17, 2024

问题是因为过期任务执行update造成,已修复!

from clock.

MarkLi2011 avatar MarkLi2011 commented on July 17, 2024

好的

from clock.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.