global bigtable connection for functions

Change-Id: Ib61bd2e9f4a19e1f8405491c572ef6b36c54142e
This commit is contained in:
justinschuldt 2021-10-29 02:56:02 -05:00 committed by Justin Schuldt
parent f59f4bbb2e
commit 6fb8cdfd01
5 changed files with 24 additions and 67 deletions

View File

@ -2,17 +2,13 @@
package p
import (
"context"
"encoding/json"
"fmt"
"html"
"io"
"log"
"net/http"
"os"
"strings"
"cloud.google.com/go/bigtable"
)
// fetch a single row by the row key
@ -109,20 +105,6 @@ func ReadRow(w http.ResponseWriter, r *http.Request) {
}
rowKey = emitterChain + ":" + emitterAddress + ":" + sequence
clientOnce.Do(func() {
// Declare a separate err variable to avoid shadowing client.
var err error
project := os.Getenv("GCP_PROJECT")
instance := os.Getenv("BIGTABLE_INSTANCE")
client, err = bigtable.NewClient(context.Background(), project, instance)
if err != nil {
http.Error(w, "Error initializing client", http.StatusInternalServerError)
log.Printf("bigtable.NewClient: %v", err)
return
}
})
tbl := client.Open("v2Events")
row, err := tbl.ReadRow(r.Context(), rowKey)
if err != nil {
http.Error(w, "Error reading rows", http.StatusInternalServerError)

View File

@ -9,7 +9,6 @@ import (
"io"
"log"
"net/http"
"os"
"sort"
"strconv"
"strings"
@ -164,21 +163,6 @@ func Recent(w http.ResponseWriter, r *http.Request) {
}
}
// create bibtable client and open table
clientOnce.Do(func() {
// Declare a separate err variable to avoid shadowing client.
var err error
project := os.Getenv("GCP_PROJECT")
instance := os.Getenv("BIGTABLE_INSTANCE")
client, err = bigtable.NewClient(context.Background(), project, instance)
if err != nil {
http.Error(w, "Error initializing client", http.StatusInternalServerError)
log.Printf("bigtable.NewClient: %v", err)
return
}
})
tbl := client.Open("v2Events")
// use the groupBy value to determine how many segements of the rowkey should be used for indexing results.
keySegments := 0
if groupBy == "chain" {

View File

@ -1,7 +1,10 @@
package p
import (
"context"
"log"
"net/http"
"os"
"strings"
"sync"
@ -14,6 +17,27 @@ import (
// every request.
var client *bigtable.Client
var clientOnce sync.Once
var tbl *bigtable.Table
// init runs during cloud function initialization. So, this will only run during an
// an instance's cold start.
// https://cloud.google.com/functions/docs/bestpractices/networking#accessing_google_apis
func init() {
clientOnce.Do(func() {
// Declare a separate err variable to avoid shadowing client.
var err error
project := os.Getenv("GCP_PROJECT")
instance := os.Getenv("BIGTABLE_INSTANCE")
client, err = bigtable.NewClient(context.Background(), project, instance)
if err != nil {
// http.Error(w, "Error initializing client", http.StatusInternalServerError)
log.Printf("bigtable.NewClient error: %v", err)
return
}
})
tbl = client.Open("v2Events")
}
var columnFamilies = []string{"MessagePublication", "Signatures", "VAAState", "QuorumState"}

View File

@ -9,7 +9,6 @@ import (
"io"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"
@ -235,21 +234,6 @@ func Totals(w http.ResponseWriter, r *http.Request) {
}
}
// create bibtable client and open table
clientOnce.Do(func() {
// Declare a separate err variable to avoid shadowing client.
var err error
project := os.Getenv("GCP_PROJECT")
instance := os.Getenv("BIGTABLE_INSTANCE")
client, err = bigtable.NewClient(context.Background(), project, instance)
if err != nil {
http.Error(w, "Error initializing client", http.StatusInternalServerError)
log.Printf("bigtable.NewClient: %v", err)
return
}
})
tbl := client.Open("v2Events")
// create the rowkey prefix for querying
prefix := ""
if forChain != "" {

View File

@ -2,14 +2,12 @@
package p
import (
"context"
"encoding/json"
"fmt"
"html"
"io"
"log"
"net/http"
"os"
"cloud.google.com/go/bigtable"
)
@ -75,21 +73,6 @@ func Transaction(w http.ResponseWriter, r *http.Request) {
http.Error(w, http.StatusText(http.StatusBadRequest), http.StatusBadRequest)
}
// create bibtable client and open table
clientOnce.Do(func() {
// Declare a separate err variable to avoid shadowing client.
var err error
project := os.Getenv("GCP_PROJECT")
instance := os.Getenv("BIGTABLE_INSTANCE")
client, err = bigtable.NewClient(context.Background(), project, instance)
if err != nil {
http.Error(w, "Error initializing client", http.StatusInternalServerError)
log.Printf("bigtable.NewClient: %v", err)
return
}
})
tbl := client.Open("v2Events")
var result bigtable.Row
readErr := tbl.ReadRows(r.Context(), bigtable.PrefixRange(""), func(row bigtable.Row) bool {