MCPcopy Create free account
hub / github.com/couchbase/sync_gateway / ConnectToBucket

Function ConnectToBucket

db/database.go:342–367  ·  view source on GitHub ↗

ConnectToBucket opens a Couchbase connection and return a specific bucket. If failFast is set, fail immediately if the bucket doesn't exist, otherwise retry waiting for bucket to exist.

(ctx context.Context, spec base.BucketSpec, failFast bool)

Source from the content-addressed store, hash-verified

340
341// ConnectToBucket opens a Couchbase connection and return a specific bucket. If failFast is set, fail immediately if the bucket doesn't exist, otherwise retry waiting for bucket to exist.
342func ConnectToBucket(ctx context.Context, spec base.BucketSpec, failFast bool) (base.Bucket, error) {
343 if failFast {
344 bucket, err := base.GetBucket(ctx, spec)
345 _, err = connectToBucketErrorHandling(ctx, spec, err)
346 return bucket, err
347 }
348
349 // start a retry loop to connect to the bucket backing off double the delay each time
350 worker := func() (bool, error, interface{}) {
351 bucket, err := base.GetBucket(ctx, spec)
352
353 // Retry if there was a non-fatal error
354 fatalError, newErr := connectToBucketErrorHandling(ctx, spec, err)
355 shouldRetry := newErr != nil && !fatalError
356
357 return shouldRetry, newErr, bucket
358 }
359
360 description := fmt.Sprintf("Attempt to connect to bucket : %v", spec.BucketName)
361 err, ibucket := base.RetryLoop(ctx, description, worker, base.GetNewDatabaseSleeperFunc())
362 if err != nil {
363 return nil, err
364 }
365
366 return ibucket.(base.Bucket), nil
367}
368
369// Creates a new DatabaseContext on a bucket. The bucket will be closed when this context closes.
370func NewDatabaseContext(ctx context.Context, dbName string, bucket base.Bucket, autoImport bool, options DatabaseContextOptions) (dbc *DatabaseContext, returnedError error) {

Callers 4

handleFlushMethod · 0.92
BenchmarkDatabaseFunction · 0.85
BenchmarkPutFunction · 0.85

Calls 4

GetBucketFunction · 0.92
RetryLoopFunction · 0.92

Tested by 2

BenchmarkDatabaseFunction · 0.68
BenchmarkPutFunction · 0.68