Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.

Commit e8a6514

Browse files
authored
Merge pull request #1364 from antsystem/feat/update-clientwriter-interface
update interface of ClientWriter
2 parents f1bd6e8 + 872c6e2 commit e8a6514

File tree

3 files changed

+116
-2
lines changed

3 files changed

+116
-2
lines changed

dfget/corev2/basic/interface.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,19 @@ type RangeRequest interface {
3232
// Extra gets the extra info.
3333
Extra() interface{}
3434
}
35+
36+
// NotifyResult defines the result of notify.
37+
type NotifyResult interface {
38+
Success() bool
39+
Error() error
40+
Data() interface{}
41+
}
42+
43+
// Notify defines how to notify asynchronous call if finished and get the result.
44+
type Notify interface {
45+
// Done returns a channel that's closed when work done.
46+
Done() <-chan struct{}
47+
48+
// Result returns the NotifyResult and only valid after Done channel is closed.
49+
Result() NotifyResult
50+
}

dfget/corev2/clientwriter/client_writer.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,24 @@
1717
package clientwriter
1818

1919
import (
20+
"context"
21+
"io"
22+
23+
"github.com/dragonflyoss/Dragonfly/dfget/corev2/basic"
2024
"github.com/dragonflyoss/Dragonfly/pkg/protocol"
2125
)
2226

23-
// ClientStream defines how to organize distribution data for range request.
27+
// ClientWriter defines how to organize distribution data for range request.
2428
// An instance binds to a range request.
2529
// It may receive a lot of distribution data.
26-
// Developer could add a io.WriteCloser in constructor of instance, and the ClientWriter will
30+
// Developer could call Run() to start the loop in which ClientWriter will
2731
// write request data to io.Writer.
2832
type ClientWriter interface {
2933
// WriteData writes the distribution data from other peers, it may be called more times.
3034
PutData(data protocol.DistributionData) error
35+
36+
// Run starts the loop and ClientWriter will write request data to wc.
37+
// Run should only be called once.
38+
// caller gets the result by Notify.
39+
Run(ctx context.Context, wc io.WriteCloser) (basic.Notify, error)
3140
}

dfget/corev2/common/notify.go

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright The Dragonfly Authors.
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 common
18+
19+
import (
20+
"fmt"
21+
"sync"
22+
23+
"github.com/dragonflyoss/Dragonfly/dfget/corev2/basic"
24+
)
25+
26+
// NewNotify creates Notify which implements basic.Notify.
27+
func NewNotify() *Notify {
28+
return &Notify{}
29+
}
30+
31+
// Notify is an implementation of basic.Notify.
32+
type Notify struct {
33+
sync.RWMutex
34+
done chan struct{}
35+
result basic.NotifyResult
36+
}
37+
38+
func (notify *Notify) Done() <-chan struct{} {
39+
return notify.done
40+
}
41+
42+
// Result returns the NotifyResult and only valid after Done channel is closed.
43+
func (notify *Notify) Result() basic.NotifyResult {
44+
notify.RLock()
45+
defer notify.RUnlock()
46+
47+
return notify.result
48+
}
49+
50+
// Finish sets result and close done channel to notify work done.
51+
func (notify *Notify) Finish(result basic.NotifyResult) error {
52+
notify.Lock()
53+
defer notify.Unlock()
54+
55+
if notify.result != nil {
56+
return fmt.Errorf("result have been set once")
57+
}
58+
59+
notify.result = result
60+
close(notify.done)
61+
return nil
62+
}
63+
64+
// NotifyResult is an implementation of basic.NotifyResult.
65+
type notifyResult struct {
66+
success bool
67+
err error
68+
data interface{}
69+
}
70+
71+
func NewNotifyResult(success bool, err error, data interface{}) basic.NotifyResult {
72+
return &notifyResult{
73+
success: success,
74+
err: err,
75+
data: data,
76+
}
77+
}
78+
79+
func (nr notifyResult) Success() bool {
80+
return nr.success
81+
}
82+
83+
func (nr notifyResult) Error() error {
84+
return nr.err
85+
}
86+
87+
func (nr notifyResult) Data() interface{} {
88+
return nr.data
89+
}

0 commit comments

Comments
 (0)