Skip to content

Commit eefb5df

Browse files
authored
fix(factory): on reload, close the connection right away. (#235)
* fix(factory): on reload, close the connection right away. graceful shutdown logic should be implemented in the close function, not in the factory. * fix(cron): test flakes
1 parent eaf7ad9 commit eefb5df

3 files changed

Lines changed: 5 additions & 38 deletions

File tree

cron/options_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func TestJobOption(t *testing.T) {
179179
},
180180
func(ctx context.Context) error {
181181
entryCount++
182-
time.Sleep(5 * time.Millisecond)
182+
time.Sleep(6 * time.Millisecond)
183183
return nil
184184
},
185185
func(t *testing.T) {

di/factory.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ package di
22

33
import (
44
"context"
5-
"reflect"
6-
"runtime"
75
"sync"
86

97
"golang.org/x/sync/singleflight"
@@ -69,17 +67,7 @@ func (f *Factory) SubscribeReloadEventFrom(dispatcher contract.Dispatcher) {
6967
if pair.Closer == nil {
7068
return true
7169
}
72-
finalized := make(chan struct{})
73-
if reflect.TypeOf(pair.Conn).Kind() == reflect.Ptr {
74-
runtime.SetFinalizer(pair.Conn, func(_ interface{}) { finalized <- struct{}{} })
75-
}
76-
go func() {
77-
select {
78-
case <-ctx.Done():
79-
case <-finalized:
80-
}
81-
pair.Closer()
82-
}()
70+
pair.Closer()
8371
return true
8472
})
8573
return nil

di/factory_test.go

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"errors"
66
"math/rand"
7-
"runtime"
87
"testing"
98
"time"
109

@@ -115,41 +114,21 @@ func TestFactory_Watch(t *testing.T) {
115114
func TestFactory_SubscribeReloadEventFrom(t *testing.T) {
116115
t.Parallel()
117116

118-
var (
119-
ptr = &struct {
120-
Dummy string
121-
}{Dummy: "dummy"}
122-
closed = make(chan struct{})
123-
)
117+
closed := make(chan struct{})
124118
ctx, cancel := context.WithCancel(context.Background())
125119
defer cancel()
126120
f := NewFactory(func(_ string) (Pair, error) {
127121
return Pair{
128-
Conn: ptr,
122+
Conn: &struct{}{},
129123
Closer: func() { close(closed) },
130124
}, nil
131125
})
132126
dispatcher := events.SyncDispatcher{}
133127
f.SubscribeReloadEventFrom(&dispatcher)
134128

135-
foo, err := f.Make("default")
136-
assert.NoError(t, err)
137-
assert.Same(t, ptr, foo)
138-
129+
f.Make("default")
139130
_ = dispatcher.Dispatch(ctx, events.OnReload, events.OnReloadPayload{})
140131

141-
// We don't want to interrupt ongoing request, so foo should not be closed by now
142-
select {
143-
case <-closed:
144-
t.Fatalf("foo should not be closed.")
145-
default:
146-
}
147-
148-
// now that foo is garbage collected, we can safely close foo.
149-
ptr = nil //nolint
150-
foo = nil //nolint
151-
runtime.GC()
152-
cancel()
153132
select {
154133
case <-closed:
155134
case <-time.After(4 * time.Second):

0 commit comments

Comments
 (0)