Example #1
0
File: pool.go Project: karrick/gocp
// New returns a new Pool structure.
func New(setters ...Configurator) (*Pool, error) {
	pc := &config{
		size: DefaultSize,
	}
	for _, setter := range setters {
		if err := setter(pc); err != nil {
			return nil, err
		}
	}
	if pc.address == "" {
		return nil, errors.New("cannot create pool with empty address")
	}
	pool, err := gopool.New(
		gopool.Size(pc.size),
		gopool.Close(func(conn interface{}) error {
			return conn.(io.ReadWriteCloser).Close()
		}),
		gopool.Factory(func() (interface{}, error) {
			if pc.printer != nil {
				return goperconn.New(goperconn.Address(pc.address), goperconn.Logger(pc.printer))
			}
			return goperconn.New(goperconn.Address(pc.address))
		}),
	)
	if err != nil {
		return nil, err
	}
	return &Pool{pool: pool}, nil
}
Example #2
0
func main() {
	printer := log.New(os.Stderr, "WARNING: ", 0)

	// NOTE: Address is required, but all other parameters have defaults.
	conn, err := goperconn.New(goperconn.Address("echo-server.example.com:7"),
		goperconn.DialTimeout(5*time.Second),
		goperconn.Logger(printer),
		goperconn.RetryMin(time.Second),
		goperconn.RetryMax(30*time.Second))
	if err != nil {
		log.Fatal(err)
	}

	// later ...

	_, err = conn.Write([]byte("hello, world"))
	if err != nil {
		log.Fatal(err)
	}

	buf := make([]byte, 512)
	_, err = conn.Read(buf)
	if err != nil {
		log.Fatal(err)
	}
}