Permalink
Browse files

Add install instructions and example to readme

We now have just enough documentation for someone to start using the library.
It should not be possible for someone to
  • Loading branch information...
1 parent c4f6033 commit f993d34f4cad04e083aad56d0ab803959dc4ebb3 Evan Farrer committed Apr 23, 2012
Showing with 61 additions and 1 deletion.
  1. +61 −1 README.md
View
@@ -1,4 +1,64 @@
iothrottler
===========
-A Go package for throttling application IO (such as bandwidth).
+A Go package for throttling application IO (such as bandwidth).
+
+Installation
+------------
+
+If you have the Go Language installed type
+`go get "github.com/efarrer/iothrottler"`
+
+Example
+-------
+// Download google's home page at 100 bytes per second and print how many bytes were downloaded
+
+package main
+
+import (
+ "github.com/efarrer/iothrottler"
+ "io/ioutil"
+ "log"
+ "net"
+ "net/http"
+)
+
+func main() {
+
+ // Construct a bandwidth throttling pool that's limited to 100 bytes per second
+ pool := iothrottler.NewIOThrottlerPool(iothrottler.BytesPerSecond * 100)
+ defer pool.ReleasePool()
+
+ // Create our own Dial function that will be used for the http connection
+ throttledDial := func(nt, addr string) (c net.Conn, err error) {
+ conn, err := net.Dial(nt, addr)
+ if err != nil {
+ return nil, err
+ }
+
+ return pool.AddConn(conn)
+ }
+
+ // Create a transport that will use our throttled Dial function
+ tr := &http.Transport{
+ DisableCompression: true,
+ Dial: throttledDial,
+ }
+
+ // Download the page
+ client := &http.Client{Transport: tr}
+ resp, err := client.Get("http://www.google.com")
+ if err != nil {
+ log.Fatal(err)
+ }
+ defer resp.Body.Close()
+
+ // Read the entire contents of the body
+ body, err := ioutil.ReadAll(resp.Body)
+ if err != nil {
+ log.Fatal(err)
+ }
+
+ // Print the body length
+ println(len(body))
+}

0 comments on commit f993d34

Please sign in to comment.