-
Notifications
You must be signed in to change notification settings - Fork 243
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/master' into stable
- Loading branch information
Showing
23 changed files
with
340 additions
and
459 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package utils | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/9seconds/mtg/v2/network" | ||
) | ||
|
||
type Listener struct { | ||
net.Listener | ||
|
||
bufferSize int | ||
} | ||
|
||
func (l Listener) Accept() (net.Conn, error) { | ||
conn, err := l.Listener.Accept() | ||
if err != nil { | ||
return nil, err // nolint: wrapcheck | ||
} | ||
|
||
if err := network.SetClientSocketOptions(conn, l.bufferSize); err != nil { | ||
conn.Close() | ||
|
||
return nil, fmt.Errorf("cannot set TCP options: %w", err) | ||
} | ||
|
||
return conn, nil | ||
} | ||
|
||
func NewListener(bindTo string, bufferSize int) (net.Listener, error) { | ||
base, err := net.Listen("tcp", bindTo) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot build a base listener: %w", err) | ||
} | ||
|
||
return Listener{ | ||
Listener: base, | ||
bufferSize: bufferSize, | ||
}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,19 @@ | ||
package relay | ||
|
||
import ( | ||
"context" | ||
"io" | ||
"fmt" | ||
"net" | ||
"time" | ||
) | ||
|
||
type conn struct { | ||
io.ReadWriteCloser | ||
|
||
ctx context.Context | ||
tickChannel chan struct{} | ||
net.Conn | ||
} | ||
|
||
func (c conn) Read(p []byte) (int, error) { | ||
n, err := c.ReadWriteCloser.Read(p) | ||
|
||
select { | ||
case <-c.ctx.Done(): | ||
case c.tickChannel <- struct{}{}: | ||
} | ||
|
||
return n, err // nolint: wrapcheck | ||
} | ||
|
||
func (c conn) Write(p []byte) (int, error) { | ||
n, err := c.ReadWriteCloser.Write(p) | ||
|
||
select { | ||
case <-c.ctx.Done(): | ||
case c.tickChannel <- struct{}{}: | ||
if err := c.SetReadDeadline(time.Now().Add(getTimeout())); err != nil { | ||
return 0, fmt.Errorf("cannot set read deadline: %w", err) | ||
} | ||
|
||
return n, err // nolint: wrapcheck | ||
return c.Conn.Read(p) // nolint: wrapcheck | ||
} |
Oops, something went wrong.