4444func main () {
4545 var (
4646 count = 250_000
47- testType = "runTest "
47+ testType = "runSchoolsTest "
4848 err error
4949 )
5050 if err = initializeCoherence (); err != nil {
@@ -63,7 +63,7 @@ func main() {
6363 if testType == "load" {
6464 err = populateSchoolsCache (count )
6565 } else {
66- err = runTest ()
66+ err = runSchoolsTest ()
6767 }
6868 if err != nil {
6969 log .Fatalf ("failed to run %s: %v" , testType , err )
@@ -90,7 +90,7 @@ func initializeCoherence() error {
9090 return err
9191}
9292
93- func runTest () error {
93+ func runSchoolsTest () error {
9494 http .HandleFunc ("/api/schools" , schoolsHandler )
9595
9696 fmt .Println ("Server running on port 8080" )
@@ -102,6 +102,16 @@ func runTest() error {
102102func schoolsHandler (w http.ResponseWriter , r * http.Request ) {
103103 ctx := context .Background ()
104104
105+ defer func () {
106+ if session .IsClosed () {
107+ log .Printf ("Session [%s] is closed, calling initializeCoherence()" , session .ID ())
108+ err := initializeCoherence ()
109+ if err != nil {
110+ log .Fatalf ("failed to initialize coherence: %v" , err )
111+ }
112+ }
113+ }()
114+
105115 switch r .Method {
106116
107117 case http .MethodGet :
@@ -117,14 +127,13 @@ func schoolsHandler(w http.ResponseWriter, r *http.Request) {
117127
118128 var people = make ([]School , 0 )
119129
120- if session .IsClosed () {
121- log .Printf ("Session [%s] is closed, calling initializeCoherence()" , session .ID ())
122- err := initializeCoherence ()
123- if err != nil {
124- log .Printf ("failed to initialize coherence: %v" , err )
125- http .Error (w , "not connected" , http .StatusInternalServerError )
126- return
127- }
130+ log .Println ("Health check via size()" )
131+ _ , err := schoolsCache .Size (ctx )
132+ if err != nil {
133+ session .Close ()
134+ log .Printf ("failed to do health check: %v" , err )
135+ http .Error (w , err .Error (), http .StatusInternalServerError )
136+ return
128137 }
129138
130139 for ch := range schoolsCache .ValuesFilter (ctx , marketSegmentFilter ) {
0 commit comments