github.com/confluentinc/confluent-kafka-go@v1.9.2/kafka/time.go (about)

     1  /**
     2   * Copyright 2019 Confluent Inc.
     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 kafka
    18  
    19  import "C"
    20  
    21  import (
    22  	"context"
    23  	"time"
    24  )
    25  
    26  const (
    27  	cTimeoutInfinite = C.int(-1) // Blocks indefinitely until completion.
    28  	cTimeoutNoWait   = C.int(0)  // Returns immediately without blocking.
    29  )
    30  
    31  // cTimeoutFromContext returns the remaining time after which work done on behalf of this context
    32  // should be canceled, in milliseconds.
    33  //
    34  // If no deadline/timeout is set, or if the timeout does not fit in an int32, it returns
    35  // cTimeoutInfinite;
    36  // If there is no time left in this context, it returns cTimeoutNoWait.
    37  func cTimeoutFromContext(ctx context.Context) C.int {
    38  	if ctx == nil {
    39  		return cTimeoutInfinite
    40  	}
    41  	timeout, hasTimeout := timeout(ctx)
    42  	if !hasTimeout {
    43  		return cTimeoutInfinite
    44  	}
    45  	if timeout <= 0 {
    46  		return cTimeoutNoWait
    47  	}
    48  
    49  	timeoutMs := int64(timeout / time.Millisecond)
    50  	if int64(int32(timeoutMs)) < timeoutMs {
    51  		return cTimeoutInfinite
    52  	}
    53  
    54  	return C.int(timeoutMs)
    55  }