...

Source file src/k8s.io/kubernetes/pkg/scheduler/framework/parallelize/error_channel.go

Documentation: k8s.io/kubernetes/pkg/scheduler/framework/parallelize

     1  /*
     2  Copyright 2019 The Kubernetes 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  package parallelize
    18  
    19  import "context"
    20  
    21  // ErrorChannel supports non-blocking send and receive operation to capture error.
    22  // A maximum of one error is kept in the channel and the rest of the errors sent
    23  // are ignored, unless the existing error is received and the channel becomes empty
    24  // again.
    25  type ErrorChannel struct {
    26  	errCh chan error
    27  }
    28  
    29  // SendError sends an error without blocking the sender.
    30  func (e *ErrorChannel) SendError(err error) {
    31  	select {
    32  	case e.errCh <- err:
    33  	default:
    34  	}
    35  }
    36  
    37  // SendErrorWithCancel sends an error without blocking the sender and calls
    38  // cancel function.
    39  func (e *ErrorChannel) SendErrorWithCancel(err error, cancel context.CancelFunc) {
    40  	e.SendError(err)
    41  	cancel()
    42  }
    43  
    44  // ReceiveError receives an error from channel without blocking on the receiver.
    45  func (e *ErrorChannel) ReceiveError() error {
    46  	select {
    47  	case err := <-e.errCh:
    48  		return err
    49  	default:
    50  		return nil
    51  	}
    52  }
    53  
    54  // NewErrorChannel returns a new ErrorChannel.
    55  func NewErrorChannel() *ErrorChannel {
    56  	return &ErrorChannel{
    57  		errCh: make(chan error, 1),
    58  	}
    59  }
    60  

View as plain text