get rid of WriteJSON() and replace calls with manually marshaled envelopes.

This commit is contained in:
fiatjaf
2023-05-09 17:02:22 -03:00
parent d36fbb95b9
commit ccbb44989f
4 changed files with 46 additions and 50 deletions

View File

@@ -4,7 +4,6 @@ import (
"bytes"
"compress/flate"
"context"
"encoding/json"
"errors"
"fmt"
"io"
@@ -105,34 +104,6 @@ func NewConnection(ctx context.Context, url string, requestHeader http.Header) (
}, nil
}
func (c *Connection) WriteJSON(v any) error {
c.mutex.Lock()
defer c.mutex.Unlock()
if c.enableCompression && c.msgState.IsCompressed() {
c.flateWriter.Reset(c.writer)
if err := json.NewEncoder(c.flateWriter).Encode(v); err != nil {
return fmt.Errorf("failed to encode json: %w", err)
}
err := c.flateWriter.Close()
if err != nil {
return fmt.Errorf("failed to close flate writer: %w", err)
}
} else {
if err := json.NewEncoder(c.writer).Encode(v); err != nil {
return fmt.Errorf("failed to encode json: %w", err)
}
}
err := c.writer.Flush()
if err != nil {
return fmt.Errorf("failed to flush writer: %w", err)
}
return nil
}
func (c *Connection) Ping() error {
c.mutex.Lock()
defer c.mutex.Unlock()