LogsChannel 返回一个把 xray 历史 + 实时日志统一推出的只读通道。 ctx 取消时通道会被关闭。底层基于 coremanager.Manager 的 Subscribe/Unsubscribe。 返回的 chan 会先发送当前缓冲区的全部历史行,然后转入实时订阅模式。
(ctx context.Context)
| 110 | // |
| 111 | // 返回的 chan 会先发送当前缓冲区的全部历史行,然后转入实时订阅模式。 |
| 112 | func (a *API) LogsChannel(ctx context.Context) <-chan string { |
| 113 | out := make(chan string, 64) |
| 114 | mgr := a.activeManager() |
| 115 | go func() { |
| 116 | defer close(out) |
| 117 | for _, line := range mgr.Logs() { |
| 118 | select { |
| 119 | case out <- line: |
| 120 | case <-ctx.Done(): |
| 121 | return |
| 122 | } |
| 123 | } |
| 124 | id, ch := mgr.Subscribe() |
| 125 | defer mgr.Unsubscribe(id) |
| 126 | for { |
| 127 | select { |
| 128 | case <-ctx.Done(): |
| 129 | return |
| 130 | case line, ok := <-ch: |
| 131 | if !ok { |
| 132 | return |
| 133 | } |
| 134 | select { |
| 135 | case out <- line: |
| 136 | case <-ctx.Done(): |
| 137 | return |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | }() |
| 142 | return out |
| 143 | } |
| 144 | |
| 145 | // DoStart 启动核心,返回最新 Status。 |
| 146 | func (a *API) DoStart(config, core string) (coremanager.Status, error) { |
no test coverage detected