forked from gptankit/serviceq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserviceq.go
65 lines (53 loc) · 1.51 KB
/
serviceq.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package main
import (
"fmt"
"model"
"net"
"os"
"protocol"
"time"
)
func main() {
if sqp, err := getProperties(getPropertyFilePath()); err == nil {
if listener, err := getListener(sqp); err == nil {
defer listener.Close()
cwork := make(chan int, sqp.MaxConcurrency+1) // work done queue
creq := make(chan interface{}, sqp.MaxConcurrency) // request queue
// observe bufferred requests
go workBackground(creq, cwork, &sqp)
// accept new connections
listenActive(listener, creq, cwork, &sqp)
} else {
fmt.Fprintf(os.Stderr, "Could not listen on :"+sqp.ListenerPort+" -- %s\n", err.Error())
}
} else {
fmt.Fprintf(os.Stderr, "Could not read sq.properties, closing listener -- %s\n", err.Error())
}
}
func listenActive(listener net.Listener, creq chan interface{}, cwork chan int, sqp *model.ServiceQProperties) {
for {
if conn, err := listener.Accept(); err == nil {
if len(cwork) < cap(cwork)-1 {
if (*sqp).Proto == "http" {
go protocol.HandleHttpConnection(&conn, creq, cwork, sqp)
} else {
<-cwork
conn.Close()
}
} else {
go protocol.DiscardHttpConnection(&conn, sqp)
}
}
}
}
func workBackground(creq chan interface{}, cwork chan int, sqp *model.ServiceQProperties) {
for {
if len(cwork) > 0 && len(creq) > 0 {
if (*sqp).Proto == "http" {
go protocol.HandleHttpBufferedReader((<-creq).(model.RequestParam), creq, cwork, sqp)
}
} else {
time.Sleep(time.Duration((*sqp).IdleGap) * time.Millisecond) // wait for more work
}
}
}