github.com/dubbogo/gost@v1.14.0/container/gxsync/batcher.go (about)

     1  /*
     2  Copyright 2019 The Vitess Authors.
     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   * Licensed to the Apache Software Foundation (ASF) under one or more
    19   * contributor license agreements.  See the NOTICE file distributed with
    20   * this work for additional information regarding copyright ownership.
    21   * The ASF licenses this file to You under the Apache License, Version 2.0
    22   * (the "License"); you may not use this file except in compliance with
    23   * the License.  You may obtain a copy of the License at
    24   *
    25   *     http://www.apache.org/licenses/LICENSE-2.0
    26   *
    27   * Unless required by applicable law or agreed to in writing, software
    28   * distributed under the License is distributed on an "AS IS" BASIS,
    29   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    30   * See the License for the specific language governing permissions and
    31   * limitations under the License.
    32   */
    33  
    34  package gxsync
    35  
    36  import (
    37  	"time"
    38  )
    39  
    40  import (
    41  	"go.uber.org/atomic"
    42  )
    43  
    44  // Batcher delays concurrent operations for a configurable interval in order to
    45  // batch them up or otherwise clock their operation to run concurrently.
    46  //
    47  // It is implemented as a channel of int32s. Each waiter blocks on the channel
    48  // from which it gets a sequentially increasing batch ID when the timer elapses.
    49  //
    50  // Hence a waiter is delayed for at most the batch interval.
    51  type Batcher struct {
    52  	interval time.Duration
    53  	queue    chan int
    54  	waiters  *atomic.Int32
    55  	nextID   *atomic.Int32
    56  	after    func(time.Duration) <-chan time.Time
    57  }
    58  
    59  // NewBatcher returns a new Batcher
    60  func NewBatcher(interval time.Duration) *Batcher {
    61  	return &Batcher{
    62  		interval: interval,
    63  		queue:    make(chan int),
    64  		waiters:  atomic.NewInt32(0),
    65  		nextID:   atomic.NewInt32(0),
    66  		after:    time.After,
    67  	}
    68  }
    69  
    70  // newBatcherForTest returns a Batcher for testing where time.After can
    71  // be replaced by a fake alternative.
    72  func newBatcherForTest(interval time.Duration, after func(time.Duration) <-chan time.Time) *Batcher {
    73  	return &Batcher{
    74  		interval: interval,
    75  		queue:    make(chan int),
    76  		waiters:  atomic.NewInt32(0),
    77  		nextID:   atomic.NewInt32(0),
    78  		after:    after,
    79  	}
    80  }
    81  
    82  // Wait adds a new waiter to the queue and blocks until the next batch
    83  func (b *Batcher) Wait() int {
    84  	numWaiters := b.waiters.Add(1)
    85  	if numWaiters == 1 {
    86  		b.newBatch()
    87  	}
    88  	return <-b.queue
    89  }
    90  
    91  // newBatch starts a new batch
    92  func (b *Batcher) newBatch() {
    93  	go func() {
    94  		<-b.after(b.interval)
    95  
    96  		id := b.nextID.Add(1)
    97  
    98  		// Make sure to atomically reset the number of waiters to make
    99  		// sure that all incoming requests either make it into the
   100  		// current batch or the next one.
   101  		waiters := b.waiters.Load()
   102  		for !b.waiters.CAS(waiters, 0) {
   103  			waiters = b.waiters.Load()
   104  		}
   105  
   106  		for i := int32(0); i < waiters; i++ {
   107  			b.queue <- int(id)
   108  		}
   109  	}()
   110  }