github.com/aacfactory/fns@v1.2.86-0.20240310083819-80d667fc0a17/transports/standard/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 standard
    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  	dialer = &Dialer{
    39  		config: opts,
    40  		group:  &singleflight.Group{},
    41  		clients: lru.NewWithExpire[string, transports.Client](cacheSize, time.Duration(cacheSeconds)*time.Second, func(key string, value transports.Client) {
    42  			value.Close()
    43  		}),
    44  	}
    45  	return
    46  }
    47  
    48  type Dialer struct {
    49  	config  *ClientConfig
    50  	group   *singleflight.Group
    51  	clients *lru.LRU[string, transports.Client]
    52  }
    53  
    54  func (dialer *Dialer) Dial(addressBytes []byte) (client transports.Client, err error) {
    55  	address := bytex.ToString(addressBytes)
    56  	cc, doErr, _ := dialer.group.Do(address, func() (clients interface{}, err error) {
    57  		hosted, has := dialer.clients.Get(address)
    58  		if has {
    59  			clients = hosted
    60  			return
    61  		}
    62  		hosted, err = dialer.createClient(address)
    63  		if err != nil {
    64  			return
    65  		}
    66  		dialer.clients.Add(address, hosted)
    67  		clients = hosted
    68  		return
    69  	})
    70  	dialer.group.Forget(address)
    71  	if doErr != nil {
    72  		err = errors.Warning("http2: dial failed").WithMeta("address", address).WithCause(doErr)
    73  		return
    74  	}
    75  	client = cc.(*Client)
    76  	return
    77  }
    78  
    79  func (dialer *Dialer) createClient(address string) (client transports.Client, err error) {
    80  	client, err = NewClient(address, dialer.config)
    81  	if err != nil {
    82  		return
    83  	}
    84  	return
    85  }
    86  
    87  func (dialer *Dialer) Close() {
    88  	dialer.clients.Purge()
    89  }