Working out the number of times a (request handler) function has been called in Go - http

Context
I'm making a web app that serves dynamically generated pdfs. These contain content from the internet, so every time it serves a pdf, it downloads a number of files to a new temporary folder.
The Problem
I end up with a large number of folders after I load the page once, so it seems that, for some reason, the handler is being called multiple times, which is an issue because I'm downloading multiple times more than I need to of not insubstantial files. I'd like to check at what stage of the process multiple requests are occurring.
The Question
Is there a way of working out how many times a function has been called, quite possibly using closures? (I haven't quite got closures into my mental model for programming yet; I don't completely understand them/how they're used).
This would preferably be something involving an int in the language rather than printing something at every stage and counting by hand - I'm looking for a more scalable solution than that (for later situations as well as this one).
Thanks!

Here are two ways you can count function calls, and one for method calls. There are plenty of other ways too, but just to get you started:
Using closure: (not what I would recommended)
package main
import(
"fmt"
"sync/atomic"
)
var Foo = func() (func() uint64) {
var called uint64
return func() uint64 {
atomic.AddUint64(&called, 1)
fmt.Println("Foo!")
return called
}
}()
func main() {
Foo()
c := Foo()
fmt.Printf("Foo() is called %d times\n", c)
}
Playground: http://play.golang.org/p/euKbamdI7h
Using global counter:
package main
import (
"fmt"
"sync/atomic"
)
var called uint64
func Foo() {
atomic.AddUint64(&called, 1)
fmt.Println("Foo!");
}
func main() {
Foo()
Foo()
fmt.Printf("Foo() is called %d times\n", called)
}
Playground: http://play.golang.org/p/3Ib29VCnoF
Counting method calls:
package main
import (
"fmt"
"sync/atomic"
)
type T struct {
Called uint64
}
func (t *T) Foo() {
atomic.AddUint64(&t.Called, 1)
fmt.Println("Foo!")
}
func main() {
var obj T
obj.Foo()
obj.Foo()
fmt.Printf("obj.Foo() is called %d times\n", obj.Called)
}
Playground: http://play.golang.org/p/59eOQdUQU1
Edit:
I just realized that the handler might not be in your own package. In such a case, you might want to write a wrapper:
var called uint64
func Foo() {
atomic.AddUint64(&called, 1)
importedPackage.Foo()
}
Edit 2:
Updated the examples to use atomic +1 operations.

Counting Calls
To answer the specific question you asked, here is one quick way to count handler executions:
func countCalls(h http.HandlerFunc) http.HandlerFunc {
var lock sync.Mutex
var count int
return func(w http.ResponseWriter, r *http.Request) {
lock.Lock()
count++
w.Header().Set("X-Call-Count", fmt.Sprintf("%d", count))
lock.Unlock()
h.ServeHTTP(w, r)
}
}
http.Handle("/foobar", countCalls(foobarHandler))
This will add a header that you can inspect with your favorite web developer tools; you could also just log it to standard output or something.
Logging Handlers
To expand upon the answers mentioned above, what you probably want to do to debug this and have in place for future use is to log details of each request.
package main
import (
"flag"
"log"
"net/http"
"os"
"github.com/gorilla/handlers"
)
var (
accessLogFile = flag.String("log", "/var/log/yourapp/access.log", "Access log file")
)
func main() {
accessLog, err := os.OpenFile(*accessLogFile, os.O_CREATE|os.O_WRITE|os.O_APPEND, 0644)
if err != nil {
log.Fatalf("Failed to open access log: %s", err)
}
wrap := func(f http.HandlerFunc) http.Handler {
return handlers.LoggingHandler(accessLog, http.HandlerFunc(foobarHandler))
}
http.Handle("/foobar", wrap(foobarHandler))
...
}
This uses LoggingHandler (or CombinedLoggingHandler) to write a standard Apache format log message that you can either inspect yourself or analyze with various tools.
An example of a log line would be
127.0.0.1 - frank [10/Oct/2000:13:55:36 -0700] "GET /apache_pb.gif HTTP/1.0" 200 2326
which tells you who made the request, when, what the method and URL was, how your server responded, and how long the response was. From this log, you should be able to see exactly what requests are being made, to determine not only how many times your handlers are being called, but exactly what is generating the requests and whether they're to another endpoint (like /favicon.ico).

Related

Recursion when overwriting in variable

I hope you can help me out since this gave me quite a headache.
I'm creating a chain for the middleware which is executed afterwards. But it looks like it has become recurring. The variable next within the anonymous function points to itself.
type MiddlewareInterface interface {
// Run the middleware for the given request, and receive the next handler.
Run(http.ResponseWriter, *http.Request, http.Handler)
}
createChain(collection []MiddlewareInterface, handler http.Handler) http.Handler
next := handler
for _, middlew := range collection {
next = func(w http.ResponseWriter, res *http.Request) {
middlew.Run(w, res, next)
}
}
return next
}
I know it's kind of a noob question, but I sincerely do want to understand what causes this and how this can be resolved. Looking forward to your answers!
Seems this is a closure variable in loop problem. You are creating a function which is capturing next in each loop, but what this will mean is that all of the functions share the same variable next and they will all have the value that is left on the last loop. I think you can fix it by introducing a new temporary variable inside the loop scope:
func createChain(collection []MiddlewareInterface, handler http.Handler) http.Handler
next := handler
for _, middlew := range collection {
thisNext:= next
mw := middlew
next = func(w http.ResponseWriter, res *http.Request) {
mw.Run(w, res, thisNext)
}
}
return next
}
Possibly the placement of the new variable definition isn't quite right but the closure issue will definitely be the source of your problem. It's not normally how http middleware handlers work as they normally wrap each other rather than being chained.

