go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/swarming/server/rbe/pool.go (about)

     1  // Copyright 2023 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package rbe
    16  
    17  import (
    18  	"context"
    19  	"sync/atomic"
    20  
    21  	"google.golang.org/grpc"
    22  
    23  	"go.chromium.org/luci/swarming/internal/remoteworkers"
    24  )
    25  
    26  type botsConnPool struct {
    27  	next    atomic.Uint32
    28  	clients []remoteworkers.BotsClient
    29  }
    30  
    31  // botsConnectionPool returns a BotsClient backed by a simple connection pool.
    32  func botsConnectionPool(cc []grpc.ClientConnInterface) remoteworkers.BotsClient {
    33  	clients := make([]remoteworkers.BotsClient, len(cc))
    34  	for i, conn := range cc {
    35  		clients[i] = remoteworkers.NewBotsClient(conn)
    36  	}
    37  	return &botsConnPool{clients: clients}
    38  }
    39  
    40  // get returns the next client to use.
    41  func (p *botsConnPool) get() remoteworkers.BotsClient {
    42  	return p.clients[p.next.Add(1)%uint32(len(p.clients))]
    43  }
    44  
    45  // CreateBotSession is a part of remoteworkers.BotsClient interface.
    46  func (p *botsConnPool) CreateBotSession(ctx context.Context, in *remoteworkers.CreateBotSessionRequest, opts ...grpc.CallOption) (*remoteworkers.BotSession, error) {
    47  	return p.get().CreateBotSession(ctx, in, opts...)
    48  }
    49  
    50  // UpdateBotSession  is a part of remoteworkers.BotsClient interface.
    51  func (p *botsConnPool) UpdateBotSession(ctx context.Context, in *remoteworkers.UpdateBotSessionRequest, opts ...grpc.CallOption) (*remoteworkers.BotSession, error) {
    52  	return p.get().UpdateBotSession(ctx, in, opts...)
    53  }