Queuing problem using localhost storing lookup table?


(Sibert) #1

I am trying to create a lookup table of sql queries using Go and Postgresql. This is an unorthodox way to solve the problem with many queries, so it might be the wrong direction.

The code below works as expected for a few calling computers, but as every call ends up in ONE single session in the lookup database, I am wondering if this may cause future problem with queuing the lookups.

This is a followup question from REST API with 1000 query templates

I have setup a test server for only two lookups:

http://94.237.25.207:8099/co
http://94.237.25.207:8099/pe

package main

import (
    "fmt"
    "log"
    "strings"
    "net/http"
    "database/sql"
    _ "github.com/lib/pq"
)

var db *sql.DB
var err error

func main() {
    http.HandleFunc("/", Query)
    Connect()
    log.Fatal(http.ListenAndServe(":8080", nil))
}

func Connect() {
  const (
     host = "127.0.0.1"
     port = 5432
     user = "go"
     password = "test"
     dbname  = "lookup")

  login := fmt.Sprintf("host=%s port=%d user=%s "+
    "password=%s dbname=%s sslmode=disable",
    host, port, user, password, dbname)

  var err error
    db, err = sql.Open("postgres", login)
    
    if err != nil {
      log.Fatalln(err)
    }
    
    err = db.Ping()
  
    if err != nil {
      panic(err)
  }
}

func Query (w http.ResponseWriter, r *http.Request){
  id := strings.Replace(r.URL.String(), "/", "", -1) //remove slashes
  values := []string{"SELECT sqlx FROM get WHERE id =","'",id,"'"}
  query := strings.Join(values, "")
  
  var sqlx string
  var row *sql.Row

  row = db.QueryRow(query)
  err := row.Scan(&sqlx)

  if err != nil && err != sql.ErrNoRows {
    log.Fatalln(err)
  }

  fmt.Fprintf(w,"%s", sqlx)

}