Where does named pipe (FIFO) data go when reader disconnects?

Let's say I have a producer.go and consumer.go. The consumer.go reads from a UNIX named pipe, and the producer writes to the named pipe.
As expected, If you start up just one of the producer or consumer programs, it hangs because there's no reader or writer on the other side of the pipe.
Now, If I start both programs, then immediately CTRL-C from the consumer, the producer continues sending data to the pipe and as far as I can tell there's no limit to the size of that data (I've sent 80MB)
If I start the consumer program again (while the producer is still running), it starts pulling data off of the named pipe, but not the data that I "missed" while the consumer program was not running.
My question is: When a reader of a named pipe disconnects, what happens to the data that's sent to the named pipe?
Here are my consumer.go and producer.go programs:
consumer.go
package main
import (
"io"
"io/ioutil"
"log"
"os"
"syscall"
)
func main() {
syscall.Mkfifo("fifo0", 0777)
fp, err := os.OpenFile("fifo0", os.O_RDONLY, 0777)
if err != nil {
log.Fatalf("Could not open fifo0: %s", err)
}
tee := io.TeeReader(fp, os.Stdout)
ioutil.ReadAll(tee)
}
producer.go
package main
import (
"fmt"
"io"
"log"
"os"
"strings"
"time"
)
func main() {
dots := strings.Repeat(".", 990)
fifo, err := os.OpenFile("fifo0", os.O_WRONLY, 0777)
if err != nil {
log.Fatalf("Could not open fifo0: %s", err)
}
defer fifo.Close()
w := io.MultiWriter(os.Stdout, fifo)
for i := 0; i < 8000; i++ {
fmt.Fprintf(w, "%010d%s\n", i, dots)
time.Sleep(time.Millisecond * 10)
}
}
A FIFO requires at least one source and one destination for data to get transferred anywhere. A reader alone waits to pull from someone, and a writer alone waits to send to someone. In this way, there are no gaps in a one-to-one pipe.
So if you're still trying to read or write from one end of a disconnected or nonexistent pipe, the answer is that the data is going nowhere; the pipe is "blocked" and can hold no data of its own. So it depends on how your code handles this situation.
In producer.go, the loop continues to run even when there's no longer a connection. Because Fprintf or MultiWriter don't raise a code-stopping error for whatever reason. In this case you could either add a check in the loop or an event handler for the fifo object's disconnection.
The reason it seems that there's a gap of disappearing data, is because the loop continues to iterate for i and generate strings it can't send.

Why is there a "-fm" suffix when getting a function's name in Go?

