You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

sessionoptions.go 3.3 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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. package options
  7. import (
  8. "time"
  9. "go.mongodb.org/mongo-driver/mongo/readconcern"
  10. "go.mongodb.org/mongo-driver/mongo/readpref"
  11. "go.mongodb.org/mongo-driver/mongo/writeconcern"
  12. )
  13. // DefaultCausalConsistency is the default value for the CausalConsistency option.
  14. var DefaultCausalConsistency = true
  15. // SessionOptions represents all possible options for creating a new session.
  16. type SessionOptions struct {
  17. CausalConsistency *bool // Specifies if reads should be causally consistent. Defaults to true.
  18. DefaultReadConcern *readconcern.ReadConcern // The default read concern for transactions started in the session.
  19. DefaultReadPreference *readpref.ReadPref // The default read preference for transactions started in the session.
  20. DefaultWriteConcern *writeconcern.WriteConcern // The default write concern for transactions started in the session.
  21. DefaultMaxCommitTime *time.Duration // The default max commit time for transactions started in the session.
  22. }
  23. // Session creates a new *SessionOptions
  24. func Session() *SessionOptions {
  25. return &SessionOptions{
  26. CausalConsistency: &DefaultCausalConsistency,
  27. }
  28. }
  29. // SetCausalConsistency specifies if a session should be causally consistent. Defaults to true.
  30. func (s *SessionOptions) SetCausalConsistency(b bool) *SessionOptions {
  31. s.CausalConsistency = &b
  32. return s
  33. }
  34. // SetDefaultReadConcern sets the default read concern for transactions started in a session.
  35. func (s *SessionOptions) SetDefaultReadConcern(rc *readconcern.ReadConcern) *SessionOptions {
  36. s.DefaultReadConcern = rc
  37. return s
  38. }
  39. // SetDefaultReadPreference sets the default read preference for transactions started in a session.
  40. func (s *SessionOptions) SetDefaultReadPreference(rp *readpref.ReadPref) *SessionOptions {
  41. s.DefaultReadPreference = rp
  42. return s
  43. }
  44. // SetDefaultWriteConcern sets the default write concern for transactions started in a session.
  45. func (s *SessionOptions) SetDefaultWriteConcern(wc *writeconcern.WriteConcern) *SessionOptions {
  46. s.DefaultWriteConcern = wc
  47. return s
  48. }
  49. // SetDefaultMaxCommitTime sets the default max commit time for transactions started in a session.
  50. func (s *SessionOptions) SetDefaultMaxCommitTime(mct *time.Duration) *SessionOptions {
  51. s.DefaultMaxCommitTime = mct
  52. return s
  53. }
  54. // MergeSessionOptions combines the given *SessionOptions into a single *SessionOptions in a last one wins fashion.
  55. func MergeSessionOptions(opts ...*SessionOptions) *SessionOptions {
  56. s := Session()
  57. for _, opt := range opts {
  58. if opt == nil {
  59. continue
  60. }
  61. if opt.CausalConsistency != nil {
  62. s.CausalConsistency = opt.CausalConsistency
  63. }
  64. if opt.DefaultReadConcern != nil {
  65. s.DefaultReadConcern = opt.DefaultReadConcern
  66. }
  67. if opt.DefaultReadPreference != nil {
  68. s.DefaultReadPreference = opt.DefaultReadPreference
  69. }
  70. if opt.DefaultWriteConcern != nil {
  71. s.DefaultWriteConcern = opt.DefaultWriteConcern
  72. }
  73. if opt.DefaultMaxCommitTime != nil {
  74. s.DefaultMaxCommitTime = opt.DefaultMaxCommitTime
  75. }
  76. }
  77. return s
  78. }