3939 closed chan struct {}
4040 trace * trace.Query
4141 statsCallback func (queryStats stats.QueryStats )
42+ onClose []func ()
4243 onNextPartErr []func (err error )
4344 onTxMeta []func (txMeta * Ydb_Query.TransactionMeta )
4445 }
@@ -98,6 +99,12 @@ func withStatsCallback(callback func(queryStats stats.QueryStats)) resultOption
9899 }
99100}
100101
102+ func withOnClose (onClose func ()) resultOption {
103+ return func (s * streamResult ) {
104+ s .onClose = append (s .onClose , onClose )
105+ }
106+ }
107+
101108func onNextPartErr (callback func (err error )) resultOption {
102109 return func (s * streamResult ) {
103110 s .onNextPartErr = append (s .onNextPartErr , callback )
@@ -115,22 +122,33 @@ func newResult(
115122 stream Ydb_Query_V1.QueryService_ExecuteQueryClient ,
116123 opts ... resultOption ,
117124) (_ * streamResult , finalErr error ) {
118- r := streamResult {
119- stream : stream ,
120- closed : make (chan struct {}),
121- resultSetIndex : - 1 ,
122- }
123- r .closeOnce = sync .OnceFunc (func () {
124- close (r .closed )
125- r .stream = nil
126- })
125+ var (
126+ closed = make (chan struct {})
127+ r = streamResult {
128+ stream : stream ,
129+ onClose : []func (){
130+ func () {
131+ close (closed )
132+ },
133+ },
134+ closed : closed ,
135+ resultSetIndex : - 1 ,
136+ }
137+ )
127138
128139 for _ , opt := range opts {
129140 if opt != nil {
130141 opt (& r )
131142 }
132143 }
133144
145+ r .closeOnce = sync .OnceFunc (func () {
146+ for _ , onClose := range r .onClose {
147+ onClose ()
148+ }
149+ r .stream = nil
150+ })
151+
134152 if r .trace != nil {
135153 onDone := trace .QueryOnResultNew (r .trace , & ctx ,
136154 stack .FunctionID ("github.com/ydb-platform/ydb-go-sdk/v3/internal/query.newResult" ),
0 commit comments