You are an expert in writing code in many different languages. Your goal is to perform code completion for the following code, keeping in mind the rest of the code and the file meta data.
    Metadata details:
    filename: publisher.go
    path of file: ./repos/magistrala/pkg/events/redis
    
    the code of the file until where you have to start completion: // Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

package redis

import (
	"context"
	"sync"
	"time"

	"github.com/absmach/magistrala/pkg/events"
	"github.com/go-redis/redis/v8"
)

type pubEventStore struct {
	client            *redis.Client
	unpublishedEvents chan *redis.XAddArgs
	stream            string
	mu                sync.Mutex
	flushPerio
    
func (es *pubEventStore) Publish(ctx context.Context, event events.Event) error {
	values, err := event.Encode()
	if err != nil {
		return err
	}
	values["occurred_at"] = time.Now().UnixNano()

	record := &redis.XAddArgs{
		Stream: es.stream,
		MaxLen: events.MaxEventStreamLen,
		Approx: true,
		Values: values,
	}

	switch err := es.checkConnection(ctx); err {
	case nil:
		return es.client.XAdd(ctx, record).Err()
	default:
		es.mu.Lock()
		defer es.mu.Unlock()

		// If the channel is full (rarely happens), drop the events.
		if len(es.unpublishedEvents) == int(events.MaxUnpublishedEvents) {
			return nil
		}

		es.unpublishedEvents <- record

		return nil
	}
}