github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/x/client/nextServer.go (about)

     1  // Copyright 2017-present Kirill Danshin and Gramework contributors
     2  // Copyright 2019-present Highload LTD (UK CN: 11893420)
     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  
    11  package client
    12  
    13  import (
    14  	"net/url"
    15  
    16  	"github.com/gramework/gramework"
    17  )
    18  
    19  func (client *Instance) nextServer() (*requestInfo, error) {
    20  	if len(client.conf.Addresses) == 0 {
    21  		return nil, ErrNoServerAvailable
    22  	}
    23  
    24  	for i := 0; i < len(client.conf.Addresses); i++ {
    25  		addr := client.conf.Addresses[client.balancer.next()]
    26  		hostURL, err := url.Parse(addr)
    27  		if err != nil {
    28  			gramework.Errorf("error while parsing host url: %s", err)
    29  			continue
    30  		}
    31  
    32  		return &requestInfo{
    33  			HostClient: client.getHostClient(hostURL),
    34  			Addr:       addr,
    35  		}, nil
    36  	}
    37  
    38  	return nil, ErrNoServerAvailable
    39  }