本文整理匯總了Golang中github.com/kr/beanstalk.Dial函數的典型用法代碼示例。如果您正苦於以下問題:Golang Dial函數的具體用法?Golang Dial怎麽用?Golang Dial使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Dial函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: createConnection
func (m *mainFrame) createConnection() (*beanstalk.Conn, error) {
c, err := beanstalk.Dial("tcp", fmt.Sprintf("%s:%d", m.host, m.port))
if err != nil {
return nil, err
}
return c, nil
}
示例2: TestReserveIsParallelAndWaits
func TestReserveIsParallelAndWaits(t *testing.T) {
count := int32(0)
tubeName := strconv.Itoa(int(time.Now().Unix()))
start := time.Now()
mux := NewWorkMux()
mux.Handle(tubeName, HandlerFunc(func(job *Job) {
time.Sleep(time.Second)
atomic.AddInt32(&count, 1)
job.Delete()
}))
go func() {
conn, _ := beanstalk.Dial("tcp", "localhost:11300")
tube := &beanstalk.Tube{Conn: conn, Name: tubeName}
tube.Put([]byte("job1"), 0, 0, time.Minute)
tube.Put([]byte("job2"), 0, 0, time.Minute)
tube.Put([]byte("job3"), 0, 0, time.Minute)
tube.Put([]byte("job4"), 0, 0, time.Minute)
tube.Put([]byte("job5"), 0, 0, time.Minute)
time.Sleep(time.Millisecond * 1100)
syscall.Kill(syscall.Getpid(), syscall.SIGTERM)
}()
ConnectAndWork("tcp", "localhost:11300", mux)
if count != 5 || time.Since(start) > time.Duration(time.Millisecond*2200) {
t.Fail()
}
}
示例3: getTestConn
func getTestConn(t *testing.T) *beanstalk.Conn {
conn, err := beanstalk.Dial("tcp", "localhost:11300")
if err != nil {
t.Fail()
}
return conn
}
示例4: Producer
func Producer(fname, tubeName string) {
if fname == "" || tubeName == "" {
return
}
c, err := beanstalk.Dial("tcp", "127.0.0.1:11300")
if err != nil {
panic(err)
}
defer c.Close()
c.Tube.Name = tubeName
c.TubeSet.Name[tubeName] = true
fmt.Println(fname, " [Producer] tubeName:", tubeName, " c.Tube.Name:", c.Tube.Name)
for i := 0; i < 5; i++ {
msg := fmt.Sprintf("for %s %d", tubeName, i)
c.Put([]byte(msg), 30, 0, 120*time.Second)
fmt.Println(fname, " [Producer] beanstalk put body:", msg)
//time.Sleep(1 * time.Second)
}
c.Close()
fmt.Println("Producer() end.")
}
示例5: Connect
// Connect to the server
func (c *Client) Connect() (err error) {
if c.Conn != nil {
return nil
}
c.Conn, err = beanstalk.Dial("tcp", c.addr)
return err
}
示例6: TubesStats
// TubesStats connects to beanstalkd and return a hash of all stats
// for each tube we are watching
//
// Return error if fail to connect to beanstalkd of fail to get stats
// for a specic tube
//
// Panic if a specic stat is not an integer, this should never happen
func TubesStats() (stats map[string]map[string]int, err error) {
conn, err := beanstalk.Dial("tcp", config.BeanstalkdAddr)
if err != nil {
return stats, fmt.Errorf("Failed to connect to beanstalkd: %s", err)
}
tubes, err := conn.ListTubes()
if err != nil {
return stats, fmt.Errorf("Failed to list tubes: %s", err)
}
stats = map[string]map[string]int{}
for _, tubeName := range tubes {
if !watchingTube(tubeName) {
continue
}
tube := &beanstalk.Tube{
Name: tubeName,
Conn: conn,
}
data, err := tube.Stats()
if err != nil {
return stats, fmt.Errorf("Failed to get stats for tube %s: %s", tubeName, err)
}
stats[tubeName] = map[string]int{
"buried": mustInt(data["current-jobs-buried"]),
"ready": mustInt(data["current-jobs-ready"]),
"delayed": mustInt(data["current-jobs-delayed"]),
"reserver": mustInt(data["current-jobs-reserved"]),
"urgent": mustInt(data["current-jobs-urgent"]),
"waiting": mustInt(data["current-waiting"]),
"total": mustInt(data["total-jobs"]),
}
}
return stats, nil
}
示例7: main
func main() {
commandArgs, other := parseCommands()
beansAddr := parseArgs(other)
c, err := beanstalk.Dial("tcp", beansAddr)
if err != nil {
fmt.Printf("Warning: can't connect to beanstalk on %s (use -b arg to specify correct connection addr:port)\n", beansAddr)
} else {
defer c.Close()
}
if len(commandArgs) == 0 {
printUsageInfo()
os.Exit(1)
}
switch commandArgs[0] {
case "help":
helpCmd(c)
os.Exit(0)
case "server-info":
checkConn(c)
s, _ := c.Stats()
fmt.Println("Global server info:\n")
printStats(s)
default:
checkConn(c)
tubeCmd(c, commandArgs[0], commandArgs[1:])
}
}
示例8: DialBeanstalk
func DialBeanstalk() *beanstalk.Conn {
conn, err := beanstalk.Dial("tcp", *beanstalkdAddress)
if err != nil {
fatal(1, "Error connecting to beanstalkd:\n%v\n", err)
}
return conn
}
示例9: connection
func connection() (*beanstalk.Conn, error) {
var (
addr string
err error
)
mut.Lock()
if conn == nil {
mut.Unlock()
addr, err = config.GetString("queue-server")
if err != nil {
addr = "localhost:11300"
}
mut.Lock()
if conn, err = beanstalk.Dial("tcp", addr); err != nil {
mut.Unlock()
return nil, err
}
}
if _, err = conn.ListTubes(); err != nil {
mut.Unlock()
conn = nil
return connection()
}
mut.Unlock()
return conn, err
}
示例10: Init
/**
連接隊列
*/
func (beansObj *BeansCon) Init(config [2]string) {
c, err := beanstalk.Dial(config[0], config[1])
if err != nil {
panic(err)
}
beansObj.conn = c
}
示例11: dialSubSocket
func (m *beanstalkdPubSub) dialSubSocket() error {
c, err := beanstalk.Dial("tcp", m.address)
if err != nil {
return err
}
m.connSub = c
return nil
}
示例12: main
func main() {
flag.Parse()
var tb *beanstalk.TubeSet
var conn_bs *beanstalk.Conn
rs_timeout := time.Duration(Settings.BeanstalkdReserveTimeout)
fail_wait := time.Duration(Settings.FailWait) * time.Second
conn_bs, e := beanstalk.Dial("tcp", Settings.BeanstalkdAddr)
if e != nil {
log.Fatal("failed to connected to beanstalkd", e)
}
tb = beanstalk.NewTubeSet(conn_bs, Settings.BeanstalkdTube)
for {
// reserve a job
id, job, e := tb.Reserve(rs_timeout)
// timeout is valid, anything else is fatal
if cerr, ok := e.(beanstalk.ConnError); ok && cerr.Err == beanstalk.ErrTimeout {
time.Sleep(fail_wait)
continue
} else if e != nil {
log.Fatal("failed to reserve job", e)
} else {
log.Println("read job id", id, "size", len(job), "bytes")
}
// connect to the gor replay server
conn_gr, e := net.Dial("tcp", Settings.GorReplayAddr)
if e != nil {
log.Fatal("failed to connected to gor replay server", e)
time.Sleep(fail_wait)
}
// write to gor replay server
w, e := conn_gr.Write(job)
if e != nil {
log.Fatal("failed to write to", Settings.GorReplayAddr, "error", e)
} else {
log.Println("wrote", w, "bytes to", Settings.GorReplayAddr)
}
// close connection to gor replay server
conn_gr.Close()
// delete the job
e = conn_bs.Delete(id)
if e != nil {
log.Println("failed to delete job id", id, "error", e)
}
}
}
示例13: connect
func connect(hostname string) *beanstalk.Conn {
c, err := beanstalk.Dial("tcp", hostname)
if err != nil {
fmt.Fprintf(os.Stderr, "Could not connect to %s\n", hostname)
os.Exit(2)
}
return c
}
示例14: dialPubConnection
func (m *beanstalkdPubSub) dialPubConnection() error {
// open connection for subscription
c, err := beanstalk.Dial("tcp", m.address)
if err != nil {
return err
}
m.connPub = c
return nil
}
示例15: main
func main() {
c, err := beanstalk.Dial("tcp", "127.0.0.1:11300")
if err != nil {
fmt.Println("beanstakd not started")
os.Exit(1)
}
id, err := c.Put([]byte("hello"), 1, 0, 120*time.Second)
fmt.Println(id, err)
}