For the snippet below (runnable via the Go Playground),
package main
import (
"fmt"
"net/http"
"reflect"
"runtime"
)
type User struct{}
var u = &User{}
func (_ User) DummyHandler(w http.ResponseWriter, r *http.Request) {}
func funcName(i interface{}) {
p := reflect.ValueOf(i).Pointer()
n := runtime.FuncForPC(p).Name()
fmt.Println(n)
}
func main() {
funcName(u.DummyHandler)
}
The output is main.(User).DummyHandler-fm.
Why is there a -fm at the end of the function name?
Turns out u.DummyHandler is a method value and the compiler implements methods by creating a function closure and modifying the function name. To quote Ian here:
This seems to have become -fm on tip, by the way.
Your code is getting a method value. p.beHappy is the beHappy method
bound to the specific value of p. That is implemented by creating a
function closure, and the code for that closure needs a name. The
compiler happens to make that name by sticking fm on the end, but it
could be anything that won't conflict with any other function name.
There isn't any way to name that function in Go, so the name is
irrelevant for anything other than the debugger or, as you see,
FuncForPC.
It seems like a better way to get a method's name is to reference the method directly, like so:
func main() {
funcName((User).DummyHandler)
}
This will output main.User.DummyHandler.

Passing an struct to a Post martini routine

