-
Notifications
You must be signed in to change notification settings - Fork 7
/
athena.go
169 lines (140 loc) · 3.41 KB
/
athena.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
package httpToAthena
import (
"context"
"io/ioutil"
"net/http"
"strconv"
"strings"
"sync"
"time"
"github.com/gallir/smart-relayer/httpTo/httpToAthena/ifaceAthena"
"github.com/gallir/smart-relayer/lib"
"github.com/gin-gonic/gin"
)
// Server is the thread that listen for clients' connections
type Server struct {
sync.Mutex
config lib.RelayerConfig
done chan bool
exiting bool
engine *gin.Engine
iface *ifaceAthena.Athena
lastConnection time.Time
lastError time.Time
}
var (
defaultTimeout = 5 * time.Second
defaultMaxResults int64 = 100
)
// New creates a new Redis local server
func New(c lib.RelayerConfig, done chan bool) (*Server, error) {
srv := &Server{
done: done,
}
srv.Reload(&c)
return srv, nil
}
// Reload the configuration
func (srv *Server) Reload(c *lib.RelayerConfig) (err error) {
srv.Lock()
defer srv.Unlock()
srv.config = *c
at, err := ifaceAthena.New(ifaceAthena.Config{
Region: c.Region,
Profile: c.Profile,
OutputLocation: c.S3Bucket,
})
if err != nil {
return err
}
srv.iface = at
return nil
}
// Start accepts incoming connections on the Listener
func (srv *Server) Start() (e error) {
if srv.engine != nil {
return nil
}
gin.SetMode(gin.ReleaseMode)
srv.engine = gin.New()
srv.engine.POST("/:database", srv.query)
srv.engine.GET("/:jobId", srv.get)
go func() {
switch {
case strings.HasPrefix(srv.config.Listen, "tcp://"):
e = srv.engine.Run(srv.config.Listen[6:])
case strings.HasPrefix(srv.config.Listen, "unix://"):
e = srv.engine.RunUnix(srv.config.Listen[7:])
}
}()
return e
}
// Exit closes the listener and send done to main
func (srv *Server) Exit() {
srv.exiting = true
if srv.engine != nil {
// finish gin
}
// finishing the server
srv.done <- true
}
func (srv *Server) query(ctx *gin.Context) {
b, err := ioutil.ReadAll(ctx.Request.Body)
if err != nil {
ctx.String(http.StatusBadRequest, err.Error())
return
}
r, err := srv.iface.Query(ctx.Param("database"), string(b))
if err != nil {
ctx.IndentedJSON(http.StatusBadRequest, map[string]interface{}{
"Error": err.Error(),
})
return
}
ctx.IndentedJSON(http.StatusOK, map[string]interface{}{
"jobId": r,
})
}
func (srv *Server) get(ctx *gin.Context) {
nextToken := ctx.GetHeader("X-NextToken")
timeOut, err := time.ParseDuration(ctx.GetHeader("X-Timeout"))
if err != nil || timeOut == 0 {
timeOut = defaultTimeout
}
maxResults, err := strconv.ParseInt(ctx.GetHeader("X-MaxResults"), 10, 64)
if err != nil || maxResults == 0 {
maxResults = defaultMaxResults
}
// we need to add one as the first row of every result set is the header row
// and that is removed by the code
maxResults += 1
ctxTimeout, cancel := context.WithTimeout(context.Background(), timeOut)
defer cancel()
tick := time.NewTicker(1 * time.Second)
defer tick.Stop()
for {
select {
case <-tick.C:
r, nextToken, err := srv.iface.Read(ctx.Param("jobId"), nextToken, maxResults)
if err != nil {
if err == ifaceAthena.ErrPending {
continue
}
ctx.IndentedJSON(http.StatusBadRequest, map[string]interface{}{
"Error": err.Error(),
})
return
}
ctx.IndentedJSON(http.StatusOK, map[string]interface{}{
"nextToken": nextToken,
"rows": r,
})
return
case <-ctxTimeout.Done():
ctx.IndentedJSON(http.StatusCreated, map[string]interface{}{
"Error": ctxTimeout.Err().Error(),
})
return
}
}
}