github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/client/client.go (about)

     1  // Copyright (c) 2016 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package client
    22  
    23  import (
    24  	"sync"
    25  )
    26  
    27  type client struct {
    28  	sync.Mutex
    29  
    30  	opts         Options
    31  	asyncOpts    []Options
    32  	newSessionFn newReplicatedSessionFn
    33  	session      AdminSession // default cached session
    34  }
    35  
    36  // type newReplicatedSessionFn func(Options) (replicatedSession, error)
    37  type newReplicatedSessionFn func(Options, []Options, ...replicatedSessionOption) (clientSession, error)
    38  
    39  // NewClient creates a new client
    40  func NewClient(opts Options, asyncOpts ...Options) (Client, error) {
    41  	return newClient(opts, asyncOpts...)
    42  }
    43  
    44  // NewAdminClient creates a new administrative client
    45  func NewAdminClient(opts AdminOptions, asyncOpts ...Options) (AdminClient, error) {
    46  	return newClient(opts, asyncOpts...)
    47  }
    48  
    49  func newClient(opts Options, asyncOpts ...Options) (*client, error) {
    50  	if err := opts.Validate(); err != nil {
    51  		return nil, err
    52  	}
    53  	return &client{opts: opts, asyncOpts: asyncOpts, newSessionFn: newReplicatedSession}, nil
    54  }
    55  
    56  func (c *client) newSession(opts Options) (AdminSession, error) {
    57  	session, err := c.newSessionFn(opts, c.asyncOpts)
    58  	if err != nil {
    59  		return nil, err
    60  	}
    61  	if err := session.Open(); err != nil {
    62  		return nil, err
    63  	}
    64  	return session, nil
    65  }
    66  
    67  func (c *client) defaultSession() (AdminSession, error) {
    68  	c.Lock()
    69  	if c.session != nil {
    70  		session := c.session
    71  		c.Unlock()
    72  		return session, nil
    73  	}
    74  	c.Unlock()
    75  
    76  	session, err := c.newSession(c.opts)
    77  	if err != nil {
    78  		return nil, err
    79  	}
    80  
    81  	c.Lock()
    82  	if c.session != nil {
    83  		session := c.session
    84  		c.Unlock()
    85  		return session, nil
    86  	}
    87  	c.session = session
    88  	c.Unlock()
    89  
    90  	return session, nil
    91  }
    92  
    93  func (c *client) Options() Options {
    94  	return c.opts
    95  }
    96  
    97  func (c *client) NewSession() (Session, error) {
    98  	return c.newSession(c.opts)
    99  }
   100  
   101  func (c *client) NewSessionWithOptions(opts Options) (Session, error) {
   102  	return c.newSession(opts)
   103  }
   104  
   105  func (c *client) DefaultSession() (Session, error) {
   106  	return c.defaultSession()
   107  }
   108  
   109  func (c *client) NewAdminSession() (AdminSession, error) {
   110  	return c.newSession(c.opts)
   111  }
   112  
   113  func (c *client) DefaultAdminSession() (AdminSession, error) {
   114  	return c.defaultSession()
   115  }
   116  
   117  func (c *client) DefaultSessionActive() bool {
   118  	c.Lock()
   119  	defer c.Unlock()
   120  	return c.session != nil
   121  }