@@ -34,7 +34,7 @@ type SSE struct {
34
34
sseReadTimeout time.Duration
35
35
36
36
started atomic.Bool
37
- closed chan struct {}
37
+ closed atomic. Bool
38
38
cancelSSEStream context.CancelFunc
39
39
}
40
40
@@ -64,7 +64,6 @@ func NewSSE(baseURL string, options ...ClientOption) (*SSE, error) {
64
64
baseURL : parsedURL ,
65
65
httpClient : & http.Client {},
66
66
responses : make (map [int64 ]chan * JSONRPCResponse ),
67
- closed : make (chan struct {}),
68
67
endpointChan : make (chan struct {}),
69
68
sseReadTimeout : 30 * time .Second ,
70
69
headers : make (map [string ]string ),
@@ -91,9 +90,7 @@ func (c *SSE) Start(ctx context.Context) error {
91
90
req , err := http .NewRequestWithContext (ctx , "GET" , c .baseURL .String (), nil )
92
91
93
92
if err != nil {
94
-
95
93
return fmt .Errorf ("failed to create request: %w" , err )
96
-
97
94
}
98
95
99
96
req .Header .Set ("Accept" , "text/event-stream" )
@@ -153,13 +150,10 @@ func (c *SSE) readSSE(reader io.ReadCloser) {
153
150
}
154
151
break
155
152
}
156
- select {
157
- case <- c .closed :
158
- return
159
- default :
153
+ if ! c .closed .Load () {
160
154
fmt .Printf ("SSE stream error: %v\n " , err )
161
- return
162
155
}
156
+ return
163
157
}
164
158
165
159
// Remove only newline markers
@@ -248,9 +242,11 @@ func (c *SSE) SendRequest(
248
242
) (* JSONRPCResponse , error ) {
249
243
250
244
if ! c .started .Load () {
251
- return nil , fmt .Errorf ("transport not started" )
245
+ return nil , fmt .Errorf ("transport not started yet" )
246
+ }
247
+ if c .closed .Load () {
248
+ return nil , fmt .Errorf ("transport has been closed" )
252
249
}
253
-
254
250
if c .endpoint == nil {
255
251
return nil , fmt .Errorf ("endpoint not received" )
256
252
}
@@ -311,11 +307,8 @@ func (c *SSE) SendRequest(
311
307
// Close shuts down the SSE client connection and cleans up any pending responses.
312
308
// Returns an error if the shutdown process fails.
313
309
func (c * SSE ) Close () error {
314
- select {
315
- case <- c .closed :
310
+ if ! c .closed .CompareAndSwap (false , true ) {
316
311
return nil // Already closed
317
- default :
318
- close (c .closed )
319
312
}
320
313
321
314
if c .cancelSSEStream != nil {
0 commit comments