package pipline // 输入通道 type Inlet interface { In() chan<- interface{} } // 输出通道 type Outlet interface { Out() <-chan interface{} } // Source步骤 type Source interface { Outlet // 设置回调接口 Via(Flow) Flow } // Flow步骤 type Flow interface { Inlet Outlet Via(Flow) Flow To(Target) } // Target步骤 type Target interface { Inlet // 确认操作完毕 } type Pipeline struct { source Source flow Flow target Target } func Connect(source Source, flow Flow, target Target) *Pipeline { source.Via(flow) flow.To(target) return &Pipeline{ source: source, flow: flow, target: target, } } 在target中增加确认某条记录操作成功,并通知source
时间: 2024-03-05 07:49:18 浏览: 14
您可以在`Target`步骤中添加一个`Done()`方法,用于确认某条数据的操作已经完成,并通知`Source`步骤。修改后的代码如下:
```go
package pipeline
// 输入通道
type Inlet interface {
In() chan<- interface{}
}
// 输出通道
type Outlet interface {
Out() <-chan interface{}
}
// Source步骤
type Source interface {
Outlet
// 设置回调接口
Via(Flow) Flow
}
// Flow步骤
type Flow interface {
Inlet
Outlet
Via(Flow) Flow
To(Target)
}
// Target步骤
type Target interface {
Inlet
// 确认操作完毕,并通知Source
Done() bool
}
type Pipeline struct {
source Source
flow Flow
target Target
}
func Connect(source Source, flow Flow, target Target) *Pipeline {
source.Via(flow)
flow.To(target)
return &Pipeline{
source: source,
flow: flow,
target: target,
}
}
// 在Target步骤中添加Done()方法
func (p *Pipeline) Done() bool {
// 确认操作完成
p.source.Out()
// 通知Source
select {
case p.source.Out() <- struct{}{}:
return true
default:
return false
}
}
```
在`Target`步骤中,当一条数据的操作完成后,可以调用`Done()`方法,将结果返回给上一个步骤,并通知`Source`步骤。在通知`Source`步骤时,使用select语句和非阻塞通道操作,以避免在`Source`步骤中出现阻塞情况。如果成功通知了`Source`步骤,则返回`true`,否则返回`false`。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)