I have an issue using this statement
m.Post(Model, binding.Form(Wish), func(wish Wish, r render.Render, db *mgo.Database) {
This worked fine if I use the struct define inside the prog like
m.Post(Model, binding.Form(Wish1{}) , func(wish Wish1, r render.Render, db *mgo.Database) {
but I need this to be an independent package.
I get "Wish is not a type" wish is the return of the binding function.
This worked with a primary Type struct. I am passing the strut as a interface{}
I am using GO with Martini.Classic() It is really complicated for me to change Martini or Binding package. Any suggestions.
This is the all code
package chlistpkg
import (
"github.com/codegangsta/martini"
"github.com/codegangsta/martini-contrib/binding"
"github.com/codegangsta/martini-contrib/render"
"labix.org/v2/mgo"
"time"
"fmt"
"html/template"
"reflect"
"adminStruct"
)
just to show the struct that I need to pass as to routine Doall
type Wish1 struct {
Name string `form:"name"`
Description string `form:"description"`
AnyDate time.Time `form:"anydate"`
Active bool `form:"active"`
Number int `form:"number"`
NumDec float32 `form:"numDec"`
}
DB Returns a martini.Handler
func DB() martini.Handler {
session, err := mgo.Dial("mongodb://localhost")
if err != nil {
panic(err)
}
return func(c martini.Context) {
s := session.Clone()
c.Map(s.DB("advent2"))
defer s.Close()
c.Next()
}
}
GetAll returns all Wishes in the database
func GetAll(db *mgo.Database, entList interface{}) interface{} {
db.C("wishes").Find(nil).All(entList)
fmt.Println("GettAll entList =", entList)
return entList
}
func Doall(Model string, Wish interface{}, Wish2 interface{}, Wishlist interface{} ) {
m := martini.Classic()
fmt.Println ("martini.Classic =", m)
m.Use(martini.Static("images")) // serve from the "images" directory as well
m.Use(render.Renderer(render.Options{
Directory: "templates",
Layout: "layout",
}))
m.Use(DB())
m.Get(Model, func(r render.Render, db *mgo.Database) {
r.HTML(200, "lista4", GetAll(db, Wishlist))
})
binding does not take a pointer. I have to pass the struct by reference on "Wish"
the issue is the return on "wish Wish" I got an error Wish is not a type
at compilation time
m.Post(Model, binding.Form(Wish), func(wish Wish, r render.Render, db *mgo.Database) {
fmt.Println("Input wish =", wish)
db.C("wishes").Insert(wish)
r.HTML(200, "lista4", GetAll(db, Wishlist))
})
m.Run()
Thanks in advance
Luis
The reason you are getting an error is that you have called your type Wish1 (with a numerical 1) but you are referring to the Wish type (which does not exist!) in your code.
Change your struct to be:
// Note: "Wish", not "Wish1"
type Wish struct {
Name string `form:"name"`
Description string `form:"description"`
AnyDate time.Time `form:"anydate"`
Active bool `form:"active"`
Number int `form:"number"`
NumDec float32 `form:"numDec"`
}
If you want to put your type into another package (tip: don't overdo the sub-packages), then it will need to become a pkgname.Wish as names are fully qualified.
Added
After a second look, you're also messing things up here:
func Doall(Model string, Wish interface{}, Wish2 interface{}, Wishlist interface{} ) {
m := martini.Classic()
fmt.Println ("martini.Classic =", m)
m.Use(martini.Static("images")) // serve from the "images" directory as well
Your parameter list needs to provide a name for each type; you can't pass Wish interface{} as a parameter as Wish is a type, not a variable name.
You should either:
func DoAll(model string, wish interface{}, wish2 interface{}, wishList interface{}) { ... }
Or, better still, stop using interface{} like this and write:
func DoAll(model string, wishList []Wish, wishes... Wish) { ... }
However, your DoAll function does not seem to be referenced elsewhere, and is creating its own Martini instance. I highly suggest thinking about why things are "split out" like this if you're just starting out. Keep it simple - e.g.
func main() {
m := martini.Classic()
m.Use(martini.Static("images"))
m.Use(DB())
m.Use(render.Renderer(render.Options{...}))
// No need for an anonymous function, which just adds clutter
m.Get("/wishes/all", GetAllWishes)
// Same goes for here
m.Post("/wishes/new", PostWish)
m.Run()
}
PS: I've fixed the formatting of your code, as it has a lot of unnecessary spacing before/after parenthesis. Make sure to use gofmt, which is included with the Go install and can be hooked into most popular editors.

Global variables / Get command line argument and print it

This may sound stupid but how do I define a global variable in Go? const myglobalvariable = "Hi there!" doesn't really work...
I just want to get the command line argument and after this I want to print it. I do this using this code snippet:
package main
import (
"flag"
"fmt"
)
func main() {
gettext();
fmt.Println(text)
}
func gettext() {
flag.Parse()
text := flag.Args()
if len(text) < 1 {
fmt.Println("Please give me some text!")
}
}
The problem is that it just prints an empty line so I thought about declaring a global variable using const myglobalvariable = "Hi there!" but I just get the error cannot use flag.Args() (type []string) as type ideal string in assignment...
...I know this is a noob question so I hope you can help me...
I see at least two questions here, maybe three.
How do you declare a global variable?
How do you declare a global constant?
How do you parse command line options and arguments?
I hope the code below demonstrates this in a helpful way. The flag package was one of the first packages I had to cut my teeth on in Go. At the time it wasn't obvious, though the documentation is improving.
FYI, at the time of this writing I am using http://weekly.golang.org as a reference. The main site is far too out of date.
package main
import (
"flag"
"fmt"
"os"
)
//This is how you declare a global variable
var someOption bool
//This is how you declare a global constant
const usageMsg string = "goprog [-someoption] args\n"
func main() {
flag.BoolVar(&someOption, "someOption", false, "Run with someOption")
//Setting Usage will cause usage to be executed if options are provided
//that were never defined, e.g. "goprog -someOption -foo"
flag.Usage = usage
flag.Parse()
if someOption {
fmt.Printf("someOption was set\n")
}
//If there are other required command line arguments, that are not
//options, they will now be available to parse manually. flag does
//not do this part for you.
for _, v := range flag.Args() {
fmt.Printf("%+v\n", v)
}
//Calling this program as "./goprog -someOption dog cat goldfish"
//outputs
//someOption was set
//dog
//cat
//goldfish
}
func usage() {
fmt.Printf(usageMsg)
flag.PrintDefaults()
os.Exit(1)
}
The closest thing to a global variable in Go is a package variable. You define one like
var text string
Command line arguments though, are already sitting in a package variable, os.Args, waiting for you to access them. You don't even need the flag package.
package main
import (
"fmt"
"os"
)
func main() {
if len(os.Args) < 2 { // (program name is os.Arg[0])
fmt.Println("Please give me some text!")
} else {
fmt.Println(os.Args[1:]) // print all args
}
}
Why do you need a global variable? For example,
package main
import (
"flag"
"fmt"
)
func main() {
text := gettext()
fmt.Println(text)
}
func gettext() []string {
flag.Parse()
text := flag.Args()
if len(text) < 1 {
fmt.Println("Please give me some text!")
}
return text
}
See how gofmt, godoc, and others handle the same thing.

Resources