/
work.go
48 lines (41 loc) · 897 Bytes
/
work.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package main
import (
"fmt"
"code.google.com/p/go.net/context"
"github.com/golang/glog"
)
func Work(ctx context.Context, errc chan<- error, sysfd int, reqc <-chan *Request, inreqc <-chan *Request, rspc chan<- *Response) {
go func() {
defer func() {
glog.Infof("Work is stoped")
}()
for {
var rsp *Response
var err error
select {
case <-ctx.Done():
glog.Infof("Done for Work")
return
case req := <-reqc:
if rsp, err = work(req); err != nil {
errc <- fmt.Errorf("in Work req channel: %s", err)
return
}
rspc <- rsp
case req := <-inreqc:
var rsp *Response
if rsp, err = work(req); err != nil {
errc <- fmt.Errorf("in Work inner req channel: %s", err)
return
}
rspc <- rsp
}
}
}()
}
func work(req *Request) (rsp *Response, err error) {
return &Response{
Head: req.Head,
Body: req.Body,
}, nil
}