Skip to content

Commit

Permalink
deprecate NegotiateLazy
Browse files Browse the repository at this point in the history
  • Loading branch information
marten-seemann committed Apr 21, 2022
1 parent 90f2bb1 commit 1ce51bf
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 155 deletions.
51 changes: 0 additions & 51 deletions lazyServer.go

This file was deleted.

90 changes: 3 additions & 87 deletions multistream.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,94 +188,10 @@ func (msm *MultistreamMuxer) findHandler(proto string) *Handler {
// a multistream, the protocol used, the handler and an error. It is lazy
// because the write-handshake is performed on a subroutine, allowing this
// to return before that handshake is completed.
// Deprecated: use Negotiate instead.
func (msm *MultistreamMuxer) NegotiateLazy(rwc io.ReadWriteCloser) (rwc_ io.ReadWriteCloser, proto string, handler HandlerFunc, err error) {
defer func() {
if rerr := recover(); rerr != nil {
fmt.Fprintf(os.Stderr, "caught panic: %s\n%s\n", rerr, debug.Stack())
err = fmt.Errorf("panic in lazy multistream negotiation: %s", rerr)
}
}()

pval := make(chan string, 1)
writeErr := make(chan error, 1)
defer close(pval)

lzc := &lazyServerConn{
con: rwc,
}

started := make(chan struct{})
go lzc.waitForHandshake.Do(func() {
defer func() {
if rerr := recover(); rerr != nil {
fmt.Fprintf(os.Stderr, "caught panic: %s\n%s\n", rerr, debug.Stack())
err := fmt.Errorf("panic in lazy multistream negotiation, waiting for handshake: %s", rerr)
lzc.werr = err
writeErr <- err
}
}()

close(started)

defer close(writeErr)

if err := delimWriteBuffered(rwc, []byte(ProtocolID)); err != nil {
lzc.werr = err
writeErr <- err
return
}

for proto := range pval {
if err := delimWriteBuffered(rwc, []byte(proto)); err != nil {
lzc.werr = err
writeErr <- err
return
}
}
})
<-started

line, err := ReadNextToken(rwc)
if err != nil {
return nil, "", nil, err
}

if line != ProtocolID {
rwc.Close()
return nil, "", nil, ErrIncorrectVersion
}

loop:
for {
// Now read and respond to commands until they send a valid protocol id
tok, err := ReadNextToken(rwc)
if err != nil {
rwc.Close()
return nil, "", nil, err
}

h := msm.findHandler(tok)
if h == nil {
select {
case pval <- "na":
case err := <-writeErr:
rwc.Close()
return nil, "", nil, err
}
continue loop
}

select {
case pval <- tok:
case <-writeErr:
// explicitly ignore this error. It will be returned to any
// writers and if we don't plan on writing anything, we still
// want to complete the handshake
}

// hand off processing to the sub-protocol handler
return lzc, tok, h.Handle, nil
}
proto, handler, err = msm.Negotiate(rwc)
return rwc, proto, handler, err
}

// Negotiate performs protocol selection and returns the protocol name and
Expand Down
18 changes: 1 addition & 17 deletions multistream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,6 @@ func TestNegLazyStressWrite(t *testing.T) {
return
}

_, err = m.Read(nil)
if err != nil {
t.Error(err)
return
}

_, err = m.Write(message)
if err != nil {
t.Error(err)
Expand Down Expand Up @@ -670,17 +664,7 @@ func TestNegotiateFail(t *testing.T) {
rob := &readonlyBuffer{bytes.NewReader(buf.Bytes())}
_, _, err = mux.Negotiate(rob)
if err == nil {
t.Fatal("normal negotiate should fail here")
}

rob = &readonlyBuffer{bytes.NewReader(buf.Bytes())}
_, out, _, err := mux.NegotiateLazy(rob)
if err != nil {
t.Fatal("expected lazy negoatiate to succeed")
}

if out != "foo" {
t.Fatal("got wrong protocol")
t.Fatal("Negotiate should fail here")
}
}

Expand Down

0 comments on commit 1ce51bf

Please sign in to comment.