go语言-context的基本使用

1. 什么是 Context?

Go 1.7 标准库引入 context,中文译作“上下文”,准确说它是 goroutine 的上下文,包含 goroutine 的运行状态、环境、现场等信息。

context 主要用来在 goroutine 之间传递上下文信息,包括:取消信号、超时时间、截止时间、k-v 等。

Context,也叫上下文,它的接口定义如下

type Context interface {
    Deadline() (deadline time.Time, ok bool)
    Done() <-chan struct{}
    Err() error
    Value(key interface{}) interface{}
}

可以看到 Context 接口共有 4 个方法

  • Deadline:返回的第一个值是截止时间,到了这个时间点,Context 会自动触发 Cancel 动作。返回的第二个值是 一个布尔值,true 表示设置了截止时间,false 表示没有设置截止时间,如果没有设置截止时间,就要手动调用 cancel 函数取消 Context。

  • Done:返回一个只读的通道(只有在被cancel后才会返回),类型为 struct{}。当这个通道可读时,意味着parent context已经发起了取消请求,根据这个信号,开发者就可以做一些清理动作,退出goroutine。

  • Err:返回 context 被 cancel 的原因。

  • Value:返回被绑定到 Context 的值,是一个键值对,所以要通过一个Key才可以获取对应的值,这个值一般是线程安全的。

2. 为什么要用Context

  • 用于控制goroutine的结束,但它解决的并不是 能不能 的问题,而是解决 更好用 的问题。

2.1. 当不用Context时,利用channel+select来主动让goroutine停止

示例1


package main

import (
	"fmt"
	"time"
)

/*
1. 利用channel控制goroutine的停止
*/

func main() {

	stopChan := make(chan bool)

	go func() {
		for {
			select {
			case <-stopChan:
				fmt.Println("goroutin1 exit.")
				return
			default:
				fmt.Println("goroutin1 sleep 1s, keep going.")
				time.Sleep(time.Second * 2)
			}
		}
	}()

	go func() {
		for {
			select {
			case <-stopChan:
				fmt.Println("goroutin2 exit.")
				return
			default:
				fmt.Println("goroutin2 sleep 1s, keep going.")
				time.Sleep(time.Second * 3)
			}
		}
	}()

	time.Sleep(10 * time.Second)
	fmt.Println("10s 时间到了,主进程需要退出了.")
	// 发送信号让goroute1结束
	stopChan <- true

	// 发送信号让goroute2结束
	stopChan <- true
	time.Sleep(5 * time.Second)
}



示例2

package main

import (
	"fmt"
	"time"
)

/*

1. 利用关闭channel的方法,让2个goroutine同时结束
*/

func main() {

	stopChan := make(chan bool)

	go func() {
		for {
			select {
			case <-stopChan:
				fmt.Println("goroutin1 exit.")
				return
			default:
				fmt.Println("goroutin1 sleep 1s, keep going.")
				time.Sleep(time.Second * 2)
			}
		}
	}()

	go func() {
		for {
			select {
			case <-stopChan:
				fmt.Println("goroutin2 exit.")
				return
			default:
				fmt.Println("goroutin2 sleep 1s, keep going.")
				time.Sleep(time.Second * 3)
			}
		}
	}()

	time.Sleep(10 * time.Second)
	fmt.Println("10s 时间到了,主进程需要退出了.")
	// 利用关闭channel的方法,让2个goroutine同时结束
	close(stopChan)

	time.Sleep(5 * time.Second)
}


2.2 使用context来主动让goroutine停止

先ctx, cancel := context.WithCancel(context.Background()) 创建一个ctx实例
再利用cancel()函数执行控制goroutine的停止

package main

import (
	"context"
	"fmt"
	"time"
)

/*
// 利用context,手动让2个goroutine同时结束[是不是更简单?]
*/
func main() {

	ctx, cancel := context.WithCancel(context.Background())

	go func() {
		for {
			select {
			case <-ctx.Done():
				fmt.Println("goroutin1 exit.")
				return
			default:
				fmt.Println("goroutin1 sleep 1s, keep going.")
				time.Sleep(time.Second * 1)
			}
		}
	}()

	go func() {
		for {
			select {
			case <-ctx.Done():
				fmt.Println("goroutin2 exit.")
				return
			default:
				fmt.Println("goroutin2 sleep 1s, keep going.")
				time.Sleep(time.Second * 1)
			}
		}
	}()
	time.Sleep(10 * time.Second)
	fmt.Println("10s 时间到了,goroutine需要退出了.")
	
	// 利用context的方法,手动让2个goroutine同时结束
	cancel()

	time.Sleep(5 * time.Second)
}


2.3 使用context实现goroutine的超时控制

  • 使用场景:让goroutine执行一个任务,如果在指定时间内没有完成,这利用context的WithTimeout()主动让goroutine退出
package main

import (
    "fmt"
    "time"
    "context"
)

// 场景: 如果你需要对一个用协程启动的函数做超时控制,可以用context来完成goroutine的控制

func main()  {
    // 设置一个用于超时控制的context ctx, ctx作为参数可以用来作为协程的超时控制
    ctx,cancel := context.WithTimeout(context.Background(),10 * time.Second)
    defer cancel()

    // ctx作为参数传递给需要做超时控制的函数

    go Monitor(ctx)

    time.Sleep(20 * time.Second)
}

