Home > other >  Go count dynamic url calls
Go count dynamic url calls

Time:01-22

I am trying to count each new dynamic URL

var count int

// *Error* non-declaration statement outside function body
func increment() error {
    count = count   1
    return nil
}

func helloHandler(w http.ResponseWriter, r *http.Request) {
    m := make(map[string]int)
    if r.Method != "POST" {
        http.Error(w, "Method is not supported.", http.StatusNotFound)
        return
    }
    increment()
    b, err := ioutil.ReadAll(r.Body)

    if err != nil {
        panic(err)
    }

    urlPath := r.RequestURI
    value, ok := m[urlPath]
    if ok {
        m[urlPath] = count   1
        fmt.Println("value: ", value)
    } else {
        m[urlPath] = count
        fmt.Println(m)
        fmt.Println("key not found")
    }
    fmt.Println(m)

    fmt.Fprintf(w, "Hello!", count)
    fmt.Printf("%s", b)


}

func main() {
    http.HandleFunc("/report/", helloHandler) // Update this line of code

    fmt.Printf("Starting server at port 8080\n")
    if err := http.ListenAndServe(":8080", nil); err != nil {
        log.Fatal(err)
    }
}

the result should be map with all URLs as a key and number of times as value such: {"abc" : 2 "foo" : 1 "ho": 5} but when i run my code evry time the key updated again an agin

CodePudding user response:

There are multiple problems:

  • You create a new map on each execution of your handler function.

  • As HTTP handlers execute on separate goroutines which are running concurrently, you have a data race on the global counter variable.

I'm not really sure I have correctly parsed what you're after, but supposedly you should:

  • Have a global map.
  • Have each access to that map protected by a mutex.
  • Increment counters which are values in the map.

Something like this:

var (
  hitsMu sync.Mutex
  hits = make(map[string]*int)
)

func helloHandler(w http.ResponseWriter, r *http.Request) {
  // ...

  hitsMu.Lock()
  defer hitsMu.Unlock()

  counterPtr := hits[urlPath]
  if counterPtr == nil {
    counterPtr = new(int)
    hits[urlPath] = counterPtr
  }
  *counterPtr  = 1

  // ...
}
  •  Tags:  
  • Related