fixed a infinite loop

This commit is contained in:
mikespook 2012-05-28 10:34:16 +08:00
parent 0d6d1e114f
commit 842edf79ba
4 changed files with 13 additions and 3 deletions

View File

@ -23,6 +23,7 @@ var (
ErrNotConn = errors.New("Did not connect to job server.") ErrNotConn = errors.New("Did not connect to job server.")
ErrFuncNotFound = errors.New("The function was not found.") ErrFuncNotFound = errors.New("The function was not found.")
ErrEmptyReading = errors.New("Empty reading.") ErrEmptyReading = errors.New("Empty reading.")
ErrNoActiveAgent = errors.New("No active agent.")
) )
func DisablePanic() {recover()} func DisablePanic() {recover()}

View File

@ -19,6 +19,7 @@ func main() {
log.Println("Starting ...") log.Println("Starting ...")
defer log.Println("Shutdown complete!") defer log.Println("Shutdown complete!")
w := worker.New(worker.Unlimited) w := worker.New(worker.Unlimited)
defer w.Close()
w.ErrHandler = func(e error) { w.ErrHandler = func(e error) {
log.Println(e) log.Println(e)
} }

View File

@ -48,7 +48,7 @@ func (a *agent) outLoop() {
// inputing loop // inputing loop
func (a *agent) inLoop() { func (a *agent) inLoop() {
defer func() { defer func() {
a.conn.Close() recover()
close(a.in) close(a.in)
close(a.out) close(a.out)
a.worker.removeAgent(a) a.worker.removeAgent(a)
@ -85,6 +85,10 @@ func (a *agent) inLoop() {
} }
} }
func (a *agent) Close() {
a.conn.Close()
}
func (a *agent) Work() { func (a *agent) Work() {
go a.outLoop() go a.outLoop()
go a.inLoop() go a.inLoop()

View File

@ -159,6 +159,9 @@ func (worker *Worker) removeFunc(funcname string) {
func (worker *Worker) Work() { func (worker *Worker) Work() {
defer func() { defer func() {
worker.running = false worker.running = false
for _, v := range worker.agents {
v.Close()
}
}() }()
for funcname, f := range worker.funcs { for funcname, f := range worker.funcs {
worker.addFunc(funcname, f.timeout) worker.addFunc(funcname, f.timeout)
@ -168,8 +171,9 @@ func (worker *Worker) Work() {
go v.Work() go v.Work()
} }
ok := true ok := true
var job *Job
for ok { for ok {
if job, ok := <-worker.in; ok { if job, ok = <-worker.in; ok {
switch job.DataType { switch job.DataType {
case common.ERROR: case common.ERROR:
go func() { go func() {
@ -280,6 +284,6 @@ func (worker *Worker) removeAgent(a *agent) {
} }
} }
if len(worker.agents) == 0 { if len(worker.agents) == 0 {
worker.Close() worker.err(common.ErrNoActiveAgent)
} }
} }