github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/vendor_skip/go.mongodb.org/mongo-driver/mongo/options/sessionoptions.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 options
     8  
     9  import (
    10  	"time"
    11  
    12  	"go.mongodb.org/mongo-driver/mongo/readconcern"
    13  	"go.mongodb.org/mongo-driver/mongo/readpref"
    14  	"go.mongodb.org/mongo-driver/mongo/writeconcern"
    15  )
    16  
    17  // DefaultCausalConsistency is the default value for the CausalConsistency option.
    18  var DefaultCausalConsistency = true
    19  
    20  // SessionOptions represents options that can be used to configure a Session.
    21  type SessionOptions struct {
    22  	// If true, causal consistency will be enabled for the session. This option cannot be set to true if Snapshot is
    23  	// set to true. The default value is true unless Snapshot is set to true. See
    24  	// https://www.mongodb.com/docs/manual/core/read-isolation-consistency-recency/#sessions for more information.
    25  	CausalConsistency *bool
    26  
    27  	// The default read concern for transactions started in the session. The default value is nil, which means that
    28  	// the read concern of the client used to start the session will be used.
    29  	DefaultReadConcern *readconcern.ReadConcern
    30  
    31  	// The default read preference for transactions started in the session. The default value is nil, which means that
    32  	// the read preference of the client used to start the session will be used.
    33  	DefaultReadPreference *readpref.ReadPref
    34  
    35  	// The default write concern for transactions started in the session. The default value is nil, which means that
    36  	// the write concern of the client used to start the session will be used.
    37  	DefaultWriteConcern *writeconcern.WriteConcern
    38  
    39  	// The default maximum amount of time that a CommitTransaction operation executed in the session can run on the
    40  	// server. The default value is nil, which means that that there is no time limit for execution.
    41  	//
    42  	// NOTE(benjirewis): DefaultMaxCommitTime will be deprecated in a future release. The more general Timeout option
    43  	// may be used in its place to control the amount of time that a single operation can run before returning an
    44  	// error. DefaultMaxCommitTime is ignored if Timeout is set on the client.
    45  	DefaultMaxCommitTime *time.Duration
    46  
    47  	// If true, all read operations performed with this session will be read from the same snapshot. This option cannot
    48  	// be set to true if CausalConsistency is set to true. Transactions and write operations are not allowed on
    49  	// snapshot sessions and will error. The default value is false.
    50  	Snapshot *bool
    51  }
    52  
    53  // Session creates a new SessionOptions instance.
    54  func Session() *SessionOptions {
    55  	return &SessionOptions{}
    56  }
    57  
    58  // SetCausalConsistency sets the value for the CausalConsistency field.
    59  func (s *SessionOptions) SetCausalConsistency(b bool) *SessionOptions {
    60  	s.CausalConsistency = &b
    61  	return s
    62  }
    63  
    64  // SetDefaultReadConcern sets the value for the DefaultReadConcern field.
    65  func (s *SessionOptions) SetDefaultReadConcern(rc *readconcern.ReadConcern) *SessionOptions {
    66  	s.DefaultReadConcern = rc
    67  	return s
    68  }
    69  
    70  // SetDefaultReadPreference sets the value for the DefaultReadPreference field.
    71  func (s *SessionOptions) SetDefaultReadPreference(rp *readpref.ReadPref) *SessionOptions {
    72  	s.DefaultReadPreference = rp
    73  	return s
    74  }
    75  
    76  // SetDefaultWriteConcern sets the value for the DefaultWriteConcern field.
    77  func (s *SessionOptions) SetDefaultWriteConcern(wc *writeconcern.WriteConcern) *SessionOptions {
    78  	s.DefaultWriteConcern = wc
    79  	return s
    80  }
    81  
    82  // SetDefaultMaxCommitTime sets the value for the DefaultMaxCommitTime field.
    83  //
    84  // NOTE(benjirewis): DefaultMaxCommitTime will be deprecated in a future release. The more
    85  // general Timeout option may be used in its place to control the amount of time that a
    86  // single operation can run before returning an error. DefaultMaxCommitTime is ignored if
    87  // Timeout is set on the client.
    88  func (s *SessionOptions) SetDefaultMaxCommitTime(mct *time.Duration) *SessionOptions {
    89  	s.DefaultMaxCommitTime = mct
    90  	return s
    91  }
    92  
    93  // SetSnapshot sets the value for the Snapshot field.
    94  func (s *SessionOptions) SetSnapshot(b bool) *SessionOptions {
    95  	s.Snapshot = &b
    96  	return s
    97  }
    98  
    99  // MergeSessionOptions combines the given SessionOptions instances into a single SessionOptions in a last-one-wins
   100  // fashion.
   101  //
   102  // Deprecated: Merging options structs will not be supported in Go Driver 2.0. Users should create a
   103  // single options struct instead.
   104  func MergeSessionOptions(opts ...*SessionOptions) *SessionOptions {
   105  	s := Session()
   106  	for _, opt := range opts {
   107  		if opt == nil {
   108  			continue
   109  		}
   110  		if opt.CausalConsistency != nil {
   111  			s.CausalConsistency = opt.CausalConsistency
   112  		}
   113  		if opt.DefaultReadConcern != nil {
   114  			s.DefaultReadConcern = opt.DefaultReadConcern
   115  		}
   116  		if opt.DefaultReadPreference != nil {
   117  			s.DefaultReadPreference = opt.DefaultReadPreference
   118  		}
   119  		if opt.DefaultWriteConcern != nil {
   120  			s.DefaultWriteConcern = opt.DefaultWriteConcern
   121  		}
   122  		if opt.DefaultMaxCommitTime != nil {
   123  			s.DefaultMaxCommitTime = opt.DefaultMaxCommitTime
   124  		}
   125  		if opt.Snapshot != nil {
   126  			s.Snapshot = opt.Snapshot
   127  		}
   128  	}
   129  	if s.CausalConsistency == nil && (s.Snapshot == nil || !*s.Snapshot) {
   130  		s.CausalConsistency = &DefaultCausalConsistency
   131  	}
   132  
   133  	return s
   134  }