forked from snowflakedb/gosnowflake
/
heartbeat.go
112 lines (102 loc) · 2.69 KB
/
heartbeat.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
// Copyright (c) 2018 Snowflake Computing Inc. All right reserved.
package gosnowflake
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"time"
"github.com/google/uuid"
)
const (
// One hour interval should be good enough to renew tokens for four hours master token validity
heartBeatInterval = 60 * time.Minute
)
type heartbeat struct {
restful *snowflakeRestful
ShutdownChan chan int
}
func (hc *heartbeat) run() {
hbTimer := time.NewTimer(heartBeatInterval)
for {
go func() {
<-hbTimer.C
err := hc.heartbeatMain()
if err != nil {
glog.V(2).Info("failed to heartbeat")
return
}
hbTimer = time.NewTimer(heartBeatInterval)
}()
select {
case <-hc.ShutdownChan:
// stop
hbTimer.Stop()
glog.V(2).Info("stopping")
return
default:
// no more download
glog.V(2).Info("no shutdown signal")
}
time.Sleep(1 * time.Second)
}
}
func (hc *heartbeat) start() {
hc.ShutdownChan = make(chan int)
go hc.run()
glog.V(2).Info("heartbeat started")
}
func (hc *heartbeat) stop() {
hc.ShutdownChan <- 1
close(hc.ShutdownChan)
glog.V(2).Info("heartbeat stopped")
}
func (hc *heartbeat) heartbeatMain() error {
glog.V(2).Info("Heartbeating!")
params := &url.Values{}
params.Add("requestId", uuid.New().String())
fullURL := fmt.Sprintf(
"%s://%s:%d%s", hc.restful.Protocol, hc.restful.Host, hc.restful.Port, "/session/heartbeat?"+params.Encode())
headers := make(map[string]string)
headers["Content-Type"] = headerContentTypeApplicationJSON
headers["accept"] = headerAcceptTypeApplicationSnowflake
headers["User-Agent"] = userAgent
headers[headerAuthorizationKey] = fmt.Sprintf(headerSnowflakeToken, hc.restful.Token)
resp, err := hc.restful.FuncPost(context.TODO(), hc.restful, fullURL, headers, nil, hc.restful.RequestTimeout, false)
if err != nil {
return err
}
defer resp.Body.Close()
if resp.StatusCode == http.StatusOK {
glog.V(2).Infof("heartbeatMain: resp: %v", resp)
var respd execResponse
err = json.NewDecoder(resp.Body).Decode(&respd)
if err != nil {
glog.V(1).Infof("failed to decode JSON. err: %v", err)
glog.Flush()
return err
}
if respd.Code == sessionExpiredCode {
err = hc.restful.FuncRenewSession(context.TODO(), hc.restful)
if err != nil {
return err
}
}
return nil
}
b, err := ioutil.ReadAll(resp.Body)
if err != nil {
glog.V(1).Infof("failed to extract HTTP response body. err: %v", err)
return err
}
glog.V(1).Infof("HTTP: %v, URL: %v, Body: %v", resp.StatusCode, fullURL, b)
glog.V(1).Infof("Header: %v", resp.Header)
glog.Flush()
return &SnowflakeError{
Number: ErrFailedToHeartbeat,
SQLState: SQLStateConnectionFailure,
Message: "Failed to heartbeat.",
}
}