beanstalk

package module
v0.0.0-...-5ee6e8d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 2, 2012 License: MIT Imports: 9 Imported by: 0

README

beanstalk.go

beanstalk.go is a client library for the protocol used by beanstalkd.

Installation

No need for that. Just put import "github.com/kr/beanstalk.go.git" at the top of your go package, and install your own package with goinstall.

Overview

To open a connection, do

conn, err := beanstalk.Dial("localhost:11300")

This package provides a simple, blocking interface. Go makes it easy to add asynchrony if you want.

Common Case: To submit a job and get its id, do

id, err := conn.Put(...)

Fire and Forget: If you don't care about the id, no need to wait around:

go conn.Put(...)

Full Asynchrony: If you don't want to wait but still need the id, it's still easy:

go func() {
  id, err := conn.Put(...)
}()

Concurrency and Optimizations

You can perform operations on the queue at will from all goroutines. This package will issue commands to beanstalkd and return results to where they belong. It'll also optimize the commands a bit to reduce network traffic.

For example, the use, watch, and ignore commands are managed entirely by this library, and issued only as necessary.

It also collects as many outbound commands as possible into a single network packet, for efficiency.

In the future, it will manage the order of outgoing commands to further reduce the need for use, watch, and ignore commands, and to prevent reserve commands from stalling other commands unnecessarily.

Complete Example

A producer:

package main

import "github.com/kr/beanstalk.go.git"

func main() {
    conn, err := beanstalk.Dial("localhost:11300")
    conn.Put("hello", 0, 0, 10)
}

And a worker:

package main

import "github.com/kr/beanstalk.go.git"

func main() {
    conn, err := beanstalk.Dial("localhost:11300")
    for {
        j, err := conn.Reserve()
        fmt.Println(j.Body) // prints "hello"
        j.Delete()
    }
}

Contributing

  1. Fix a bug or implement a new feature.
  2. Add tests verifying your change.
  3. Publish your changes in a public git repository. At most one feature or bug fix per commit. Topic branches are preferred, especially for larger changes.
  4. Send me email with the URL of your repository and the branch(es) you want pulled.

Credit Where It's Due

  • spymemcached for the idea of making optimizing transformations on the command stream.

  • Go's standard libraries, especially net and http, for inspiration and guidance.

Documentation

Overview

Client library for the beanstalkd protocol. See http://kr.github.com/beanstalkd/

We are lenient about the protocol -- we accept either CR LF or just LF to terminate server replies. We also trim white space around words in reply lines.

This package is synchronized internally. It is safe to call any of these functions from any goroutine at any time.

Note that, as of version 1.4.4, beanstalkd provides only 1-second granularity on all duration values.

Index

Constants

View Source
const Forever = 4000000000000000 // µs

For use in parameters that measure duration (in microseconds). Not really infinite; merely large. About 126 years.

Variables

View Source
var (
	NameTooLong = os.NewError("name too long")
	IllegalChar = os.NewError("name contains illegal char")
)

Reasons for an invalid tube name.

View Source
var (
	OutOfMemory   = os.NewError("Server Out of Memory")
	InternalError = os.NewError("Server Internal Error")
	Draining      = os.NewError("Server Draining")
	Buried        = os.NewError("Buried")
	JobTooBig     = os.NewError("Job Too Big")
	TimedOut      = os.NewError("Reserve Timed Out")
	NotFound      = os.NewError("Job or Tube Not Found")
	NotIgnored    = os.NewError("Tube Not Ignored")
)

Error responses from the server.

View Source
var BadReply = os.NewError("Bad Reply from Server")

The server sent a bad reply. For example: unknown or inappropriate response, wrong number of terms, or invalid format.

Functions

This section is empty.

Types

type Conn

type Conn struct {
	Name string
	*Tube
	*TubeSet
	// contains filtered or unexported fields
}

A connection to beanstalkd. Provides methods that operate outside of any tube. This type also embeds Tube and TubeSet, which is convenient if you rarely change tubes.

func Dial

func Dial(addr string) (*Conn, os.Error)

Dial the beanstalkd server at remote address addr.

func (*Conn) ListTubes

func (c *Conn) ListTubes() ([]string, os.Error)

func (*Conn) Peek

func (c *Conn) Peek(id uint64) (*Job, os.Error)

Get a copy of the specified job.

func (*Conn) Stats

func (c *Conn) Stats() (map[string]string, os.Error)

type Error

type Error struct {
	ConnName string
	Cmd      string
	Reply    string
	Error    os.Error
}

Implements os.Error

func (Error) String

func (e Error) String() string

type Job

type Job struct {
	Id   uint64
	Body string
	// contains filtered or unexported fields
}

func (Job) Bury

func (j Job) Bury(pri uint32) os.Error

Bury job j and change its priority to pri.

func (Job) Delete

func (j Job) Delete() os.Error

Delete job j.

func (Job) Release

func (j Job) Release(pri uint32, µsDelay uint64) os.Error

Release job j, changing its priority to pri and its delay to delay.

func (Job) Stats

func (j Job) Stats() (map[string]string, os.Error)

Get statistics on job j.

func (Job) Touch

func (j Job) Touch() os.Error

Touch job j.

type Tube

type Tube struct {
	Name string
	// contains filtered or unexported fields
}

Represents a single tube. Provides methods that operate on one tube, especially Put.

func NewTube

func NewTube(c *Conn, name string) (*Tube, os.Error)

Returns an error if the tube name is invalid.

func (Tube) Kick

func (t Tube) Kick(n uint64) (uint64, os.Error)

Kick up to n jobs in tube t.

func (Tube) Pause

func (t Tube) Pause(µs uint64) os.Error

Pause tube t for µs microseconds.

func (Tube) PeekBuried

func (t Tube) PeekBuried() (*Job, os.Error)

Get a copy of a buried job in this tube, if any.

func (Tube) PeekDelayed

func (t Tube) PeekDelayed() (*Job, os.Error)

Get a copy of the next delayed job in this tube, if any.

func (Tube) PeekReady

func (t Tube) PeekReady() (*Job, os.Error)

Get a copy of the next ready job in this tube, if any.

func (Tube) Put

func (t Tube) Put(body string, pri uint32, µsDelay, µsTTR uint64) (id uint64, err os.Error)

Put a job into the queue and return its id.

If an error occured, err will be non-nil. For some errors, Put will also return a valid job id, so you must check both values.

The delay and ttr are measured in microseconds.

func (Tube) Stats

func (t Tube) Stats() (map[string]string, os.Error)

Get statistics on tube t.

type TubeError

type TubeError struct {
	TubeName string
	Error    os.Error
}

func (TubeError) String

func (e TubeError) String() string

type TubeSet

type TubeSet struct {
	Names []string
	// contains filtered or unexported fields
}

Represents a set of tubes. Provides methods that operate on several tubes at once, especially Reserve.

func NewTubeSet

func NewTubeSet(c *Conn, names []string) (*TubeSet, os.Error)

Returns an error if any of the tube names are invalid.

func (TubeSet) Reserve

func (t TubeSet) Reserve() (*Job, os.Error)

Reserve a job from any one of the tubes in t.

Directories

Path Synopsis
A convenient way to run jobs from beanstalkd.
A convenient way to run jobs from beanstalkd.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL