github.com/gramework/gramework@v1.8.1-0.20231027140105-82555c9057f5/x/client/rangeBalancer.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  	"sync/atomic"
    15  )
    16  
    17  func newRangeBalancer() *rangeBalancer {
    18  	var curr, total int64
    19  	return &rangeBalancer{
    20  		curr:  &curr,
    21  		total: &total,
    22  	}
    23  }
    24  
    25  func (i *rangeBalancer) next() int64 {
    26  	if atomic.LoadInt64(i.curr) == atomic.LoadInt64(i.total) {
    27  		return atomic.SwapInt64(i.curr, 0)
    28  	}
    29  
    30  	return atomic.AddInt64(i.curr, 1)
    31  }