Skip to content

Commit 5c2d2e0

Browse files
deprecate NegotiateLazy (#85)
1 parent cbe57f2 commit 5c2d2e0

3 files changed

Lines changed: 4 additions & 155 deletions

File tree

lazyServer.go

Lines changed: 0 additions & 51 deletions
This file was deleted.

multistream.go

Lines changed: 3 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -188,94 +188,10 @@ func (msm *MultistreamMuxer) findHandler(proto string) *Handler {
188188
// a multistream, the protocol used, the handler and an error. It is lazy
189189
// because the write-handshake is performed on a subroutine, allowing this
190190
// to return before that handshake is completed.
191+
// Deprecated: use Negotiate instead.
191192
func (msm *MultistreamMuxer) NegotiateLazy(rwc io.ReadWriteCloser) (rwc_ io.ReadWriteCloser, proto string, handler HandlerFunc, err error) {
192-
defer func() {
193-
if rerr := recover(); rerr != nil {
194-
fmt.Fprintf(os.Stderr, "caught panic: %s\n%s\n", rerr, debug.Stack())
195-
err = fmt.Errorf("panic in lazy multistream negotiation: %s", rerr)
196-
}
197-
}()
198-
199-
pval := make(chan string, 1)
200-
writeErr := make(chan error, 1)
201-
defer close(pval)
202-
203-
lzc := &lazyServerConn{
204-
con: rwc,
205-
}
206-
207-
started := make(chan struct{})
208-
go lzc.waitForHandshake.Do(func() {
209-
defer func() {
210-
if rerr := recover(); rerr != nil {
211-
fmt.Fprintf(os.Stderr, "caught panic: %s\n%s\n", rerr, debug.Stack())
212-
err := fmt.Errorf("panic in lazy multistream negotiation, waiting for handshake: %s", rerr)
213-
lzc.werr = err
214-
writeErr <- err
215-
}
216-
}()
217-
218-
close(started)
219-
220-
defer close(writeErr)
221-
222-
if err := delimWriteBuffered(rwc, []byte(ProtocolID)); err != nil {
223-
lzc.werr = err
224-
writeErr <- err
225-
return
226-
}
227-
228-
for proto := range pval {
229-
if err := delimWriteBuffered(rwc, []byte(proto)); err != nil {
230-
lzc.werr = err
231-
writeErr <- err
232-
return
233-
}
234-
}
235-
})
236-
<-started
237-
238-
line, err := ReadNextToken(rwc)
239-
if err != nil {
240-
return nil, "", nil, err
241-
}
242-
243-
if line != ProtocolID {
244-
rwc.Close()
245-
return nil, "", nil, ErrIncorrectVersion
246-
}
247-
248-
loop:
249-
for {
250-
// Now read and respond to commands until they send a valid protocol id
251-
tok, err := ReadNextToken(rwc)
252-
if err != nil {
253-
rwc.Close()
254-
return nil, "", nil, err
255-
}
256-
257-
h := msm.findHandler(tok)
258-
if h == nil {
259-
select {
260-
case pval <- "na":
261-
case err := <-writeErr:
262-
rwc.Close()
263-
return nil, "", nil, err
264-
}
265-
continue loop
266-
}
267-
268-
select {
269-
case pval <- tok:
270-
case <-writeErr:
271-
// explicitly ignore this error. It will be returned to any
272-
// writers and if we don't plan on writing anything, we still
273-
// want to complete the handshake
274-
}
275-
276-
// hand off processing to the sub-protocol handler
277-
return lzc, tok, h.Handle, nil
278-
}
193+
proto, handler, err = msm.Negotiate(rwc)
194+
return rwc, proto, handler, err
279195
}
280196

281197
// Negotiate performs protocol selection and returns the protocol name and

multistream_test.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,6 @@ func TestNegLazyStressWrite(t *testing.T) {
248248
return
249249
}
250250

251-
_, err = m.Read(nil)
252-
if err != nil {
253-
t.Error(err)
254-
return
255-
}
256-
257251
_, err = m.Write(message)
258252
if err != nil {
259253
t.Error(err)
@@ -690,17 +684,7 @@ func TestNegotiateFail(t *testing.T) {
690684
rob := &readonlyBuffer{bytes.NewReader(buf.Bytes())}
691685
_, _, err = mux.Negotiate(rob)
692686
if err == nil {
693-
t.Fatal("normal negotiate should fail here")
694-
}
695-
696-
rob = &readonlyBuffer{bytes.NewReader(buf.Bytes())}
697-
_, out, _, err := mux.NegotiateLazy(rob)
698-
if err != nil {
699-
t.Fatal("expected lazy negoatiate to succeed")
700-
}
701-
702-
if out != "foo" {
703-
t.Fatal("got wrong protocol")
687+
t.Fatal("Negotiate should fail here")
704688
}
705689
}
706690

0 commit comments

Comments
 (0)