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)
| 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. |
| 342 | func 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. |
| 370 | func NewDatabaseContext(ctx context.Context, dbName string, bucket base.Bucket, autoImport bool, options DatabaseContextOptions) (dbc *DatabaseContext, returnedError error) { |