Skip to content

Commit

Permalink
Push examples into their own file.
Browse files Browse the repository at this point in the history
Update documentation with new and improved examples
  • Loading branch information
Evan Farrer authored and Evan Farrer committed Apr 26, 2012
1 parent f5cccf2 commit fa823a7
Show file tree
Hide file tree
Showing 2 changed files with 132 additions and 47 deletions.
91 changes: 44 additions & 47 deletions README.md
Expand Up @@ -11,54 +11,51 @@ If you have the Go Language installed type


Example Example
------- -------
// Download google's home page at 100 bytes per second and print how many bytes were downloaded

package main

import ( import (
"github.com/efarrer/iothrottler" . "github.com/efarrer/iothrottler"
"io/ioutil"
"log"
"net"
"net/http"
) )


func main() { /*

* Throttle web requests using an IOThrottlerPool
// Construct a bandwidth throttling pool that's limited to 100 bytes per second */
pool := iothrottler.NewIOThrottlerPool(iothrottler.BytesPerSecond * 100) func ExampleIOThrottlerPool_AddConn() {
defer pool.ReleasePool() // Construct a bandwidth throttling pool that's limited to 30 kilobits per

// second
// Create our own Dial function that will be used for the http connection pool := NewIOThrottlerPool(Kbps * 30)
throttledDial := func(nt, addr string) (c net.Conn, err error) { defer pool.ReleasePool()
conn, err := net.Dial(nt, addr)
if err != nil { // Create our own Dial function that will be used for the http connection
return nil, err throttledDial := func(nt, addr string) (c net.Conn, err error) {
} conn, err := net.Dial(nt, addr)

if err != nil {
return pool.AddConn(conn) return nil, err
} }


// Create a transport that will use our throttled Dial function return pool.AddConn(conn)
tr := &http.Transport{ }
DisableCompression: true,
Dial: throttledDial, // Create a transport that will use our throttled Dial function
} tr := &http.Transport{

Proxy: http.ProxyFromEnvironment,
// Download the page Dial: throttledDial,
client := &http.Client{Transport: tr} }
resp, err := client.Get("http://www.google.com")
if err != nil { // Download the page
log.Fatal(err) client := &http.Client{Transport: tr}
} resp, err := client.Get("http://www.google.com")
defer resp.Body.Close() if err != nil {

// handle error
// Read the entire contents of the body return
body, err := ioutil.ReadAll(resp.Body) }
if err != nil { defer resp.Body.Close()
log.Fatal(err)
} // Read the entire contents of the body

_, err = ioutil.ReadAll(resp.Body)
// Print the body length if err != nil {
println(len(body)) // handle error
return
}

fmt.Println("Done")
// Output: Done
} }
88 changes: 88 additions & 0 deletions example_test.go
@@ -0,0 +1,88 @@
package iothrottler

import (
"bytes"
"fmt"
"io"
"io/ioutil"
"net"
"net/http"
"os"
)

/*
* Basic usage of a IOThrottlerPool to throttle reading from a file
*/
func ExampleIOThrottlerPool() {
// Construct a bandwidth throttling pool that's limited to 100 bytes per second
pool := NewIOThrottlerPool(BytesPerSecond * 100)
defer pool.ReleasePool()

file, err := os.Open("/dev/zero")
if err != nil {
// handle error
return
}
defer file.Close()

throttledFile, err := pool.AddReader(file)
if err != nil {
// handle error
return
}

var zeros bytes.Buffer

_, err = io.CopyN(&zeros, throttledFile, 200)
if err != nil {
// handle error
}

fmt.Println("Done")
// Output: Done
}

/*
* Throttle web requests using an IOThrottlerPool
*/
func ExampleIOThrottlerPool_AddConn() {
// Construct a bandwidth throttling pool that's limited to 30 kilobits per
// second
pool := NewIOThrottlerPool(Kbps * 30)
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{
Proxy: http.ProxyFromEnvironment,
Dial: throttledDial,
}

// Download the page
client := &http.Client{Transport: tr}
resp, err := client.Get("http://www.google.com")
if err != nil {
// handle error
return
}
defer resp.Body.Close()

// Read the entire contents of the body
_, err = ioutil.ReadAll(resp.Body)
if err != nil {
// handle error
return
}

fmt.Println("Done")
// Output: Done
}

0 comments on commit fa823a7

Please sign in to comment.