Skip to content
This repository has been archived by the owner on Mar 16, 2019. It is now read-only.

Commit

Permalink
Add examples in README.md, update code examples
Browse files Browse the repository at this point in the history
  • Loading branch information
bahlo committed Sep 2, 2015
1 parent 454a08b commit 4422016
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 15 deletions.
99 changes: 96 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,103 @@

abutil is a collection of often-used Golang helpers.

## Usage
## Contents
- [Functions](#functions)
- [OnSignal](#onsignal)
- [Parallel](#parallel)
- [RollbackErr](#rollbackerr)
- [RemoteIP](#remoteip)
- [GracefulServer](#gracefulserver)
- [License](#license)

Please read the [Godoc](https://godoc.org/github.com/bahlo/abutil) for
an overview of the provided functionality and examples.
## Functions

#### [OnSignal](https://godoc.org/github.com/bahlo/abutil#OnSignal)
Listens to various signals and executes the given function with the received
signal.

```go
go abutil.OnSignal(func(s os.Signal) {
fmt.Printf("Got signal %s\n", s)
})
```

#### [Parallel](https://godoc.org/github.com/bahlo/abutil#Parallel)
Executes the given function n times concurrently.

```go
var m sync.Mutex
c := 0
abutil.Parallel(4, func() {
m.Lock()
defer m.Unlock()

fmt.Print(c)
c++
})
```

#### [RollbackErr](https://godoc.org/github.com/bahlo/abutil#RollbackErr)
Does a rollback on the given transaction and returns either the rollback-error,
if occured, or the given one.

```go
insertSomething := func(db *sql.DB) error {
tx, _ := db.Begin()

_, err := tx.Exec("INSERT INTO some_table (some_column) VALUES (?)",
"foobar")
if err != nil {
// The old way, imagine doing this 10 times in a method
if err := tx.Rollback(); err != nil {
return err
}

return err
}

_, err = tx.Exec("DROP DATABASE foobar")
if err != nil {
// With RollbackErr
return abutil.RollbackErr(tx, err)
}

return nil
}
```

#### [RemoteIP](https://godoc.org/github.com/bahlo/abutil#RemoteIP)
Tries everything to get the remote ip.

```go
someHandler := func(w http.ResponseWriter, r *http.Request) {
fmt.Printf("New request from %s\n", abutil.RemoteIP(r))
}
```

#### [GracefulServer](https://godoc.org/github.com/bahlo/abutil#GracefulServer)
A wrapper around `graceful.Server` from <http://github.com/tylerb/graceful>
with state variable and easier handling.

```go
s := abutil.NewGracefulServer(1337, someHandlerFunc)

// This channel blocks until all connections are closed or the time is up
sc := s.StopChan()

// Some go func that stops the server after 2 seconds for no reason
time.After(2 * time.Second, func() {
s.Stop(10 * time.Second)
})

if err := s.ListenAndServe(); err != nil && !s.Stopped() {
// We didn't stop the server, so something must be wrong
panic(err)
}

// Wait for the server to finish
<-sc
```

## License

Expand Down
10 changes: 5 additions & 5 deletions concurrency_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ func TestParallel(t *testing.T) {
wg.Add(2)
go Parallel(2, func() {
m.Lock()
defer func() {
m.Unlock()
wg.Done()
}()

counter++

m.Unlock()
wg.Done()
})
wg.Wait()

Expand All @@ -35,11 +36,10 @@ func ExampleParallel() {
c := 0
Parallel(4, func() {
m.Lock()
defer m.Unlock()

fmt.Print(c)
c++

m.Unlock()
})

// Output: 0123
Expand Down
10 changes: 7 additions & 3 deletions database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,13 +70,17 @@ func ExampleRollbackErr() {
_, err := tx.Exec("INSERT INTO some_table (some_column) VALUES (?)",
"foobar")
if err != nil {
// We now have a one-liner instead of a check every time an error
// occurs
return RollbackErr(tx, err)
// The old way, imagine doing this 10 times in a method
if err := tx.Rollback(); err != nil {
return err
}

return err
}

_, err = tx.Exec("DROP DATABASE foobar")
if err != nil {
// With RollbackErr
return RollbackErr(tx, err)
}

Expand Down
14 changes: 10 additions & 4 deletions http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,16 +184,22 @@ func ExampleGracefulServer() {
w.Write([]byte("Foo bar"))
}))

time.AfterFunc(20*time.Millisecond, func() {
// For some reason, we need to stop
// This channel blocks until all connections are closed or the time is up
sc := s.StopChan()

// Some go func that stops the server after 2 seconds for no reason
time.AfterFunc(1*time.Second, func() {
fmt.Print("Stopping server..")
s.Stop(0)
s.Stop(10 * time.Second)
})

if err := s.ListenAndServe(); err != nil && !s.Stopped() {
// An error occured but it wasn't intentionally
// We didn't stop the server, so something must be wrong
panic(err)
}

// Wait for the server to finish
<-sc
fmt.Print("bye!")

// Output: Stopping server..bye!
Expand Down

0 comments on commit 4422016

Please sign in to comment.