| 164 | } |
| 165 | |
| 166 | func (s *Source) open() error { |
| 167 | var err error |
| 168 | |
| 169 | ctx, cancel := context.WithTimeout(context.Background(), connTimeout) |
| 170 | defer cancel() |
| 171 | |
| 172 | opts := []*options.ClientOptions{ |
| 173 | options.Client().ApplyURI(s.connURL.String()), |
| 174 | } |
| 175 | |
| 176 | if s.session, err = mongo.Connect(ctx, opts...); err != nil { |
| 177 | return fmt.Errorf("mongo.Connect: %w", err) |
| 178 | } |
| 179 | |
| 180 | s.collections = map[string]*Collection{} |
| 181 | s.database = s.session.Database(settings.Database) |
| 182 | |
| 183 | // ping |
| 184 | if err = s.Ping(); err != nil { |
| 185 | return fmt.Errorf("Ping: %w", err) |
| 186 | } |
| 187 | |
| 188 | return nil |
| 189 | } |
| 190 | |
| 191 | // Close terminates the current database session. |
| 192 | func (s *Source) Close() error { |