github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/transports/fast/dialer.go (about)

     1  /*
     2   * Copyright 2023 Wang Min Xiang
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   * 	http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   *
    16   */
    17  
    18  package fast
    19  
    20  import (
    21  	"github.com/aacfactory/errors"
    22  	"github.com/aacfactory/fns/commons/bytex"
    23  	"github.com/aacfactory/fns/commons/caches/lru"
    24  	"github.com/aacfactory/fns/transports"
    25  	"golang.org/x/sync/singleflight"
    26  	"time"
    27  )
    28  
    29  func NewDialer(opts ClientConfig) (dialer *Dialer, err error) {
    30  	cacheSize := opts.Dialer.CacheSize
    31  	if cacheSize < 1 {
    32  		cacheSize = 64
    33  	}
    34  	cacheSeconds := opts.Dialer.ExpireSeconds
    35  	if cacheSeconds < 1 {
    36  		cacheSeconds = 24 * 60 * 60
    37  	}
    38  
    39  	dialer = &Dialer{
    40  		config: opts,
    41  		group:  &singleflight.Group{},
    42  		clients: lru.NewWithExpire[string, transports.Client](cacheSize, time.Duration(cacheSeconds)*time.Second, func(key string, value transports.Client) {
    43  			value.Close()
    44  		}),
    45  	}
    46  	return
    47  }
    48  
    49  type Dialer struct {
    50  	config  ClientConfig
    51  	group   *singleflight.Group
    52  	clients *lru.LRU[string, transports.Client]
    53  }
    54  
    55  func (dialer *Dialer) Dial(addressBytes []byte) (client transports.Client, err error) {
    56  	address := bytex.ToString(addressBytes)
    57  	cc, doErr, _ := dialer.group.Do(address, func() (clients interface{}, err error) {
    58  		hosted, has := dialer.clients.Get(address)
    59  		if has {
    60  			clients = hosted
    61  			return
    62  		}
    63  		hosted, err = dialer.createClient(address)
    64  		if err != nil {
    65  			return
    66  		}
    67  		dialer.clients.Add(address, hosted)
    68  		clients = hosted
    69  		return
    70  	})
    71  	dialer.group.Forget(address)
    72  	if doErr != nil {
    73  		err = errors.Warning("fast: dial failed").WithMeta("address", address).WithCause(doErr)
    74  		return
    75  	}
    76  	client = cc.(*Client)
    77  	return
    78  }
    79  
    80  func (dialer *Dialer) createClient(address string) (client transports.Client, err error) {
    81  	client, err = NewClient(address, dialer.config)
    82  	if err != nil {
    83  		return
    84  	}
    85  	return
    86  }
    87  
    88  func (dialer *Dialer) Close() {
    89  	dialer.clients.Purge()
    90  }