本文整理匯總了Golang中github.com/cockroachdb/cockroach/client.Open函數的典型用法代碼示例。如果您正苦於以下問題:Golang Open函數的具體用法?Golang Open怎麽用?Golang Open使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了Open函數的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: createTestClient
func createTestClient(addr string) *client.DB {
db, err := client.Open("https://[email protected]" + addr + "?certs=" + security.EmbeddedCertsDir)
if err != nil {
log.Fatal(err)
}
return db
}
示例2: Open
func (d *roachDriver) Open(dsn string) (driver.Conn, error) {
db, err := client.Open(dsn)
if err != nil {
return nil, err
}
u, err := url.Parse(dsn)
if err != nil {
return nil, err
}
ctx := &base.Context{}
ctx.InitDefaults()
if u.User != nil {
ctx.User = u.User.Username()
}
q := u.Query()
if dir := q["certs"]; len(dir) > 0 {
ctx.Certs = dir[0]
}
sender, err := newHTTPSender(u.Host, ctx, client.DefaultTxnRetryOptions)
if err != nil {
return nil, err
}
return &conn{db: db, sender: sender}, nil
}
示例3: Open
func (d *roachDriver) Open(dsn string) (driver.Conn, error) {
db, err := client.Open(dsn)
if err != nil {
return nil, err
}
return &conn{db: db}, nil
}
示例4: Start
// Start starts the test cluster by bootstrapping an in-memory store
// (defaults to maximum of 50M). The server is started, launching the
// node RPC server and all HTTP endpoints. Use the value of
// TestServer.Addr after Start() for client connections. Use Stop()
// to shutdown the server after the test completes.
func (ltc *LocalTestCluster) Start(t util.Tester) {
ltc.Manual = hlc.NewManualClock(0)
ltc.Clock = hlc.NewClock(ltc.Manual.UnixNano)
ltc.Stopper = stop.NewStopper()
rpcContext := rpc.NewContext(testutils.NewRootTestBaseContext(), ltc.Clock, ltc.Stopper)
ltc.Gossip = gossip.New(rpcContext, gossip.TestInterval, gossip.TestBootstrap)
ltc.Eng = engine.NewInMem(proto.Attributes{}, 50<<20)
ltc.lSender = newRetryableLocalSender(NewLocalSender())
ltc.Sender = NewTxnCoordSender(ltc.lSender, ltc.Clock, false, nil, ltc.Stopper)
var err error
if ltc.DB, err = client.Open("//[email protected]", client.SenderOpt(ltc.Sender)); err != nil {
t.Fatal(err)
}
transport := multiraft.NewLocalRPCTransport(ltc.Stopper)
ltc.Stopper.AddCloser(transport)
ctx := storage.TestStoreContext
ctx.Clock = ltc.Clock
ctx.DB = ltc.DB
ctx.Gossip = ltc.Gossip
ctx.Transport = transport
ltc.Store = storage.NewStore(ctx, ltc.Eng, &proto.NodeDescriptor{NodeID: 1})
if err := ltc.Store.Bootstrap(proto.StoreIdent{NodeID: 1, StoreID: 1}, ltc.Stopper); err != nil {
t.Fatalf("unable to start local test cluster: %s", err)
}
ltc.lSender.AddStore(ltc.Store)
if err := ltc.Store.BootstrapRange(nil); err != nil {
t.Fatalf("unable to start local test cluster: %s", err)
}
if err := ltc.Store.Start(ltc.Stopper); err != nil {
t.Fatalf("unable to start local test cluster: %s", err)
}
}
示例5: createTestStoreWithoutStart
// createTestStoreWithoutStart creates a test store using an in-memory
// engine without starting the store. It returns the store, the store
// clock's manual unix nanos time and a stopper. The caller is
// responsible for stopping the stopper upon completion.
func createTestStoreWithoutStart(t *testing.T) (*Store, *hlc.ManualClock, *stop.Stopper) {
stopper := stop.NewStopper()
rpcContext := rpc.NewContext(rootTestBaseContext, hlc.NewClock(hlc.UnixNano), stopper)
ctx := TestStoreContext
ctx.Gossip = gossip.New(rpcContext, gossip.TestInterval, gossip.TestBootstrap)
manual := hlc.NewManualClock(0)
ctx.Clock = hlc.NewClock(manual.UnixNano)
eng := engine.NewInMem(proto.Attributes{}, 10<<20)
ctx.Transport = multiraft.NewLocalRPCTransport()
stopper.AddCloser(ctx.Transport)
sender := &testSender{}
var err error
if ctx.DB, err = client.Open("//[email protected]", client.SenderOpt(sender)); err != nil {
t.Fatal(err)
}
store := NewStore(ctx, eng, &proto.NodeDescriptor{NodeID: 1})
sender.store = store
if err := store.Bootstrap(proto.StoreIdent{NodeID: 1, StoreID: 1}, stopper); err != nil {
t.Fatal(err)
}
if err := store.BootstrapRange(); err != nil {
t.Fatal(err)
}
return store, manual, stopper
}
示例6: createTestNode
// createTestNode creates an rpc server using the specified address,
// gossip instance, KV database and a node using the specified slice
// of engines. The server, clock and node are returned. If gossipBS is
// not nil, the gossip bootstrap address is set to gossipBS.
func createTestNode(addr net.Addr, engines []engine.Engine, gossipBS net.Addr, t *testing.T) (
*rpc.Server, *hlc.Clock, *Node, *stop.Stopper) {
var err error
ctx := storage.StoreContext{}
stopper := stop.NewStopper()
ctx.Clock = hlc.NewClock(hlc.UnixNano)
nodeRPCContext := rpc.NewContext(nodeTestBaseContext, ctx.Clock, stopper)
ctx.ScanInterval = 10 * time.Hour
rpcServer := rpc.NewServer(addr, nodeRPCContext)
if err := rpcServer.Start(); err != nil {
t.Fatal(err)
}
g := gossip.New(nodeRPCContext, testContext.GossipInterval, testContext.GossipBootstrapResolvers)
if gossipBS != nil {
// Handle possibility of a :0 port specification.
if gossipBS == addr {
gossipBS = rpcServer.Addr()
}
g.SetResolvers([]resolver.Resolver{resolver.NewResolverFromAddress(gossipBS)})
g.Start(rpcServer, stopper)
}
ctx.Gossip = g
sender := kv.NewDistSender(&kv.DistSenderContext{Clock: ctx.Clock}, g)
if ctx.DB, err = client.Open("//[email protected]", client.SenderOpt(sender)); err != nil {
t.Fatal(err)
}
// TODO(bdarnell): arrange to have the transport closed.
// (or attach LocalRPCTransport.Close to the stopper)
ctx.Transport = multiraft.NewLocalRPCTransport(stopper)
ctx.EventFeed = util.NewFeed(stopper)
node := NewNode(ctx)
return rpcServer, ctx.Clock, node, stopper
}
示例7: BootstrapCluster
// BootstrapCluster bootstraps a multiple stores using the provided engines and
// cluster ID. The first bootstrapped store contains a single range spanning
// all keys. Initial range lookup metadata is populated for the range.
//
// Returns a KV client for unittest purposes. Caller should close the returned
// client.
func BootstrapCluster(clusterID string, engines []engine.Engine, stopper *stop.Stopper) (*client.DB, error) {
ctx := storage.StoreContext{}
ctx.ScanInterval = 10 * time.Minute
ctx.Clock = hlc.NewClock(hlc.UnixNano)
// Create a KV DB with a local sender.
lSender := kv.NewLocalSender()
sender := kv.NewTxnCoordSender(lSender, ctx.Clock, false, nil, stopper)
var err error
if ctx.DB, err = client.Open("//[email protected]", client.SenderOpt(sender)); err != nil {
return nil, err
}
ctx.Transport = multiraft.NewLocalRPCTransport(stopper)
for i, eng := range engines {
sIdent := proto.StoreIdent{
ClusterID: clusterID,
NodeID: 1,
StoreID: proto.StoreID(i + 1),
}
// The bootstrapping store will not connect to other nodes so its
// StoreConfig doesn't really matter.
s := storage.NewStore(ctx, eng, &proto.NodeDescriptor{NodeID: 1})
// Verify the store isn't already part of a cluster.
if len(s.Ident.ClusterID) > 0 {
return nil, util.Errorf("storage engine already belongs to a cluster (%s)", s.Ident.ClusterID)
}
// Bootstrap store to persist the store ident.
if err := s.Bootstrap(sIdent, stopper); err != nil {
return nil, err
}
// Create first range, writing directly to engine. Note this does
// not create the range, just its data. Only do this if this is the
// first store.
if i == 0 {
if err := s.BootstrapRange(); err != nil {
return nil, err
}
}
if err := s.Start(stopper); err != nil {
return nil, err
}
lSender.AddStore(s)
// Initialize node and store ids. Only initialize the node once.
if i == 0 {
if nodeID, err := allocateNodeID(ctx.DB); nodeID != sIdent.NodeID || err != nil {
return nil, util.Errorf("expected to initialize node id allocator to %d, got %d: %s",
sIdent.NodeID, nodeID, err)
}
}
if storeID, err := allocateStoreIDs(sIdent.NodeID, 1, ctx.DB); storeID != sIdent.StoreID || err != nil {
return nil, util.Errorf("expected to initialize store id allocator to %d, got %d: %s",
sIdent.StoreID, storeID, err)
}
}
return ctx.DB, nil
}
示例8: ExampleDB_Put_insecure
func ExampleDB_Put_insecure() {
s := &server.TestServer{}
s.Ctx = server.NewTestContext()
s.Ctx.Insecure = true
if pErr := s.Start(); pErr != nil {
log.Fatalf("Could not start server: %v", pErr)
}
defer s.Stop()
db, err := client.Open(s.Stopper(), "rpc://[email protected]"+s.ServingAddr())
if err != nil {
log.Fatal(err)
}
if pErr := db.Put("aa", "1"); pErr != nil {
panic(pErr)
}
result, pErr := db.Get("aa")
if pErr != nil {
panic(pErr)
}
fmt.Printf("aa=%s\n", result.ValueBytes())
// Output:
// aa=1
}
示例9: ExampleDB_Insecure
func ExampleDB_Insecure() {
s := &server.TestServer{}
s.Ctx = server.NewTestContext()
s.Ctx.Insecure = true
if err := s.Start(); err != nil {
log.Fatalf("Could not start server: %v", err)
}
log.Printf("Test server listening on %s: %s", s.Ctx.RequestScheme(), s.ServingAddr())
defer s.Stop()
db, err := client.Open("http://[email protected]" + s.ServingAddr())
if err != nil {
log.Fatal(err)
}
if err := db.Put("aa", "1"); err != nil {
panic(err)
}
result, err := db.Get("aa")
if err != nil {
panic(err)
}
fmt.Printf("aa=%s\n", result.ValueBytes())
// Output:
// aa=1
}
示例10: createTestClientFor
func createTestClientFor(stopper *stop.Stopper, addr, user string) *client.DB {
db, err := client.Open(stopper, "rpcs://"+user+"@"+addr+"?certs="+security.EmbeddedCertsDir)
if err != nil {
log.Fatal(err)
}
return db
}
示例11: makeClient
func makeClient(t *testing.T, str string) (*client.DB, *stop.Stopper) {
stopper := stop.NewStopper()
db, err := client.Open(stopper, str)
if err != nil {
t.Fatal(err)
}
return db, stopper
}
示例12: createTestClientForUser
func createTestClientForUser(t *testing.T, addr, user string) *client.DB {
db, err := client.Open(fmt.Sprintf("https://%[email protected]%s?certs=%s",
user, addr, security.EmbeddedCertsDir))
if err != nil {
t.Fatal(err)
}
return db
}
示例13: createTestClientForUser
func createTestClientForUser(t *testing.T, stopper *stop.Stopper, addr, user string) *client.DB {
db, err := client.Open(stopper, fmt.Sprintf("rpcs://%[email protected]%s?certs=%s&failfast=1",
user, addr, security.EmbeddedCertsDir))
if err != nil {
t.Fatal(err)
}
return db
}
示例14: setup
func setup() (*server.TestServer, *client.DB) {
s := server.StartTestServer(nil)
db, err := client.Open("https://[email protected]" + s.ServingAddr() + "?certs=test_certs")
if err != nil {
log.Fatal(err)
}
return s, db
}
示例15: createTestNotifyClient
// createTestNotifyClient creates a new client which connects using an HTTP
// sender to the server at addr. It contains a waitgroup to allow waiting.
func createTestNotifyClient(addr string, priority int) (*client.DB, *notifyingSender) {
db, err := client.Open(fmt.Sprintf("https://[email protected]%s?certs=%s&priority=%d",
addr, security.EmbeddedCertsDir, priority))
if err != nil {
log.Fatal(err)
}
sender := ¬ifyingSender{wrapped: db.Sender}
db.Sender = sender
return db, sender
}