Skip to content
This repository has been archived by the owner. It is now read-only.
[UNMAINTAINED] 🆎 A collection of often-used Golang helpers
Go Makefile
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.gitignore Add Makefile, .gitignore Aug 27, 2015
.travis.yml
LICENSE Add LICENSE Aug 26, 2015
Makefile Add Makefile, .gitignore Aug 27, 2015
README.md Add examples in README.md, update code examples Sep 2, 2015
concurrency.go Improve Parallel comment Aug 27, 2015
concurrency_test.go Add examples in README.md, update code examples Sep 2, 2015
database.go Rename Rollback -> RollbackErr, add example Sep 1, 2015
database_test.go Add examples in README.md, update code examples Sep 2, 2015
http.go
http_test.go Add examples in README.md, update code examples Sep 2, 2015
signals.go Remove redundant locks, test OnSignal Aug 27, 2015
signals_test.go Reduce test methods to one per method Sep 2, 2015

README.md

abutil GoDoc Build Status Coverage Status

abutil is a collection of often-used Golang helpers.

Contents

Functions

OnSignal

Listens to various signals and executes the given function with the received signal.

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

Parallel

Executes the given function n times concurrently.

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

    fmt.Print(c)
    c++
})

RollbackErr

Does a rollback on the given transaction and returns either the rollback-error, if occured, or the given one.

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

Tries everything to get the remote ip.

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

GracefulServer

A wrapper around graceful.Server from http://github.com/tylerb/graceful with state variable and easier handling.

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

This project is licensed under the WTFPL, for more information see the LICENSE file.

You can’t perform that action at this time.