/
app.go
64 lines (55 loc) · 1.48 KB
/
app.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
package queryport
import (
c "github.com/couchbase/indexing/secondary/common"
"github.com/couchbase/indexing/secondary/logging"
"net"
protobuf "github.com/couchbase/indexing/secondary/protobuf/query"
)
// Application is example application logic that uses query-port server
func Application(config c.Config) {
killch := make(chan bool)
s, err := NewServer(
"",
"localhost:9990",
func(req interface{}, ctx interface{},
conn net.Conn, quitch <-chan bool, clientVersion uint32) {
requestHandler(req, conn, quitch, killch)
},
nil,
config)
if err != nil {
logging.Fatalf("Listen failed - %v", err)
}
<-killch
s.Close()
}
// will be spawned as a go-routine by server's connection handler.
func requestHandler(
req interface{},
conn net.Conn, // Write handle to the tcp socket
quitch <-chan bool, // client / connection might have quit (done)
killch chan bool, // application is shutting down the server.
) {
var responses []*protobuf.ResponseStream
switch req.(type) {
case *protobuf.StatisticsRequest:
// responses = getStatistics()
case *protobuf.ScanRequest:
// responses = scanIndex()
case *protobuf.ScanAllRequest:
// responses = fullTableScan()
}
buf := make([]byte, 1024, 1024)
loop:
for _, resp := range responses {
// query storage backend for request
protobuf.EncodeAndWrite(conn, buf, resp)
select {
case <-quitch:
close(killch)
break loop
}
}
protobuf.EncodeAndWrite(conn, buf, &protobuf.StreamEndResponse{})
// Free resources.
}