-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
157 lines (126 loc) · 3.47 KB
/
main.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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/afex/hystrix-go/hystrix"
"github.com/gocql/gocql"
"github.com/gorilla/mux"
)
var Session *gocql.Session
func Connect() error {
var err error
if Session != nil {
Session.Close()
Session = nil
}
//cluster := gocql.NewCluster("_portname._tcp.cassandra-headless-svc.default.svc.cluster.local")
cluster := gocql.NewCluster("127.0.0.1")
cluster.Keyspace = "code2succeed"
Session, err = cluster.CreateSession()
return err
}
func init() {
hystrix.ConfigureCommand("my_command", hystrix.CommandConfig{
// How long to wait for command to complete, in milliseconds
Timeout: 5000,
// MaxConcurrent is how many commands of the same type
// can run at the same time
MaxConcurrentRequests: 10,
// VolumeThreshold is the minimum number of requests
// needed before a circuit can be tripped due to health
RequestVolumeThreshold: 10,
// SleepWindow is how long, in milliseconds,
// to wait after a circuit opens before testing for recovery
SleepWindow: 1000,
// ErrorPercentThreshold causes circuits to open once
// the rolling measure of errors exceeds this percent of requests
ErrorPercentThreshold: 50,
})
err := Connect()
if err != nil {
panic(err)
}
fmt.Println("cassandra init done")
}
type Emp struct {
Id string `json:"id,omitempty"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
Age int `json:"age,omitempty"`
}
func getEmps() []Emp {
fmt.Println("Getting all Employees")
var emps []Emp
m := map[string]interface{}{}
iter := Session.Query("SELECT * FROM emps").Iter()
for iter.MapScan(m) {
emps = append(emps, Emp{
Id: m["id"].(string),
FirstName: m["firstname"].(string),
LastName: m["lastname"].(string),
Age: m["age"].(int),
})
m = map[string]interface{}{}
}
return emps
}
func updateEmp(emp Emp) error {
fmt.Printf("Updating Emp with id = %s\n", emp.Id)
if err := Session.Query("UPDATE emps SET firstname = ?, lastname = ?, age = ? WHERE id = ?",
emp.FirstName, emp.LastName, emp.Age, emp.Id).Exec(); err != nil {
fmt.Println("Error while updating Emp")
fmt.Println(err)
return err
}
return nil
}
func GetEmps(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(getEmps())
}
func CreateEmp(w http.ResponseWriter, r *http.Request) {
var emp Emp
_ = json.NewDecoder(r.Body).Decode(&emp)
// Apply circuit breaker for db access
errChan := hystrix.Go("upsert_cassandra", func() error {
err := updateEmp(emp)
if err == gocql.ErrNoConnections {
//TODO: Retry logic
err = Connect()
if err != nil {
return err
}
err = updateEmp(emp) // retry 1
if err != nil {
return err
}
}
defer r.Body.Close()
return nil
}, func(err error) error {
json.NewEncoder(w).Encode("Cassandra unavailable")
return err
})
// Block until we have a result or an error.
select {
case err := <-errChan:
log.Println("failure:", err)
w.WriteHeader(http.StatusServiceUnavailable)
default:
json.NewEncoder(w).Encode(emp)
}
}
func main() {
router := mux.NewRouter()
router.HandleFunc("/emp", logger(GetEmps)).Methods("GET")
router.HandleFunc("/emp", logger(CreateEmp)).Methods("POST")
log.Fatal(http.ListenAndServe(":8080", router))
}
// log is Handler wrapper function for logging
func logger(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.Path, r.Method)
fn(w, r)
}
}