Skip to content

Commit ba322ab

Browse files
committed
Introduce file name limit setting
1 parent d550528 commit ba322ab

File tree

2 files changed

+45
-25
lines changed

2 files changed

+45
-25
lines changed

sender.go

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ import (
4848
// chars found in table or column name.
4949
var ErrInvalidMsg = errors.New("invalid message")
5050

51-
const fsFileNameLimit = 127
51+
const (
52+
defaultBufferCapacity = 128 * 1024
53+
defaultFileNameLimit = 127
54+
)
5255

5356
type tlsMode int64
5457

@@ -64,18 +67,19 @@ const (
6467
// Each sender corresponds to a single TCP connection. A sender
6568
// should not be called concurrently by multiple goroutines.
6669
type LineSender struct {
67-
address string
68-
tlsMode tlsMode
69-
keyId string // Erased once auth is done.
70-
key string // Erased once auth is done.
71-
bufCap int
72-
conn net.Conn
73-
buf *buffer
74-
lastMsgPos int
75-
lastErr error
76-
hasTable bool
77-
hasTags bool
78-
hasFields bool
70+
address string
71+
tlsMode tlsMode
72+
keyId string // Erased once auth is done.
73+
key string // Erased once auth is done.
74+
bufCap int
75+
fileNameLimit int
76+
conn net.Conn
77+
buf *buffer
78+
lastMsgPos int
79+
lastErr error
80+
hasTable bool
81+
hasTags bool
82+
hasFields bool
7983
}
8084

8185
// LineSenderOption defines line sender option.
@@ -128,6 +132,18 @@ func WithBufferCapacity(capacity int) LineSenderOption {
128132
}
129133
}
130134

135+
// WithFileNameLimit sets maximum file name length in chars
136+
// allowed by the server. Affects maximum table and column name
137+
// lengths accepted by the sender. Should be set to the same value
138+
// as on the server. Defaults to 127.
139+
func WithFileNameLimit(limit int) LineSenderOption {
140+
return func(s *LineSender) {
141+
if limit > 0 {
142+
s.fileNameLimit = limit
143+
}
144+
}
145+
}
146+
131147
// NewLineSender creates new InfluxDB Line Protocol (ILP) sender. Each
132148
// sender corresponds to a single TCP connection. Sender should
133149
// not be called concurrently by multiple goroutines.
@@ -140,9 +156,10 @@ func NewLineSender(ctx context.Context, opts ...LineSenderOption) (*LineSender,
140156
)
141157

142158
s := &LineSender{
143-
address: "127.0.0.1:9009",
144-
bufCap: 128 * 1024,
145-
tlsMode: noTls,
159+
address: "127.0.0.1:9009",
160+
bufCap: defaultBufferCapacity,
161+
fileNameLimit: defaultFileNameLimit,
162+
tlsMode: noTls,
146163
}
147164
for _, opt := range opts {
148165
opt(s)
@@ -379,8 +396,8 @@ func (s *LineSender) writeTableName(str string) error {
379396
}
380397
// We use string length in bytes as an approximation. That's to
381398
// avoid calculating the number of runes.
382-
if len(str) > fsFileNameLimit {
383-
return fmt.Errorf("table name length is too large: %w", ErrInvalidMsg)
399+
if len(str) > s.fileNameLimit {
400+
return fmt.Errorf("table name length exceeds the limit: %w", ErrInvalidMsg)
384401
}
385402
// Since we're interested in ASCII chars, it's fine to iterate
386403
// through bytes instead of runes.
@@ -479,8 +496,8 @@ func (s *LineSender) writeColumnName(str string) error {
479496
}
480497
// We use string length in bytes as an approximation. That's to
481498
// avoid calculating the number of runes.
482-
if len(str) > fsFileNameLimit {
483-
return fmt.Errorf("column name length is too large: %w", ErrInvalidMsg)
499+
if len(str) > s.fileNameLimit {
500+
return fmt.Errorf("column name length exceeds the limit: %w", ErrInvalidMsg)
484501
}
485502
// Since we're interested in ASCII chars, it's fine to iterate
486503
// through bytes instead of runes.

sender_test.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,9 +194,12 @@ func TestFloat64Serialization(t *testing.T) {
194194
}
195195

196196
func TestErrorOnLengthyNames(t *testing.T) {
197-
ctx := context.Background()
197+
const nameLimit = 42
198198

199-
lengthyStr := strings.Repeat("a", 128)
199+
var (
200+
lengthyStr = strings.Repeat("a", nameLimit+1)
201+
ctx = context.Background()
202+
)
200203

201204
testCases := []struct {
202205
name string
@@ -208,14 +211,14 @@ func TestErrorOnLengthyNames(t *testing.T) {
208211
func(s *qdb.LineSender) error {
209212
return s.Table(lengthyStr).StringColumn("str_col", "foo").AtNow(ctx)
210213
},
211-
"table name length is too large",
214+
"table name length exceeds the limit",
212215
},
213216
{
214217
"lengthy column name",
215218
func(s *qdb.LineSender) error {
216219
return s.Table(testTable).StringColumn(lengthyStr, "foo").AtNow(ctx)
217220
},
218-
"column name length is too large",
221+
"column name length exceeds the limit",
219222
},
220223
}
221224

@@ -224,7 +227,7 @@ func TestErrorOnLengthyNames(t *testing.T) {
224227
srv, err := newTestServer(readAndDiscard)
225228
assert.NoError(t, err)
226229

227-
sender, err := qdb.NewLineSender(ctx, qdb.WithAddress(srv.addr))
230+
sender, err := qdb.NewLineSender(ctx, qdb.WithAddress(srv.addr), qdb.WithFileNameLimit(nameLimit))
228231
assert.NoError(t, err)
229232

230233
err = tc.writerFn(sender)

0 commit comments

Comments
 (0)