Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Push examples into their own file.

Update documentation with new and improved examples
  • Loading branch information...
commit fa823a7dd203eedae80441eb0aca4baedd9c9f90 1 parent f5cccf2
Evan Farrer authored
Showing with 132 additions and 47 deletions.
  1. +44 −47 README.md
  2. +88 −0 example_test.go
View
91 README.md
@@ -11,54 +11,51 @@ If you have the Go Language installed type
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"
+. "github.com/efarrer/iothrottler"
)
-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))
+/*
+ * 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
}
View
88 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
+}
Please sign in to comment.
Something went wrong with that request. Please try again.