github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/mongo/readpref/options.go (about)

     1  // Copyright (C) MongoDB, Inc. 2017-present.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License"); you may
     4  // not use this file except in compliance with the License. You may obtain
     5  // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
     6  
     7  package readpref
     8  
     9  import (
    10  	"errors"
    11  	"time"
    12  
    13  	"go.mongodb.org/mongo-driver/tag"
    14  )
    15  
    16  // ErrInvalidTagSet indicates that an invalid set of tags was specified.
    17  var ErrInvalidTagSet = errors.New("an even number of tags must be specified")
    18  
    19  // Option configures a read preference
    20  type Option func(*ReadPref) error
    21  
    22  // WithMaxStaleness sets the maximum staleness a
    23  // server is allowed.
    24  func WithMaxStaleness(ms time.Duration) Option {
    25  	return func(rp *ReadPref) error {
    26  		rp.maxStaleness = ms
    27  		rp.maxStalenessSet = true
    28  		return nil
    29  	}
    30  }
    31  
    32  // WithTags specifies a single tag set used to match replica set members. If no members match the
    33  // tag set, read operations will return an error. To avoid errors if no members match the tag set, use
    34  // [WithTagSets] and include an empty tag set as the last tag set in the list.
    35  //
    36  // The last call to [WithTags] or [WithTagSets] overrides all previous calls to either method.
    37  //
    38  // For more information about read preference tags, see
    39  // https://www.mongodb.com/docs/manual/core/read-preference-tags/
    40  func WithTags(tags ...string) Option {
    41  	return func(rp *ReadPref) error {
    42  		length := len(tags)
    43  		if length < 2 || length%2 != 0 {
    44  			return ErrInvalidTagSet
    45  		}
    46  
    47  		tagset := make(tag.Set, 0, length/2)
    48  
    49  		for i := 1; i < length; i += 2 {
    50  			tagset = append(tagset, tag.Tag{Name: tags[i-1], Value: tags[i]})
    51  		}
    52  
    53  		return WithTagSets(tagset)(rp)
    54  	}
    55  }
    56  
    57  // WithTagSets specifies a list of tag sets used to match replica set members. If the list contains
    58  // multiple tag sets, members are matched against each tag set in succession until a match is found.
    59  // Once a match is found, the remaining tag sets are ignored. If no members match any of the tag
    60  // sets, the read operation returns with an error. To avoid an error if no members match any of the
    61  // tag sets, include an empty tag set as the last tag set in the list.
    62  //
    63  // The last call to [WithTags] or [WithTagSets] overrides all previous calls to either method.
    64  //
    65  // For more information about read preference tags, see
    66  // https://www.mongodb.com/docs/manual/core/read-preference-tags/
    67  func WithTagSets(tagSets ...tag.Set) Option {
    68  	return func(rp *ReadPref) error {
    69  		rp.tagSets = tagSets
    70  		return nil
    71  	}
    72  }
    73  
    74  // WithHedgeEnabled specifies whether or not hedged reads should be enabled in the server. This feature requires MongoDB
    75  // server version 4.4 or higher. For more information about hedged reads, see
    76  // https://www.mongodb.com/docs/manual/core/sharded-cluster-query-router/#mongos-hedged-reads. If not specified, the default
    77  // is to not send a value to the server, which will result in the server defaults being used.
    78  func WithHedgeEnabled(hedgeEnabled bool) Option {
    79  	return func(rp *ReadPref) error {
    80  		rp.hedgeEnabled = &hedgeEnabled
    81  		return nil
    82  	}
    83  }