| 87 | ) |
| 88 | |
| 89 | func (this *daemon) loop() { |
| 90 | conn_in := make(chan net.Conn) |
| 91 | go func() { |
| 92 | for { |
| 93 | c, err := this.listener.Accept() |
| 94 | if err != nil { |
| 95 | panic(err) |
| 96 | } |
| 97 | conn_in <- c |
| 98 | } |
| 99 | }() |
| 100 | |
| 101 | timeout := time.Duration(g_config.CloseTimeout) * time.Second |
| 102 | countdown := time.NewTimer(timeout) |
| 103 | |
| 104 | for { |
| 105 | // handle connections or server CMDs (currently one CMD) |
| 106 | select { |
| 107 | case c := <-conn_in: |
| 108 | rpc.ServeConn(c) |
| 109 | countdown.Reset(timeout) |
| 110 | runtime.GC() |
| 111 | case cmd := <-this.cmd_in: |
| 112 | switch cmd { |
| 113 | case daemon_close: |
| 114 | return |
| 115 | } |
| 116 | case <-countdown.C: |
| 117 | return |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | func (this *daemon) close() { |
| 123 | this.cmd_in <- daemon_close |