2021-11-10 23:56:52 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-04-19 10:09:43 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-11-10 23:56:52 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-04-19 10:09:43 +08:00
|
|
|
//
|
2021-11-10 23:56:52 +08:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2021-04-19 10:09:43 +08:00
|
|
|
|
2021-06-22 14:40:07 +08:00
|
|
|
package proxy
|
2020-11-17 20:00:23 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2021-03-05 10:15:27 +08:00
|
|
|
"errors"
|
2020-11-17 20:00:23 +08:00
|
|
|
)
|
|
|
|
|
2021-10-01 15:37:18 +08:00
|
|
|
// Condition defines the interface of variable condition.
|
2020-11-17 20:00:23 +08:00
|
|
|
type Condition interface {
|
|
|
|
WaitToFinish() error
|
|
|
|
Notify(err error)
|
2021-02-03 17:30:10 +08:00
|
|
|
Ctx() context.Context
|
2020-11-17 20:00:23 +08:00
|
|
|
}
|
|
|
|
|
2021-10-09 21:01:18 +08:00
|
|
|
// make sure interface implementation
|
|
|
|
var _ Condition = (*TaskCondition)(nil)
|
|
|
|
|
|
|
|
// TaskCondition implements Condition interface for tasks
|
2020-11-17 20:00:23 +08:00
|
|
|
type TaskCondition struct {
|
|
|
|
done chan error
|
|
|
|
ctx context.Context
|
|
|
|
}
|
|
|
|
|
2021-10-09 21:01:18 +08:00
|
|
|
// WaitToFinish waits until the TaskCondition is notified or context done or canceled
|
2020-11-17 20:00:23 +08:00
|
|
|
func (tc *TaskCondition) WaitToFinish() error {
|
2022-06-15 16:32:10 +08:00
|
|
|
select {
|
|
|
|
case <-tc.ctx.Done():
|
|
|
|
return errors.New("proxy TaskCondition context Done")
|
|
|
|
case err := <-tc.done:
|
|
|
|
return err
|
2020-11-17 20:00:23 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-09 21:01:18 +08:00
|
|
|
// Notify sends a signal into the done channel
|
2020-11-17 20:00:23 +08:00
|
|
|
func (tc *TaskCondition) Notify(err error) {
|
|
|
|
tc.done <- err
|
|
|
|
}
|
|
|
|
|
2021-10-09 21:01:18 +08:00
|
|
|
// Ctx returns internal context
|
2021-02-03 17:30:10 +08:00
|
|
|
func (tc *TaskCondition) Ctx() context.Context {
|
|
|
|
return tc.ctx
|
|
|
|
}
|
|
|
|
|
2021-10-09 21:01:18 +08:00
|
|
|
// NewTaskCondition creates a TaskCondition with provided context
|
2020-11-17 20:00:23 +08:00
|
|
|
func NewTaskCondition(ctx context.Context) *TaskCondition {
|
|
|
|
return &TaskCondition{
|
2021-03-19 17:50:26 +08:00
|
|
|
done: make(chan error, 1),
|
2020-11-17 20:00:23 +08:00
|
|
|
ctx: ctx,
|
|
|
|
}
|
|
|
|
}
|