-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathserviceq.go
90 lines (76 loc) · 2.41 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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"context"
"net"
"os"
"os/signal"
"syscall"
"github.com/gptankit/serviceq/errorlog"
"github.com/gptankit/serviceq/model"
"github.com/gptankit/serviceq/properties"
"github.com/gptankit/serviceq/protocol/httpservice"
)
// main sets up serviceq properties, initializes work done and request buffers,
// and starts routines to accept new tcp connections and observe buffered requests
func main() {
ctx := context.Background()
stopCtx, stop := signal.NotifyContext(ctx, os.Interrupt, syscall.SIGTERM)
defer stop()
if sqp, err := properties.New(properties.GetFilePath()); err == nil {
if ln, err := newListener(sqp); err == nil {
defer closeListener(ln)
cwork := make(chan int, sqp.MaxConcurrency+1) // work done queue
creq := make(chan interface{}, sqp.MaxConcurrency) // request queue
// observe buffered requests
go workBackground(stopCtx, creq, cwork, sqp)
// accept new connections
listenActive(stopCtx, ln, creq, cwork, sqp)
} else {
go errorlog.LogGenericError("Could not listen on :" + sqp.ListenerPort + " -- " + err.Error())
}
} else {
go errorlog.LogGenericError("Could not read sq.properties, closing listener -- " + err.Error())
}
}
// listenActive forwards new requests to the cluster
func listenActive(ctx context.Context, ln *net.Listener, creq chan interface{}, cwork chan int, sqp *model.ServiceQProperties) {
shutListener := func(ln *net.Listener) {
<-ctx.Done()
closeListener(ln)
}
go shutListener(ln)
for {
if conn, err := (*ln).Accept(); err == nil {
if len(cwork) < cap(cwork)-1 {
switch sqp.Proto {
case "http":
if httpSrv := httpservice.New(sqp, httpservice.WithIncomingTCPConn(&conn)); httpSrv != nil {
go httpSrv.ExecuteRealTime(ctx, creq, cwork)
}
default:
conn.Close()
}
} else {
if httpSrv := httpservice.New(sqp, httpservice.WithIncomingTCPConn(&conn)); httpSrv != nil {
go httpSrv.Discard(ctx)
}
}
} else {
// handle permanent accept error including closed listener
if ae, ok := err.(net.Error); ok && !ae.Temporary() {
break
}
}
}
}
// workBackground forwards buffered requests to the cluster
func workBackground(ctx context.Context, creq chan interface{}, cwork chan int, sqp *model.ServiceQProperties) {
switch sqp.Proto {
case "http":
if httpSrv := httpservice.New(sqp); httpSrv != nil {
go httpSrv.ExecuteBuffered(ctx, creq, cwork)
}
default:
break
}
}