func Monitor(ctx context.Context)  {
    for {

        select {
        // 如果context 超时,ctx.Done()就会返回一个空接口 struct{}
        case <- ctx.Done():
            // 如果超时时间到了,就退出循环
            fmt.Println(ctx.Err())
            return
        // 如果没有超时,打印输出后继续循环
        default:
            time.Sleep(1*time.Second)
            fmt.Println("monitor")
        }


    }
}

2.4 利用context向goroutine传递参数

  • 除了超时控制与主动停止goroutine,还有可以通过Context传递上下文变量给其他协程。这样可以避免在协程之间传递大量的变量,代码更整洁可维护。下面的例子通过WithValue传递给协程一个变量,并且通过channel在协程之间通信。
package main

import (
	"context"
	"fmt"
	"time"
)

func main() {

	// 为ctx设置一个key-value
	ctx := context.Background()
	ctx = context.WithValue(ctx, "hello", "world")
	x := ctx.Value("hello")
	fmt.Println("x=", x) // world

	// 将key-vluae值传递到goroutine
	go work(ctx)

	time.Sleep(3 * time.Second)

}
func work(ctx context.Context) {
	fmt.Println("do worker.")
	fmt.Println("hello=", ctx.Value("hello")) // world,利用context传递key-value
	// 继续传递到下层goroutine
	go subwork(ctx)
}

func subwork(ctx context.Context) {
	fmt.Println("do subwork.")
	fmt.Println("hello=", ctx.Value("hello")) // world,利用context传递key-value到更进一层
}

程序输出:

x= world
do worker.
world
do subwork.
world
### 如何在Go语言使用 Confluent-Kafka-Go 库 #### 安装Confluent-Kafka-Go库 为了能够顺利地使用`confluent-kafka-go`库,首先需要确保已经安装了该库以及其依赖项。可以通过以下命令来安装这个包: ```bash go get github.com/confluentinc/confluent-kafka-go/kafka ``` 需要注意的是,由于此客户端依赖于C++版本的librdkafka库,因此还需要单独安装它。对于不同的操作系统有不同的安装方法[^2]。 #### 创建Kafka生产者实例并发送消息 下面是一个简单的例子展示怎样创建一个Kafka生产者的实例并向指定主题发布一条消息: ```go package main import ( "fmt" "log" "github.com/confluentinc/confluent-kafka-go/kafka" ) func main() { p, err := kafka.NewProducer(&kafka.ConfigMap{ "bootstrap.servers": "localhost:9092", }) if err != nil { log.Fatalf("Failed to create producer: %s", err) } defer p.Close() go func() { for e := range p.Events() { switch ev := e.(type) { case *kafka.Message: if ev.TopicPartition.Error != nil { fmt.Printf("Delivery failed: %v\n", ev.TopicPartition) } else { fmt.Printf("Delivered message to %v\n", ev.TopicPartition) } default: fmt.Println(ev) } } }() topic := "test-topic" value := []byte("Hello from confluent-kafka-go!") err = p.Produce(&kafka.Message{ TopicPartition: kafka.TopicPartition{Topic: &topic, Partition: kafka.PartitionAny}, Value: value, }, nil) if err != nil { log.Printf("Produce error: %v", err) } p.Flush(15 * 1000) } ``` 这段代码展示了如何配置连接到本地Kafka集群(`localhost:9092`)的一个生产者,并向名为`test-topic`的主题发送了一条字符串形式的消息。此外还设置了事件监听器用于处理消息传递的结果反馈。 #### 构建Kafka消费者接收数据 这里给出一段用来订阅特定主题并将接收到的数据打印出来的消费端程序片段: ```go package main import ( "context" "fmt" "os" "os/signal" "time" "github.com/confluentinc/confluent-kafka-go/kafka" ) func main() { consumer, err := kafka.NewConsumer(&kafka.ConfigMap{ "bootstrap.servers": "localhost:9092", "group.id": "myGroup", "auto.offset.reset": "earliest", }) if err != nil { panic(err) } topics := []string{"test-topic"} consumer.SubscribeTopics(topics, nil) ctx, cancel := context.WithCancel(context.Background()) signals := make(chan os.Signal, 1) signal.Notify(signals, os.Interrupt) go func() { sig := <-signals fmt.Printf("\nCaught signal %v: terminating...\n", sig) cancel() }() run := true for run { select { case <-ctx.Done(): run = false default: ev := consumer.Poll(100) if ev == nil { continue } switch e := ev.(type) { case *kafka.Message: fmt.Printf("Consumed message on topic-partition %v/%d at offset %v:\n%s\n", e.TopicPartition.Topic, e.TopicPartition.Partition, e.TopicPartition.Offset, string(e.Value)) case kafka.AssignedPartitions: fmt.Fprintf(os.Stdout, "Assigned Partitions: %+v\n", e.Partitions) case kafka.RevokedPartitions: fmt.Fprintf(os.Stdout, "Revoked Partitions: %+v\n", e.Partitions) case kafka.Error: fmt.Fprintf(os.Stderr, "Error: %v\n", e) default: fmt.Printf("Ignored %v\n", e) } } time.Sleep(time.Second / 3) } consumer.Close() } ``` 上述脚本实现了基本的功能——启动后会持续轮询来自所关注主题的新消息直到捕获到中断信号为止;期间任何到达的信息都会被立即显示出来。同时支持动态调整分配给当前消费者的分区列表变化情况的通知机制。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

SRExianxian

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值